enzis
02-16-2012, 02:30 PM
Hello. I've spent hours trying to implement rsa encryption and am getting tired of the annoying complexity and lack of WORKING examples out there. I'm asking for any kind of help.
Here's the goal (and I kinda think it should be simple and done before, since it seems like a normal goal):
I want to encrypt data sent via the client, then decrypt it on the server. I do not have a certificate so as to use https without warning messages, and learned of RSA encryption. The server should create a keypair, save the private key somewhere, and send the public key to the client. The client should use the public key to encrypt some data and send it back to the server. The server uses the private key to decrypt it, and all is good.
The Implementation:
To do something on the server it should be done via PHP. To do something on the client, it should be done via javascript. Thus, the keypair should be generated with php, while the message should be encrypted with javascript on the client, then sent to the browser to be decrypted with php.
The problem:
Apparently, javascript does NOT include a function that works with the php rsa functions (at least not easily). Nor is there a known library that accomplishes this. This is the part that really surprises me, since it seems like the whole purpose of rsa keypairs would be to work with javascript and php. If there is a better way to go about this, someone please enlighten me.
Some research:
This is someones javascript example to encode with some given public key and exponent
http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.html
Since the above link uses hex and base64, I found another site to help convert between formates
http://textmechanic.com/ASCII-Hex-Unicode-Base64-Converter.html
Here's a link to doing the RSA functions in php
http://php.net/manual/en/function.openssl-pkey-new.php
Here's a short and simple php implementation that I found somewhere
<?php
// Create the keypair
$config = array('private_key_bits' => 1024); // NOT REQUIRED. MAY COMMENT OUT
$res=openssl_pkey_new($config);
// Get private key
openssl_pkey_export($res, $privatekey);
// Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"];
// Sample sensitive data
$cleartext = '1234 5678 9012 3456';
// Display private key
echo "Private Key:<BR>$privatekey<br><br>Public Key:<BR>$publickey<BR><BR>";
// Display sensitive data
echo "Clear text:<br>$cleartext<BR><BR>";
// Display encrypted version of sensitive data
echo "Crypt text:<br>$crypttext<BR><BR>";
// Decrypt the data
openssl_private_decrypt($crypttext, $decrypted, $privatekey);
// Display decrypted form of the sensitive data
echo "<br />Decrypted text:<BR>$decrypted<br><br>";
?>
The above code works, but ONLY if the entire process stays in php. I've tried many methods to convert between here and there to get the php to work with the javascript demo, but with no long. Other javascript I've seen seem far too complex and don't have a demo I can easily test with.
Other research:
In case anyone wants the default values for php's openssl rsa functions (since they do take some hunting to find):
Exponent: 65537 (hex: 0x10001)
Bit length: 1024
Thanks in advance for any help
Here's the goal (and I kinda think it should be simple and done before, since it seems like a normal goal):
I want to encrypt data sent via the client, then decrypt it on the server. I do not have a certificate so as to use https without warning messages, and learned of RSA encryption. The server should create a keypair, save the private key somewhere, and send the public key to the client. The client should use the public key to encrypt some data and send it back to the server. The server uses the private key to decrypt it, and all is good.
The Implementation:
To do something on the server it should be done via PHP. To do something on the client, it should be done via javascript. Thus, the keypair should be generated with php, while the message should be encrypted with javascript on the client, then sent to the browser to be decrypted with php.
The problem:
Apparently, javascript does NOT include a function that works with the php rsa functions (at least not easily). Nor is there a known library that accomplishes this. This is the part that really surprises me, since it seems like the whole purpose of rsa keypairs would be to work with javascript and php. If there is a better way to go about this, someone please enlighten me.
Some research:
This is someones javascript example to encode with some given public key and exponent
http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.html
Since the above link uses hex and base64, I found another site to help convert between formates
http://textmechanic.com/ASCII-Hex-Unicode-Base64-Converter.html
Here's a link to doing the RSA functions in php
http://php.net/manual/en/function.openssl-pkey-new.php
Here's a short and simple php implementation that I found somewhere
<?php
// Create the keypair
$config = array('private_key_bits' => 1024); // NOT REQUIRED. MAY COMMENT OUT
$res=openssl_pkey_new($config);
// Get private key
openssl_pkey_export($res, $privatekey);
// Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"];
// Sample sensitive data
$cleartext = '1234 5678 9012 3456';
// Display private key
echo "Private Key:<BR>$privatekey<br><br>Public Key:<BR>$publickey<BR><BR>";
// Display sensitive data
echo "Clear text:<br>$cleartext<BR><BR>";
// Display encrypted version of sensitive data
echo "Crypt text:<br>$crypttext<BR><BR>";
// Decrypt the data
openssl_private_decrypt($crypttext, $decrypted, $privatekey);
// Display decrypted form of the sensitive data
echo "<br />Decrypted text:<BR>$decrypted<br><br>";
?>
The above code works, but ONLY if the entire process stays in php. I've tried many methods to convert between here and there to get the php to work with the javascript demo, but with no long. Other javascript I've seen seem far too complex and don't have a demo I can easily test with.
Other research:
In case anyone wants the default values for php's openssl rsa functions (since they do take some hunting to find):
Exponent: 65537 (hex: 0x10001)
Bit length: 1024
Thanks in advance for any help