Chad C. Davis
Chad C. Davis

Reputation: 112

Rijndael 256 on PHP encryption difficulties

I have to pass a value and have it encrypted to the following spec:

"256-bit key length, 256-bit block length, 32-byte blocks, ECB mode, with ASCII encoding (encrypted data is expected to be provided as a string with each character having been converted to a 2-byte HEX value)"

However I must be missing something. This is for verification purposes with a webservice but I keep getting rejected due to a decryption failure.

Here's what I have:

$key = '1324mykey';
$string = 'Ron Swanson';

// 1. Encrypt the string with the key using Rijndael 256 in ECB mode
$td = mcrypt_module_open('rijndael-256', '','ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_string = mcrypt_generic($td, $string);

// 2. Base64 encode my string
$encrypted_string = base64_encode($encrypted_string);

// 3. Convert each character of encrypted_string to it's 2-byte HEX value 
$hex='';
for ($i=0; $i < strlen($encrypted_string); $i++)
{
    $hex .= dechex(ord($encrypted_string[$i]));
}

// Now $encrypted_string should match up with the recipe, but it isn't. 
$encrypted_string = $hex;

I'm hoping there's something basic in the encryption requirements that I'm missing in my step 1 of my process.

Upvotes: 1

Views: 1723

Answers (2)

Chad C. Davis
Chad C. Davis

Reputation: 112

This has been solved... (un?)fortunately I was simply given a wrong parameter.

Upvotes: 1

aqua
aqua

Reputation: 3375

If your requirement is a 256-bit key, shouldn't your $key variable be 32 bytes? (in other words, 32 characters long) ?

Upvotes: 2

Related Questions