C# .NET 加密不能正确解密
C# .NET Cryptography don't decrypt properly
我正在尝试用 C# (.NET 3.5) 加密和解密文本,发现这个简单的代码不起作用:
private const string KEY = "Chiave";
static void Main(string[] args)
{
string plainText = "Data to be encrypted";
byte[] keyArray;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(KEY));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(plainText);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] encArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
cTransform = tdes.CreateDecryptor();
byte[] decArray = cTransform.TransformFinalBlock(encArray, 0, encArray.Length);
if (encArray.Length == decArray.Length)
{
for (int i = 0; i < encArray.Length; ++i)
Console.Out.Write("{0,3}|{1,3}", encArray[i], decArray[i]);
} else
Console.Out.Write("Length error!");
Console.In.Read();
}
似乎加密或解密阶段弄乱了文本开头的一些字节(每次我 运行 程序都以不同的方式)有时甚至会改变字节数组的长度。
我能够使用 ECB 密码使其工作,但我的数据有一些静态块,它们随时都会导致相同的结果。
来自 https://msdn.microsoft.com/it-it/library/system.security.cryptography.ciphermode(v=vs.110).aspx 关于欧洲央行:
Important: This mode is not recommended because it opens the door for
multiple security exploits. If the plain text to be encrypted contains
substantial repetition, it is feasible for the cipher text to be
broken one block at a time. It is also possible to use block analysis
to determine the encryption key. Also, an active adversary can
substitute and exchange individual blocks without detection, which
allows blocks to be saved and inserted into the stream at other points
without detection.
这些是enc之后的一些结果。和 dec.:
��u��T�be encrypted
U����ŋbe encrypted
5�AL\"0be encrypted
等等,多谢指教
您已经创建了 TripleDESCryptoServiceProvider
的新实例,但是这个新实例的 IV 值与第一个实例不同
另外,将加密数组的长度与解密数组的长度进行比较是不正确的。您应该尝试取回原始字符串并检查两者是否相等
private const string KEY = "Chiave";
static void Main(string[] args)
{
string plainText = "Data to be encrypted";
byte[] keyArray;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(KEY));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(plainText);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] encArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// REMOVE THESE LINES
// tdes = new TripleDESCryptoServiceProvider();
// tdes.Key = keyArray;
// tdes.Mode = CipherMode.CBC;
// tdes.Padding = PaddingMode.PKCS7;
cTransform = tdes.CreateDecryptor();
byte[] decArray = cTransform.TransformFinalBlock(encArray, 0, encArray.Length);
// if (encArray.Length == decArray.Length)
// {
// for (int i = 0; i < encArray.Length; ++i)
// Console.Out.Write("{0,3}|{1,3}", encArray[i], decArray[i]);
//} else
// Console.Out.Write("Length error!");
string result = UTF8Encoding.UTF8.GetString(decArray);
Console.WriteLine(result);
Console.In.Read();
}
我正在尝试用 C# (.NET 3.5) 加密和解密文本,发现这个简单的代码不起作用:
private const string KEY = "Chiave";
static void Main(string[] args)
{
string plainText = "Data to be encrypted";
byte[] keyArray;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(KEY));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(plainText);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] encArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
cTransform = tdes.CreateDecryptor();
byte[] decArray = cTransform.TransformFinalBlock(encArray, 0, encArray.Length);
if (encArray.Length == decArray.Length)
{
for (int i = 0; i < encArray.Length; ++i)
Console.Out.Write("{0,3}|{1,3}", encArray[i], decArray[i]);
} else
Console.Out.Write("Length error!");
Console.In.Read();
}
似乎加密或解密阶段弄乱了文本开头的一些字节(每次我 运行 程序都以不同的方式)有时甚至会改变字节数组的长度。 我能够使用 ECB 密码使其工作,但我的数据有一些静态块,它们随时都会导致相同的结果。
来自 https://msdn.microsoft.com/it-it/library/system.security.cryptography.ciphermode(v=vs.110).aspx 关于欧洲央行:
Important: This mode is not recommended because it opens the door for multiple security exploits. If the plain text to be encrypted contains substantial repetition, it is feasible for the cipher text to be broken one block at a time. It is also possible to use block analysis to determine the encryption key. Also, an active adversary can substitute and exchange individual blocks without detection, which allows blocks to be saved and inserted into the stream at other points without detection.
这些是enc之后的一些结果。和 dec.:
��u��T�be encrypted
U����ŋbe encrypted
5�AL\"0be encrypted
等等,多谢指教
您已经创建了 TripleDESCryptoServiceProvider
的新实例,但是这个新实例的 IV 值与第一个实例不同
另外,将加密数组的长度与解密数组的长度进行比较是不正确的。您应该尝试取回原始字符串并检查两者是否相等
private const string KEY = "Chiave";
static void Main(string[] args)
{
string plainText = "Data to be encrypted";
byte[] keyArray;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(KEY));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(plainText);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] encArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// REMOVE THESE LINES
// tdes = new TripleDESCryptoServiceProvider();
// tdes.Key = keyArray;
// tdes.Mode = CipherMode.CBC;
// tdes.Padding = PaddingMode.PKCS7;
cTransform = tdes.CreateDecryptor();
byte[] decArray = cTransform.TransformFinalBlock(encArray, 0, encArray.Length);
// if (encArray.Length == decArray.Length)
// {
// for (int i = 0; i < encArray.Length; ++i)
// Console.Out.Write("{0,3}|{1,3}", encArray[i], decArray[i]);
//} else
// Console.Out.Write("Length error!");
string result = UTF8Encoding.UTF8.GetString(decArray);
Console.WriteLine(result);
Console.In.Read();
}