Encrypt in Javascript, decrypt in PHP, using public-key cryptography -
i'd encrypt in javascript, decrypt in php, using public-key cryptography. i've been trying find libraries can accomplish this, having issues.
i looking @ openpgpjs, need support in browsers, , test page has errrors on listed supported browser (google chrome).
notes final goal:
the tcp connection protected ssl. main purpose of layer of protection defending against intentional or unintentional webserver logging, crash dumps, etc.
on php side, temporary private key generated (it expire after short time). caller (in javascript) responsible asking new public key when expires. reason private key expiration prevent logged encrypted data decryption, in case server stores private key later compromised.
servers compromised scenario: gets hands on backups machines except database server (and cannot access database due firewalling, if finds out user , password). since private key encrypted logged data no longer exists, there nothing attacker can do.
i've used similar login page; encrypts login credentials using given public key information (n, e) can decrypted in php.
it uses following files part of jsbn
:
jsbn.js
- work big integersrsa.js
- rsa encryption (uses jsbn.js)rng.js
- basic entropy collectorprng4.js
- arc4 rng backend
to encrypt data:
$pk = '-----begin rsa private key----- ... -----end rsa private key-----'; $kh = openssl_pkey_get_private($pk); $details = openssl_pkey_get_details($kh); function to_hex($data) { return strtoupper(bin2hex($data)); } ?> <script> var rsa = new rsakey(); rsa.setpublic('<?php echo to_hex($details['rsa']['n']) ?>', '<?php echo to_hex($details['rsa']['e']) ?>'); // encrypt using rsa var data = rsa.encrypt('hello world'); </script>
this how decode sent data:
$kh = openssl_pkey_get_private($pk); $details = openssl_pkey_get_details($kh); // convert data hexadecimal notation $data = pack('h*', $data); if (openssl_private_decrypt($data, $r, $kh)) { echo $r; }
Comments
Post a Comment