Important : We’ve just launched UserEncrypted.com , a free client side encryption service to securely store passwords or notes. The peculiarirty of the system is that your clear data never leaves your browser, and only encrypted data is sent to our server.
I recently happened to work on a web application whose commissioner was a society hosting sensitive data for its customers. Data needed to be accessible only by the respective owner and noone else at any level, database included. The platform was LAMPJ (Linux, Apache,Mysql,PHP and some Java webservices accessed by the PHP layer), and password protected AES encryption was required by the commissioner. Proposals
Database encryption was discarded immediately because the approach would have meant that the PHP level had access to unencrypted data, so only client encryption being left, a second choice needed be done between:
Although I’m quite accustomed to encryption, digital signature and timestamping with Java, I (initially) decided, due to strong encryption export issues, to give a try to the Javascript approach. Obviously I wasn’t going to implement aes in javascript by myself, and found the right code at Movable Type Scripts, where you can find the complete source code.
And here is the encrypt.php code: 1:<?php
2:
3:
4: $filePath=$_SERVER['DOCUMENT_ROOT']."/demo/encryption/encryption.txt";
5: if(isset($_POST['encrypted_data'])){
6: file_put_contents($filePath,$_POST['encrypted_data']);
7: }
8: $text=(file_exists($filePath)) ? file_get_contents($filePath): "";
9:?>
10:<html>
11:<script src="base64.js"></script>
12:<script src="encryption.js"></script>
13:<body onload="getPassword();decryptData();">
14: <form method=POST name=myform>
15: <input type=hidden name=encrypted_data id=encrypted_data value="<?php echo $text;?>">
16: </form>
17: <textarea id=datafield name=datafield rows=20 cols=50></textarea>
18: <input type=button onclick="return encryptData();" value="submit" >
19:
20:
21:<script language=javascript>
22: var hiddenfield=document.getElementById("encrypted_data");
23: var textarea=document.getElementById("datafield");
24: var password="";
25: function decryptData(){
26: if(hiddenfield.value !=''){
27: textarea.value=AESDecryptCtr(decode64(hiddenfield.value), password ,128);
28: }
29: }
30:
31: function encryptData(){
32: hiddenfield.value=encode64(AESEncryptCtr(textarea.value, password ,128));
33: document.forms.myform.submit();
34: }
35:
36: function getPassword(){
37: password=prompt("Password");
38: }
39:
40:</script>
41:</body>
42:</html>
First the php part: Now the html/javascript part. As we can see on line 13, when the page is loaded (event onload) we first call the getPassword function, which prompts for a password, then we call decryptData. This will take the value of the hidden field, base64 decode it and , using the provided password, try to decrypt the data and put it in our textarea. The third parameter in AESDecryptCtr is the number of bits of the encryption key (128,192 and 256 are supported). Easy, does it? Just a couple of notes: That’s all folks. P.S.: in the end, for my project, the commissioner asked for PKI and smart card certificate encryption, so I had to implement a Java applet using BouncyCastle’s library and IAIK’s pkcs11 layer. So now clients can encrypt with the public keys of all those they want to be able to decrypt their data….. |