加密功能在Windows和unix上提供不同的输出

use*_*247 7 .net c# encryption mono rijndael

我有一个用C#编写的加密工具,它以字符串作为输入.当我在我的Windows机器上运行已编译的exe文件时,我获得的输出与使用mono在远程UNIX服务器上运行时的输出不同.

这是一个例子:

视窗:

"encrypt.exe 01/01"
Output:
eR4et6LR9P19BfFnhGwPfA==
Run Code Online (Sandbox Code Playgroud)

Unix的:

"mono encrypt.exe 01/01"
Output:
Pa8pJCYBN7+U+R705TFq7Q==
Run Code Online (Sandbox Code Playgroud)

我甚至试图将输入值放在脚本中,然后再次编译并运行它,我得到了相同的结果.

decrypt函数位于远程Web服务上,使用硬编码密钥和IV值(我使用这些值进行加密),解密输出:

Input (String generated on windows):
eR4et6LR9P19BfFnhGwPfA==
Output:
01/01

Input (String generated on Unix):
Pa8pJCYBN7+U+R705TFq7Q==
Output:
????1
Run Code Online (Sandbox Code Playgroud)

这是加密功能:

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();
}
Run Code Online (Sandbox Code Playgroud)

这是解密功能(我不能对此进行更改):

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();
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*ack 1

这个问题中的问题不是你正在处理的问题,但是看看结果的差异,似乎填充是一个问题,所以你可能想看看这个问题中的一些答案,但这个答案可能有助于解决你的问题。

http://social.msdn.microsoft.com/forums/en-US/clr/thread/3df8d5aa-ea99-4553-b071-42a2ea406c7f/

当 KEY、IV 和加密数据并非全部都是正确的块大小和“方案”时,就会出现此问题。避免这个问题的唯一方法是使用算法生成的IV和KEY。您可以使用GenerateIV 获取生成IV 的算法。将其存放在安全的地方,以便您需要时使用。然后只需调用加密方法并传入数据即可。然后,该算法将加密数据并将 Key 属性设置为新生成的密钥。将其与您的加密数据一起存储。这里的所有都是它的。

虽然它已经过时,但有多种原因导致差异的响应,但如果您可以解密,那么差异的原因可能是有效的,如下所示: http ://lists.ximian.com/pipermail/mono-list/ 2006年11月/033456.html

那么,如果您可以对一个进行加密,对另一个进行解密(您能做到这一点吗?),那么如果结果不同,会有什么区别呢?