加密函数在 windows 和 unix 上给出不同的输出
我有一个用 C# 编写的加密工具,它接受一个字符串作为输入。 当我在 Windows 计算机上运行编译后的 exe 文件时,得到的输出与使用 mono 在远程 UNIX 服务器上运行时不同。
这是一个例子:
Windows:
"encrypt.exe 01/01"
Output:
eR4et6LR9P19BfFnhGwPfA==
Unix:
"mono encrypt.exe 01/01"
Output:
Pa8pJCYBN7+U+R705TFq7Q==
我什至尝试将输入值放入脚本中,然后再次编译并运行它,我得到了相同的结果。
解密函数位于远程 Web 服务上,并使用硬编码密钥和 IV 值(我正在使用这些值进行加密), 解密输出:
Input (String generated on windows):
eR4et6LR9P19BfFnhGwPfA==
Output:
01/01
Input (String generated on Unix):
Pa8pJCYBN7+U+R705TFq7Q==
Output:
????1
这是加密函数:
string text = args[0];
byte[] clearData = Encoding.Unicode.GetBytes(text);
PasswordDeriveBytes bytes = new PasswordDeriveBytes(password, new byte[] { 0x19, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0xf6 });
string a = Convert.ToBase64String(Encrypt(clearData, bytes.GetBytes(0x20), bytes.GetBytes(0x10)));
Console.Write(a);
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream stream = new MemoryStream();
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Key;
rijndael.IV = IV;
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(clearData, 0, clearData.Length);
stream2.Close();
return stream.ToArray();
}
这是解密函数(我无法对此进行更改):
byte[] cipherData = Convert.FromBase64String(encryptedString);
PasswordDeriveBytes bytes2 = new PasswordDeriveBytes(password, new byte[] { 0x19, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0xf6 });
byte[] buffer2 = Decrypt(cipherData, bytes2.GetBytes(0x20), bytes2.GetBytes(0x10));
string output = Encoding.Unicode.GetString(buffer2);
Console.Write(output);
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream stream = new MemoryStream();
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Key;
rijndael.IV = IV;
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(cipherData, 0, cipherData.Length);
stream2.Close();
return stream.ToArray();
}
I have an encryption tool written in C# that take a string as input.
When i run the compiled exe file on my windows machine i get an output that is different from when i run it on the remote UNIX server using mono.
Here is an example:
Windows:
"encrypt.exe 01/01"
Output:
eR4et6LR9P19BfFnhGwPfA==
Unix:
"mono encrypt.exe 01/01"
Output:
Pa8pJCYBN7+U+R705TFq7Q==
I even tried to put the input value in the script and then compile and run it again, and i got the same results.
The decrypt function is located on a remote web service and uses hard coded key and IV values (I'm using those values to encrypt),
Decryption output:
Input (String generated on windows):
eR4et6LR9P19BfFnhGwPfA==
Output:
01/01
Input (String generated on Unix):
Pa8pJCYBN7+U+R705TFq7Q==
Output:
????1
This is the encryption function:
string text = args[0];
byte[] clearData = Encoding.Unicode.GetBytes(text);
PasswordDeriveBytes bytes = new PasswordDeriveBytes(password, new byte[] { 0x19, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0xf6 });
string a = Convert.ToBase64String(Encrypt(clearData, bytes.GetBytes(0x20), bytes.GetBytes(0x10)));
Console.Write(a);
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream stream = new MemoryStream();
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Key;
rijndael.IV = IV;
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(clearData, 0, clearData.Length);
stream2.Close();
return stream.ToArray();
}
This is the decryption function (i cannot make changes to this):
byte[] cipherData = Convert.FromBase64String(encryptedString);
PasswordDeriveBytes bytes2 = new PasswordDeriveBytes(password, new byte[] { 0x19, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0xf6 });
byte[] buffer2 = Decrypt(cipherData, bytes2.GetBytes(0x20), bytes2.GetBytes(0x10));
string output = Encoding.Unicode.GetString(buffer2);
Console.Write(output);
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream stream = new MemoryStream();
Rijndael rijndael = Rijndael.Create();
rijndael.Key = Key;
rijndael.IV = IV;
CryptoStream stream2 = new CryptoStream(stream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(cipherData, 0, cipherData.Length);
stream2.Close();
return stream.ToArray();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题中的问题不是你正在处理的问题,但是看看结果的差异,似乎填充是一个问题,所以你可能想看看这个问题中的一些答案,但这个答案可能有助于解决你的问题。
http://social. msdn.microsoft.com/forums/en-US/clr/thread/3df8d5aa-ea99-4553-b071-42a2ea406c7f/
尽管它已经过时,但答复称存在差异的原因有多种,但如果您可以解密,那么差异的原因可能是有效的,如下所示:
http://lists.ximian.com/pipermail/mono-list /2006-November/033456.html
那么,如果您可以在一个上加密并在另一个上解密(您能做到这一点吗?),那么如果结果不同,会有什么区别呢?
The problem in this question isn't what you are dealing with, but looking at the difference in results, it appears padding was a concern, so you may want to look at some of the responses in this question, but this answer may help resolve your problem.
http://social.msdn.microsoft.com/forums/en-US/clr/thread/3df8d5aa-ea99-4553-b071-42a2ea406c7f/
Though it is dated, the response that there are various reasons for the difference, but if you can decrypt then the reasons for the difference may be valid is given here:
http://lists.ximian.com/pipermail/mono-list/2006-November/033456.html
So, if you can encrypt on one and decrypt on the other (are you able to do this?) then what difference does it make if the results are different?
也许问题是输出是 unicode,而终端显示的是 ascii。我经常看到?代替被误解的 unicode 字符。
检查字节数组的数值和数量。
Ascii 的数量是 unicode 的一半,因为每个字符有两个字节。
Perhaps the Issue is that the output is unicode, and the terminal is showing ascii. I commonly see ? in place of misunderstood unicode characters.
Check the Numerical Values of the Byte array, and the quantity.
Ascii is half the quantity of unicode as there are two bytes for each character.
您检查过测试字符串中是否有新行吗? Windows 测试字符串将具有回车+换行符,而 Unix 字符串将仅具有换行符。
Have you checked your test string for new lines? A Windows test string would have a carriage-return + line-feeds, while the Unix string would only have line-feed.