PHP Classes
elePHPant
Icontem

PHP Form Encryption: Encrypt and decrypt forms with AES and RSA

Recommend this page to a friend!
  Info   View files Example   Demos   Screenshots Screenshots   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (6)   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2016-09-10 (1 month ago) RSS 2.0 feedStarStarStarStar 79%Total: 610 This week: 10All time: 4,890 This week: 106Up
Version License PHP version Categories
cryptopost 1.1MIT/X Consortium ...5.4HTTP, PHP 5, Cryptography
Description Author

This package can encrypt and decrypt forms with AES by JavaScript and PHP.

It can create private and public RSA keys used for client/server AES key interchange.

The class can take a submitted form with values that were encrypted on the browser side using JavaScript, and can send encrypted results back to the browser.

Innovation Award
PHP Programming Innovation award nominee
September 2016
Nominee
Vote
When you need to submit security sensitive data to a Web site, you should always use encrypted connections. However, over time older SSL versions have been discovered to be vulnerable, to the point that SSL is not longer synonym of security.

Nowadays you should use at least TLS and we don't know until when the current TLS versions will be safe to use to protect the transmission of private data.

This class provides an additional layer of security by using JavaScript to encrypt data before transmitting form values using RSA public key, and then having the server to decrypt the data with a private key.

This way, eventual man in the middle attacks will not be able to decrypt the data because they don't know what is the private key that the server uses to decrypt it.

Manuel Lemos
Picture of Francisco del Aguila
  Performance   Level  
Name: Francisco del Aguila <contact>
Classes: 2 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 2x

Details
SSL encryption provides a secure mechanism to protect information over public networks, but 
it is not always available: A small business private network with Wi-Fi can expose sensitive 
information, for example. There are many situations where the technical infrastructure or 
economic resources do not allow the installation of secure communication protocols.

Some times the application level encryption may be sufficient, or can even complement the 
session and/or transport level security. PHP Form Encryption  offers a application level 
encryption solution to implement in  PHP and Javascript apps development.

How does it works:

1. Server receives a client request of a web page that contains a form.

2. Server generates a session RSA key pair, and send the public key included in the html 
response.

3. Client fills out the form and generate a AES-256 key that is returned to server encrypted 
with the received public key and the AES encrypted form data. Client saves this AES key using 
browser local storage.

4. Server receives the RSA-encrypted AES key and decrypt it using the RSA private key. Then 
this AES key will be used to decrypt the received form data and to encrypt/decrypt future forms 
until it's changed or session expires.

PHP Implementation:

Session must be started before using Cryptopost class. Then, let's intercept an encrypted form:

session_start(); 

require_once './Cryptopost.class.php'; 
$crypto = new Cryptopost(1024, './openssl.cnf');

if (isset($_POST['cryptoPost'])){
	$formId = $crypto->decodeForm();
}

So, now we know the id of the submited form and $_POST superglobal contains the decrypted data. 
Before that $_POST will only contain somethig like:

var_dump($_POST) result:
array(2) {
  ["cryptoPost_key"]=>
  string(256) "5df90b95ec4fab45d50d34c917c6578f939ccbfadf9486f133850d47a3d6b2c82a277a3468ca11fc7
  b9163c385eacc2a3a4d091cf8797e55d681b0279058a9f3e334092fb03791931d22ca3847f4f9d4dec0d0a47936f01
  2b6be9723981088d0b049cff46a8e81ec93e2b4f7c3a387d36e2033754d1420a8dc800a4eec6cd0e9"
  ["cryptoPost"]=>
  string(242) "U2FsdGVkX1/53Ut6KFi36Ou/e3lIJz/5pf8FuPb1Yh//WdefKb0iyCke2/g0QPD5
BeknGV4L8dveRDbQ4kXm5YNi3nyG+/F8JWKDipA9ygHPf5KdFr6pYcfzNQjwwfd8
rIC19cl9IOJcs171tm0OBVknaloQWDwpLM/KjISdwwPiRGCtcBhkYrcdsgv6JcwD
aVuU4VunXdWJji9WAKD+1bJrThq2VLjEHhELl26y4vI="
}
Note that "crytoPost_key" will be received only once at first time that server receives a coded 
form. Following posts will include only the "cryptoPost" value unless keys are reset.

To send the form encrypted to the server, simply include a call to the javascript crytopost class:

<form id="form1" method="POST" action="test.php" onsubmit="return cryptoPost.encrypt('form1')">

Client may need an encrypted record to edit. Server can send it in this way:

$record = array(
	"name" => $name,
	"address" => $address,
	"zipCode" => $zip
);
$encrypted = $crypto->encodeData($record, $formId);

... and then, at the bottom of the html code:

<script>cryptoPost.decrypt('<?php echo $encrypted;?>')</script>


That's a simple and easy way to protect your data even with no SSL.

PHP Form Encryption requires openssl extension and PHP 5.4+

Thanks to:

- Tom Wu, author of jsbn/RSA javascript library.
- Mark Percival, author of Gibberish-AES javascript library.
  PHP Form encryption testExternal page  
Screenshots  
  • screencap.jpg
  Files folder image Files  
File Role Description
Files folder imagejavascript (3 files)
Accessible without login Plain text file LICENSE.txt Lic. License
Accessible without login Plain text file openssl.cnf Conf. openssl config file
Accessible without login Plain text file test.php Example Test script
Accessible without login Plain text file changelog.txt Doc. Package history
Plain text file Cryptopost.class.php Class Main php class
Accessible without login Plain text file Readme.txt Doc. Documentation

  Files folder image Files  /  javascript  
File Role Description
  Plain text file cryptopost.js Class Javascript class
  Plain text file gibberish-aes.js Class Javascript AES
  Plain text file rsa_jsbn.js Class Javascript RSA

 Version Control Unique User Downloads Download Rankings  
 0%
Total:610
This week:10
All time:4,890
This week:106Up
User Ratings User Comments (3)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:95%StarStarStarStarStar
Examples:100%StarStarStarStarStarStar
Tests:-
Videos:-
Overall:79%StarStarStarStar
Rank:24
 
Thats a good class ;-)
1 month ago (José Filipe Lopes Santos)
80%StarStarStarStarStar
Thats a good class ;-)
1 month ago (José Filipe Lopes Santos)
80%StarStarStarStarStar
The first non-trivial package I've seen on this site.
1 month ago (Theodore M Rolle Jr)
80%StarStarStarStarStar