#region 3DES
///
/// 3DES加密
///
/// 需加密的字符串
/// 密匙
///
public static string DES3Encrypt(string strString, string strKey)
{
strString = strString + "".PadLeft(8);
byte[] bMsg = Encoding.GetEncoding("GBK").GetBytes(strString);
int l = (bMsg.Length / 16 + 1) * 16;
byte[] btMsg = new byte[l];
Array.Copy(bMsg, btMsg, bMsg.Length);
byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[24];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = 0, k = 16; j <8; )
{
keyBytes[k++] = keyBytes[j++];
}
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes);
var dd = DESEncrypt.TransformFinalBlock(btMsg, 0, btMsg.Length);
return byte2hex(dd).ToString().Substring(0, (bMsg.Length / 8 + 1) * 16);
}
public static String getAdd(int length, String strKey)
{
byte[] btMsg = new byte[length / 2];
byte[] digestOfPassword = Encoding.UTF8.GetBytes(strKey);
//byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[24];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = 0, k = 16; j <8; )
{
keyBytes[k++] = keyBytes[j++];
}
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes);
var dd = DESEncrypt.TransformFinalBlock(btMsg, 0, btMsg.Length);
String rtn = byte2hex(dd);
return rtn.Substring(length);
}
///
/// 字节数组转16进制字符串
///
///
///
private static string byte2hex(byte[] dd)
{
StringBuilder ret = new StringBuilder();
foreach (var item in dd)
{
ret.AppendFormat("{0:X2}", item);
}
return ret.ToString();
}
///
/// 字符串转16进制字节数组
///
///
///
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i )
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
///
/// 3DES解密
///
/// 需解密的字符串
/// 密匙
///
public static string DES3Decrypt(string strString, string strKey)
{
strString += getAdd(strString.Length, strKey);
byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[24];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = 0, k = 16; j <8; )
{
keyBytes[k++] = keyBytes[j++];
}
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor(keyBytes, keyBytes);
string result = "";
try
{
byte[] Buffer = strToToHexByte(strString);
byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);
result = Encoding.GetEncoding("GBK").GetString(bb);
}
catch (Exception e)
{
throw e;
}
return result;
}
#endregion
#region 3DES
///
/// 3DES加密
///
/// 需加密的字符串
/// 密匙
///
public static string DES3Encrypt(string strString, string strKey)
{
byte[] bMsg = Encoding.UTF8.GetBytes(strString);
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
string key = strKey;
while (key.Length <24)
{
key += strKey;
}
byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[24];
Array.Copy(digestOfPassword.Take(24).ToArray(), keyBytes, digestOfPassword.Take(24).ToArray().Length);
DES.Key = keyBytes;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
var dd = DESEncrypt.TransformFinalBlock(bMsg, 0, bMsg.Length);
return Convert.ToBase64String(dd);
}
///
/// 3DES解密
///
/// 需解密的字符串
/// 密匙
///
public static string DES3Decrypt(string strString, string strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
string key = strKey;
while (key.Length <24)
{
key += strKey;
}
byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[24];
Array.Copy(digestOfPassword.Take(24).ToArray(), keyBytes, digestOfPassword.Take(24).ToArray().Length);
DES.Key = keyBytes;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(strString);
byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);
result = Encoding.UTF8.GetString(bb);
}
catch (Exception e)
{
throw e;
}
return result;
}
#endregion