1

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

1 Answer 1

1

Looking at your PHP code you seem to be using 128 bit Rijndael in CBC mode with an explicit IV and unspecified padding. Better to explicitly specify the padding, usually PKCS5 or PKCS7. Given that your error message is talking about one of your blocks not being 16 bytes long then I would be inclined to first suspect a problem with the padding. The last block of your plaintext may not have been padded to the next block boundary so it will be short of the required 16 bytes, hence the error message.

Apart from that you need to ensure that you specify CBC mode in your Ruby code, so it matches the PHP code, and that the cyphertext, key and IV are identical at the byte level on both systems. Converting to and from strings can lead to differences and hence to problems. This is my second thought about your block size problem. If your conversion of the cyphertext from bytes to string and back to bytes again is changing the length of the cyphertext, then you will get the same error message.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for the answer. This Ruby library (crypt) has only one mode and that's CBC. I tried using a function from the CBC module called decrypt_string() instead of decrypt_block() and that seems to have got rid of the error. The decrypted string however is still not what I expected. I will look try to change according to your suggestions. Appreciate your help. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.