LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2018, 05:30 PM   #1
goingbackwards
LQ Newbie
 
Registered: Jul 2015
Posts: 16

Rep: Reputation: Disabled
PHP: OpenSSL issue


Hi, can anyone see why the decryption returns an empty string?

Code:
<?php
$sx_key = "95719d373a36051b273f39f2a1c4cedbd0cfeee354ece178ecf731a9771fe1fe";
$sx_iv = "b47c0457b2973ca931559e6fcf0cca66";

$msg = "Now is the time for all good men to come to the aide of their country";

//	ENCRYPT
$ss = f_encrypt($msg, $sx_key, $sx_iv);
echo "c_msg::".$ss."\n";

//	DECRYPT
$s = f_decrypt($ss, $sx_key, $sx_iv);
echo "msg::".$s."\n";


function f_encrypt($msg, $sx_key, $sx_iv)
{
	$cipher="AES-256-CBC";
	
	$key = Hex2String($sx_key);
	$iv = Hex2String($sx_iv);
	
	$ciphertext_raw = openssl_encrypt($msg, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
	
	return String2Hex($ciphertext_raw);
}


function f_decrypt($msg, $sx_key, $sx_iv)
{
	$cipher="AES-256-CBC";
	
	$key = Hex2String($sx_key);
	$iv = Hex2String($sx_iv);
	
	$ciphertext_raw = Hex2String($msg);		//	hex2bin
	//echo $ciphertext_raw."\n";
	
	$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
	
	return $original_plaintext;
}

//	http://www.jonasjohn.de/snippets/php/hex-string.htm
function String2Hex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}
function Hex2String($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
?>
 
Old 01-11-2018, 06:30 PM   #2
goingbackwards
LQ Newbie
 
Registered: Jul 2015
Posts: 16

Original Poster
Rep: Reputation: Disabled
Was a hex padding issue...

Code:
function String2Hex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= zeropad(dechex(ord($string[$i])), 2);
    }
    return $hex;
}

function zeropad($num, $lim)
{
   return (strlen($num) >= $lim) ? $num : zeropad("0" .$num, $lim);
}
 
Old 01-11-2018, 09:24 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,666
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
Thanks for returning to post the solution! If this issue is now resolved please use the Thread Tools options at top to mark it solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php-openssl RPM ravi_nandula Linux - Newbie 2 12-11-2013 09:40 PM
PHP with OpenSSL Support Failing pinstripe Linux - Server 4 12-20-2009 08:17 AM
openssl issue??? birjodh Linux - Networking 6 06-07-2007 10:47 AM
compile php with openssl problem rmang Linux - Software 1 01-23-2004 09:14 AM
PHP + OpenSSL Miky28 Linux - Newbie 0 07-29-2003 06:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration