javascript - JSEncrypt producing different output for identical message and public key each time -
i've got simple bit of javascript:
<html> <script type="text/javascript" src="http://cdn.rawgit.com/travist/jsencrypt/v2.1.0/bin/jsencrypt.js"></script> <script type="text/javascript"> var message = "this message" // create new jsencrypt object rsa encryption var rsaencrypt = new jsencrypt(); var publickey = "-----begin public key-----" + "\nmigfma0gcsqgsib3dqebaquaa4gnadcbiqk" + "bgqcqdxdfoyjpkcaqex4cbvnctbjx\nmzggmo" + "lss2wyrvu1ixr36kpejcrcyhz+anyhnupykgk" + "+heqjtdsniru5dk5ovyyi\ngf2ulogv5tp/ka" + "k49r9vtxldchrazignqjcdequd/vikd/3dvm8" + "nawr/mtg0ncbe\nreqkatw/xxynjh/coqidaq" + "ab\n-----end public key-----" rsaencrypt.setpublickey(publickey); var encryptedmessage = rsaencrypt.encrypt(message); console.log(encryptedmessage) </script> </html>
that feel should producing same output in console each time runs, doesn't.
example outputs:
abqe+ykcmkfwgsazbzpfgvoxlci9fl/wzlyumr6zfkolsvjc5mdjgq5yn+axxy8xlkhdory6czaaoqotl2hxdksfsypc8nqdu8sx5puegmyjvj/deyfu6jvuxfh1qmuk6aogvhepnfdlc4ksjgp1rxtosp5nqaei24eumx3uuli= ozzm03pki3o631kouz5nyqku1xxrblhhrr0wnje5ns5sssoicewlrs+svtp0cbzaywzjc+flzqnfuam4ic233bkny5nrr5ppj14eabvj4x3fr8filwtyew7ntzisas7ys2rkpjuzmkioczhwipxnuo10kvo8763+jiudb0cdps4=
can explain behaviour?
that expected.
the rsa cryptosystem works in group modulo prime number. ensure possible plaintexts encrypted same security, plaintexts padded produce padded plaintext smaller modulus. since padding applied before encryption, ciphertext looks different.
jsencrypt based on jsbn in turn implements pkcs#1 v1.5 padding type 2 (rfc 2313). second type of padding introduces random bytes removed after decryption because of marker bytes. @ least 11 bytes needed padding.
if want check interoperability other implementations, need full encryption-decryption cycle , make sure same plaintext back.
note nowadays, pkcs#1 v1.5 padding shouldn't used anymore , pkcs#1 v.2 oaep preferred randomized.
Comments
Post a Comment