Encryption function gives different output on windows and unix
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 decryp开发者_运维百科tion 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();
}
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?
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.
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.
精彩评论