Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP  AES Bit     - 256 Bit  Encryption

Posted on 2015-01-08
5
Medium Priority
?
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40538526
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
ID: 40538644
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
ID: 40538674
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 84

Expert Comment

by:Dave Baldwin
ID: 40538696
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
ID: 40546202
Figured it out myself..It seems to have worked!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

721 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