I'm fairly new encryption and decryption so please excuse my lack of knowledge in the subject.
I am trying to decrypt a string in Ruby. The string is read from a server where it was encrypted in PHP using MCRYPT_RIJNDAEL_128. I have the code to decrypt it in PHP which works and I'm trying to convert that to Ruby. I need this for authentication for the mobile application that I'm making using Rhodes (mobile application framework in Ruby).
Here is a snippet for the decryption at the PHP end.
$key = "This is a test key";
$string2 = hex2bin($string); // The encrypted string
$iv = '1111F321414LOJL018473914DSADAS'; // Just given a random Initialisation vector for the example
$encrypted = mcrypt_cbc(MCRYPT_RIJNDAEL_128, $key, $string2, MCRYPT_DECRYPT, $iv);
function hex2bin($str) {
$bin = "";
$i = 0;
do {
$bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
$i += 2;
} while ($i < strlen($str));
return $bin;
}
hex2bin is a function that converts hexadecimal to binary.
So far here is my failed attempt to do this decryption using crpt:rijndael library provided by Ruby.
key = "This is a test key"
rd = Crypt::Rijndael.new(key, 128, 128)
string = @params['body'] // The encrypted string
encrypted_string = hex2bin(string)
decrypted_string = rd.decrypt_block(encrypted_string)
def hex2bin(str)
bin = ""
i = 0
begin
bin = bin + ((str[i]+str[i+1]).hex).chr
i = i+2
end while i<str.length
return bin
end
I get an error saying that "App error: block must be 16 bytes long". I tried adding the code to make it accept the initialisation vector from http://pastebin.com/m1rsJUXM. I still get the same error. Any sort of help or direction would be greatly appreciated.
Regards,
Ash