C# 3DES加密解密 3DES加密解密 3DES加密解密
3DE的加密解密
密钥:0123456789ABCDEF0123456789ABCDEF
密文:DC8E659612F5E0AD
明文:48B85E63C34EE31F
如上:密文经密钥解密后得到明文
找了几段代码,总是得不出这样的结果啊
哪位大侠以前用过,帮忙看看,给段加解密代码,能够得出这样的结果的。
实在不行,C代码也行,用C先编个动态库也用着,急啊
多谢多谢
[解决办法]
3DES分二倍长和三倍长,微软封装的是三倍长,两倍长得自已写
/// <summary>
/// ECB解密
/// </summary>
/// <param name="encryptedDataBytes"></param>
/// <param name="keys"></param>
/// <returns></returns>
public static byte[] DecryptECB(byte[] encryptedDataBytes, byte[] keys, Byte[] iv)
{
//Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
//Byte[] keys = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, key.Length));
//Byte[] encryptedDataBytes = System.Convert.FromBase64String(sourceData);
MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);
DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
decryptor.Mode = CipherMode.ECB;
decryptor.Padding = PaddingMode.None;
CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(keys, iv), CryptoStreamMode.Read);
//StreamReader allDataReader = new StreamReader(decryptionStream);
byte[] data = new byte[encryptedDataBytes.Length];
decryptionStream.Read(data, 0, data.Length);
decryptionStream.Close();
tempStream.Close();
return data;
}
/// <summary>
/// ECB加密
/// </summary>
/// <param name="sourceDataBytes"></param>
/// <param name="keys"></param>
/// <returns></returns>
public static byte[] EncryptECB(byte[] sourceDataBytes, byte[] keys, Byte[] iv)
{
//Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
MemoryStream tempStream = new MemoryStream();
//get encryptor and encryption stream
DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
encryptor.Mode = CipherMode.ECB;
encryptor.Padding = PaddingMode.None;
CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(keys, iv), CryptoStreamMode.Write);
encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
encryptionStream.FlushFinalBlock();
encryptionStream.Close();
byte[] encryptedDataBytes = tempStream.ToArray();
tempStream.Close();
return encryptedDataBytes;
}
/// <summary>
/// 3des二倍长加密
/// </summary>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] _3DES2Encrypt(byte[] key, byte[] iv, byte[] data)
{
byte[] key1 = new byte[8];
Array.Copy(key, 0, key1, 0, 8);
byte[] key2 = new byte[8];
Array.Copy(key, 8, key2, 0, 8);
byte[] data1 = MAC.EncryptECB(data,key1,iv);
data1 = MAC.DecryptECB(data1,key2,iv);
data1 = MAC.EncryptECB(data1, key1, iv);
return data1;
}
/// <summary>
/// 3des二倍长解密
/// </summary>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] _3DES2Descrypt(byte[] key, byte[] iv, byte[] data)
{
byte[] key1 = new byte[8];
Array.Copy(key, 0, key1, 0, 8);
byte[] key2 = new byte[8];
Array.Copy(key, 8, key2, 0, 8);
byte[] data1 = MAC.DecryptECB(data, key1, iv);
data1 = MAC.EncryptECB(data1, key2, iv);
data1 = MAC.DecryptECB(data1, key1, iv);
return data1;
}