码迷,mamicode.com
首页 > 其他好文 > 详细

3DES 加密解密

时间:2014-07-27 21:49:49      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 3DES 加密解密
/// </summary>
class Encrypt
{
public static string EncryptString(string Value, string txtKey, string txtIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

mCSP.Key = Encoding.UTF8.GetBytes(txtKey);
mCSP.IV = Encoding.UTF8.GetBytes(txtIV);

ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
//mCSP.Mode = CipherMode.CBC;
//mCSP.Padding = PaddingMode.PKCS7;
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}

public static string DecryptString(string Value, string txtKey, string txtIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
mCSP.Key = Encoding.UTF8.GetBytes(txtKey);
mCSP.IV = Encoding.UTF8.GetBytes(txtIV);

ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
//mCSP.Mode = CipherMode.CBC;
//mCSP.Padding = PaddingMode.PKCS7;
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
}

3DES 加密解密

标签:

原文地址:http://www.cnblogs.com/top15from/p/3871140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!