Solved

PHP  AES Bit     - 256 Bit  Encryption

Posted on 2015-01-08
5
479 Views
Last Modified: 2015-01-13
Hello I am trying to use PHP to encrypt a string using AES 256 but I can not seem to get the correct out put..The information below shows what I am trying to encrypt and the correct output..Any help would be greatly appreciated.


Algorithm Details
 
Name - AES
Bit     - 256 Bit  
Key  - bvXB4fMlk5hjkacfg28GTdr270FGHgdc
IV -  jTfQrHFg4esdrtyRE8gh45rtyFG8tm1e
 
 
use the following simple text for testing:
 
plain text(input):   Hodanglobal
 
Encrypted text (output): 0hVNjVnRrmFS10x647nA/WAkWSuPOpbUgewMIJnBd+I=
0
Comment
Question by:bradley525
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Please show us the code you are trying to use.  Note that there are differences in the way things are handled by Microsoft .NET code and PHP.
0
 

Author Comment

by:bradley525
Comment Utility
I am using the following code. And it is rendering the following errors...Thanks!

Before encryption: Hodanglobal
Warning: openssl_encrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in C:\Inetpub\vhosts\hodanfinancial.com\httpdocs\encrypt.php on line 75
Encrypted: 2LeVFWKbJQPUkO0gSHNH6Q==
Warning: openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in C:\Inetpub\vhosts\hodanfinancial.com\httpdocs\encrypt.php on line 88
Decrypted: Hodanglobal

// DEFINE our cipher
define('AES_256_CBC', 'aes-256-cbc');

// Generate a 256-bit encryption key
// This should be stored somewhere instead of recreating it each time
//$encryption_key = openssl_random_pseudo_bytes(32);
$encryption_key =  'bvXB4fMlk5hjkacfg28GTdr270FGHgdc';
// Generate an initialization vector
// This *MUST* be available for decryption as well
//$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));
$iv = 'jTfQrHFg4esdrtyRE8gh45rtyFG8tm1e';
// Create some data to encrypt
$data = "Hodanglobal";
echo "Before encryption: $data\n";

// Encrypt $data using aes-256-cbc cipher with the given encryption key and 
// our initialization vector. The 0 gives us the default options, but can
// be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
$encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
echo "Encrypted: $encrypted\n";

// If we lose the $iv variable, we can't decrypt this, so append it to the 
// encrypted data with a separator that we know won't exist in base64-encoded 
// data
$encrypted = $encrypted . ':' . $iv;

// To decrypt, separate the encrypted data from the initialization vector ($iv)
$parts = explode(':', $encrypted);
// $parts[0] = encrypted data
// $parts[1] = initialization vector

$decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, $parts[1]);
echo "Decrypted: $decrypted\n";

Open in new window

0
 

Accepted Solution

by:
bradley525 earned 0 total points
Comment Utility
Got it to work...


ini_set('display_errors', 1);
error_reporting(E_ALL);

// I blantantly stole, tweaked and happily used this code from: 
// Lord of Ports http://www.experts-exchange.com/M_1736399.html

/*
$ky = 'lkirwf897+22#bbtrm8814z5qq=498j5'; // 32 * 8 = 256 bit key
$iv = '741952hheeyy66#cs!9hjv887mxx7@8y'; // 32 * 8 = 256 bit iv
*/
$ky = 'bvXB4fMlk5hjkacfg28GTdr270FGHgdc';
$iv = 'jTfQrHFg4esdrtyRE8gh45rtyFG8tm1e';

$text = "Hodanglobal";

$from_vb = "0hVNjVnRrmFS10x647nA/WAkWSuPOpbUgewMIJnBd+I=";   // enter value from vb.net app here to test

$etext = encryptRJ256($ky, $iv, $text);
$dtext = decryptRJ256($ky, $iv, $etext);
$vtext = decryptRJ256($ky, $iv, $from_vb);

echo "<HR>orignal string: $text";
echo "<HR>encrypted in php: $etext";
echo "<HR>decrypted in php: $dtext";
echo "<HR>encrypted in vb: $from_vb";
echo "<HR>from vb decrypted in php: $vtext"; 



exit;



function decryptRJ256($key,$iv,$string_to_decrypt)
{

    $string_to_decrypt = base64_decode($string_to_decrypt);

    $rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);

    $rtn = rtrim($rtn, "\0\4");

    return($rtn);

}


function encryptRJ256($key,$iv,$string_to_encrypt)
{

    $rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);

    $rtn = base64_encode($rtn);

    return($rtn);

}    

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
The functions are almost undocumented.  http://php.net/manual/en/function.openssl-encrypt.php  I suggest you believe their error messages and use a 16 byte key for $iv.
0
 

Author Closing Comment

by:bradley525
Comment Utility
Figured it out myself..It seems to have worked!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Cybersecurity has become the buzzword of recent years and years to come. The inventions of cloud infrastructure and the Internet of Things has made us question our online safety. Let us explore how cloud- enabled cybersecurity can help us with our b…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now