agorlov / javascript-blowfish

Blowfish encryption library Javascript, jquery,coffeescript (blowfish.js)
MIT License
71 stars 30 forks source link

Bug encrypting json data #1

Closed lucnap closed 9 years ago

lucnap commented 9 years ago

there is a bug when decrypt previous encrypted data in json format (stringified) It seems that some invisible character is added at the end...

here a plunker (watch console log) http://plnkr.co/edit/znEnkiURIjmv3DLeOgVP?p=preview

agorlov commented 9 years ago

You are right, there are zeroes added to the end of string.

Blowfish is a block cipher. It could encrypt or decrypt only blocks of information. Block size is 8 bytes.

Acceptable length of input string for Blowfish in bytes: 8,16, 24, 32 and so long.

If your messeage size is not 8 byte it is automaticaly padded by adding zeroes to the end. https://github.com/agorlov/javascript-blowfish/blob/master/Blowfish.js#L182

Example: string "secret" is 6 bytes.

To encrypt this, it is padded by zeroes: "secret\0\0" 8byte

I was doubt about shuld I trim zeroes when decrypt, or not. So I added function trimZeros to Blowfish class.

I was doubt, becouse blowfish could encrytp not only string information, but also binary.

There is no doubt I need to say about this in README.

Working example: http://plnkr.co/edit/I3y3AxgGJGFlJ89w060C?p=preview

lucnap commented 9 years ago

Ok I understand. But instead of padding with zeroes, what you think about padding with the Method 2 suggested in this page? http://www.di-mgt.com.au/cryptopad.html

agorlov commented 9 years ago

Good article, shows us different approaches of padding data for block ciphers. I will be appreciated you for pull request.