Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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();
}
like image 848
user735247 Avatar asked May 02 '11 23:05

user735247


People also ask

Which of the following command is used to encrypt the files in UNIX?

To encrypt and decrypt files with a password, use gpg command. It is an encryption and signing tool for Linux and UNIX-like operating systems such as FreeBSD, Solaris, MacOS and others.

How does OpenSSL encryption work?

Command line OpenSSL uses a rather simplistic method for computing the cryptographic key from a password, which we will need to mimic using the C++ API. OpenSSL uses a hash of the password and a random 64bit salt. Only a single iteration is performed.

How do I decrypt encrypted data in MySQL?

The MySQL AES_DECRYPT function returns the original string after decrypting an encrypted string. It uses AES(Advanced Encryption Standard) algorithm to perform the decryption. The AES_DECRYPT function returns the decrypted string or NULL if it detects invalid data.


1 Answers

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?

like image 107
James Black Avatar answered Sep 20 '22 09:09

James Black