首页 > 代码库 > 3DES 加密解密

3DES 加密解密

/// <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 加密解密