加密函数在 windows 和 unix 上给出不同的输出

发布于 2024-11-04 07:49:44 字数 2281 浏览 0 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-11-11 07:49:44

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

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

当 KEY、
IV 和加密数据并不全部
正确的块大小和
'方案'。避免这种情况的唯一方法
问题是使用IV和KEY
由算法生成。你可以
使用GenerateIV得到算法
为您生成 IV。将其存放起来
安全的地方,因为您将需要它。
然后只需调用加密方法即可
并传入数据。算法
然后将加密数据并设置
新生成的关键属性
钥匙。将其与您的加密存储一起
数据。这就是全部内容。

尽管它已经过时,但答复称存在差异的原因有多种,但如果您可以解密,那么差异的原因可能是有效的,如下所示:
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/

You get this problem when the KEY, the
IV and the ENCRYPTED DATA are not all
of the correct block sizes and
'scheme'. The only way to avoid this
problem is to use the IV and KEY
generated by the algorithm. You can
use GenerateIV to get the algorithm to
generate you an IV. Store this away
somewhere safe as you will need it.
Then simply call the encrypt method
and pass in the data. The algorithm
will then encrypt the data and set the
Key property to the newly generated
key. Store this with your encrypted
data. That's all there is to it.

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?

稚然 2024-11-11 07:49:44

也许问题是输出是 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.

沙沙粒小 2024-11-11 07:49:44

您检查过测试字符串中是否有新行吗? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文