John Davidson

encryption - Decrypt AES-128 CBC - PHP

0 comments
Message:


I have an encrypted message in AES-128 CBC in this format "f21fcc6677c9ba2335da551fa143cf08" and I'm trying to decrypt using a key in this format "cdff8db86efb418a8e492a29dba44869", I'm new to this and I can't seem to make it work, I tried this code with no success.


<?php

$simple_string = base64_decode('f21fcc6677c9ba2335da551fa143cf08');
echo $simple_string. "\n";
//echo $simple_string;


$ciphering = 'AES-128-CBC';

$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;


$encryption_key = 'cdff8db86efb418a8e492a29dba44869';
echo $encryption_key. "\n";

$decryption = openssl_decrypt($simple_string, $ciphering, $encryption_key, OPENSSL_RAW_DATA);

var_dump($decryption);

my output is


bool(false)

any ideas? I think I have to change the format of the encrypted message or the key, but don't find much in Google.



My Answer:
Here is the corrected code to decrypt the AES-128 CBC encrypted message:

php

$encrypted_message = hex2bin('f21fcc6677c9ba2335da551fa143cf08');
echo $encrypted_message . "\n";

$ciphering = 'AES-128-CBC';

$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;

$encryption_key = hex2bin('cdff8db86efb418a8e492a29dba44869');
echo $encryption_key . "\n";

$decryption = openssl_decrypt($encrypted_message, $ciphering, $encryption_key, $options);

var_dump($decryption);


In this code snippet, I have made the following changes:
1. Converted the encrypted message and encryption key from hexadecimal format to binary format using `hex2bin()` function.
2. Used the binary format of the encrypted message and encryption key in the `openssl_decrypt()` function.

This should now successfully decrypt the AES-128 CBC encrypted message.

Rate this post

5 of 5 based on 1677 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us