diff --git a/Blowfish.js b/Blowfish.js index a644fe23cfd22d03a387876236b3a3f2fb0b5d93..35eb8b49d8d20841c2e5cb5103fbbc6493dc3afe 100644 --- a/Blowfish.js +++ b/Blowfish.js @@ -177,11 +177,9 @@ Blowfish.prototype = { for (var i = 0; i < blocks; i++) { var block = string.substr(i * 8, 8); if (block.length < 8) { - // Pad with hex 0x80 followed by zeroes - var count = 8 - block.length - 1; - block += "\x80"; + var count = 8 - block.length; while (0 < count--) { - block += "0"; + block += "\0"; } } @@ -227,11 +225,9 @@ Blowfish.prototype = { for (var i = 0; i < blocks; i++) { var block = string.substr(i * 8, 8); if (block.length < 8) { - // Pad with hex 0x80 followed by zeroes - var count = 8 - block.length - 1; - block += "\x80"; + var count = 8 - block.length; while (0 < count--) { - block += "0"; + block += "\0"; } } @@ -281,14 +277,7 @@ Blowfish.prototype = { xL = xLxR[0]; xR = xLxR[1]; - var decryptedBlock; - decryptedBlock = this.num2block32(xL) + this.num2block32(xR); - // if this is the last block, strip off all trailing zero char and the 0x80 byte. - if (i === blocks - 1) { - decryptedBlock = decryptedBlock.replace(/(\x80)(0*)$/g, ""); - } - decryptedString += decryptedBlock; - + decryptedString += this.num2block32(xL) + this.num2block32(xR); } decryptedString = this.utf8Encode(decryptedString); @@ -336,17 +325,9 @@ Blowfish.prototype = { ivL = ivLtmp; ivR = ivRtmp; - - var decryptedBlock; - decryptedBlock = this.num2block32(xL) + this.num2block32(xR); - // if this is the last block, strip off all trailing zero char and the 0x80 byte. - if (i === blocks - 1) { - decryptedBlock = decryptedBlock.replace(/(\x80)(0*)$/g, ""); - } - decryptedString += decryptedBlock; - + decryptedString += this.num2block32(xL) + this.num2block32(xR); } - + decryptedString = this.utf8Encode(decryptedString); return decryptedString; }, @@ -728,9 +709,6 @@ Blowfish.prototype = { /** * Удаляет символы \0 в конце строки - * It is no more required encripting new data because now the blocks are padded - * with 0x80 followed by zero bytes (OneAndZeroes Padding) (See http://www.di-mgt.com.au/cryptopad.html) - * The function is necesary to decript data encrypted before 2015-03-07 * @param {string} input * @return {string} */ diff --git a/README.md b/README.md index 751a9297732bc7a06ff60050eb3c11529b4ee3ec..f2e2b4130565a356dffcfbd8db98748b08cc24f3 100755 --- a/README.md +++ b/README.md @@ -5,21 +5,14 @@ Blowfish encryption library Javascript, jquery,coffeescript (blowfish.js) Blowfish is block cipher, block length is **8 byte**. -Online [DEMO of javascript-blowfish](http://plnkr.co/edit/wqo56T). +Online [DEMO of javascript-blowfish](http://plnkr.co/edit/CbOyJKaRcspIAokgiomT?p=preview). A key advantage of the library is that it **works correctly with strings in UTF-8**. -### _trimZeros()_ method no more required for new encrypted data - -The padding method has been changed. Now the algorithm pad with 0x80 followed by zero bytes (OneAndZeroes Padding). -The padding data is automatically stripped off when decrypting so you "do not" need to use _trimZeros()_ on decrypted data. -Use _trimZeros()_ only to decrypt previously encrypted data before this change. - - ### Text data encryption (ASCII/text) It you want to encrypt **string information** (like text-message, or json, xml): -use _trimZeros_ method (see bellow Example 1). +use _trimZeroes_ method (see bellow Example 1). #### Example: ECB mode, default @@ -27,7 +20,7 @@ use _trimZeros_ method (see bellow Example 1). var bf = new Blowfish("secret key"); var encrypted = bf.encrypt("secret message"); var decrypted = bf.decrypt(encrypted); -decrypted = bf.trimZeros(decrypted); // for string/text information +decrypted = bf.trimZeroes(decrypted); // for string/text information console.log(decrypted); ``` diff --git a/code-review.txt b/code-review.txt index bb05a7cf623dd148264eef3a014a3ba9e43d48cd..0dd2d423da05b3949eb85e4923091148e8a5b925 100644 --- a/code-review.txt +++ b/code-review.txt @@ -1,9 +1,3 @@ -Padding method changed 2015-03-07 (@lucnap) - Coffee script not changed, only Blowfish.js and jquery.blowfish.js (do not know coffeescript) -+ pad with 0x80 followed by zero bytes (OneAndZeroes Padding) See http://www.di-mgt.com.au/cryptopad.html -- trimZeros function not more needed but not removed because useful in backward compatibility with - old encrypted data - Форматирование + большинство кода соответсвует - лучше использовать упрощенные конструкции условий с выходом (см. encrypt и decrypt) diff --git a/demo/index.html b/demo/index.html deleted file mode 100644 index 887e14ea9708112cceb9b6d7ec086a4f218273f7..0000000000000000000000000000000000000000 --- a/demo/index.html +++ /dev/null @@ -1,77 +0,0 @@ -<html> -<head> - <title>blowfish demo</title> - <meta http-equiv="content-type" content="text/html; charset=utf-8" /> - - <script type="text/javascript" src="../Blowfish.js"></script> - <script type="text/javascript" src="http://yandex.st/jquery/1.4.2/jquery.min.js"></script> - -</head> -<body> - <h1>Blowfish encryption/ decryption DEMO</h1> - <p>based on <a href="https://github.com/agorlov/javascript-blowfish">javascript-blowfish libriary</a></p> - <form> - <p>Your secret message (for encrypt) OR encrypted message (for decrypt):</p> - <textarea name="text" rows="12" cols="70"></textarea><br/> - <input type="submit" name="encrypt" value="Encrypt"> - - <p>Password:</p> - <input type="text" name="password" size="56" value="your secret key" /><br/> - - <p>Encrypted message</p> - <input type="submit" name="decrypt" value="Decrypt"><br/> - <textarea name="result" rows="12" cols="70"></textarea> - </form> - - <script type="text/javascript"> - $(function() { - $('input[name=encrypt]').submit(function(event) { - event.preventDefault(); - }); - - $('input[name=encrypt]').click(function(event) { - try { - var key = $('input[name=password]').val(); - var text = $('textarea[name=text]').val(); - - var bf = new Blowfish(key); - - var res = bf.encrypt(text); - - res = bf.base64Encode(res); - $('textarea[name=result]').val(res); - } catch(ex) { - if (window.console && console.log) { - console.log(ex) - } - } - - return false; - }); - - $('input[name=decrypt]').click(function() { - try { - var key = $('input[name=password]').val(); - var text = $('textarea[name=result]').val(); - - var bf = new Blowfish(key); - - text = bf.base64Decode(text); - - var res = bf.decrypt(text); - //res = bf.trimZeros(res); - - $('textarea[name=text]').val(res); - } catch(ex) { - if (window.console && console.log) { - console.log(ex) - } - } - - return false; - }); - }); - </script> - -</body> -</html> \ No newline at end of file diff --git a/jquery.blowfish.js b/jquery.blowfish.js index 75041517219372c280783f27a9d772c5643f2635..bd937d1efa7c6f69c5b706c7d47dfe2e4f1a20f5 100644 --- a/jquery.blowfish.js +++ b/jquery.blowfish.js @@ -179,12 +179,10 @@ for (var i = 0; i < blocks; i++) { var block = string.substr(i * 8, 8); if (block.length < 8) { - // Pad with hex 0x80 followed by zeroes - var count = 8 - block.length - 1; - block += "\x80"; - while (0 < count--) { - block += "0"; - } + var count = 8 - block.length; + while (0 < count--) { + block += "\0"; + } } var xL, xR; @@ -229,12 +227,10 @@ for (var i = 0; i < blocks; i++) { var block = string.substr(i * 8, 8); if (block.length < 8) { - // Pad with hex 0x80 followed by zeroes - var count = 8 - block.length - 1; - block += "\x80"; - while (0 < count--) { - block += "0"; - } + var count = 8 - block.length; + while (0 < count--) { + block += "\0"; + } } var xL, xR, xLxR; @@ -283,14 +279,7 @@ xL = xLxR[0]; xR = xLxR[1]; - var decryptedBlock; - decryptedBlock = this.num2block32(xL) + this.num2block32(xR); - // if this is the last block, strip off all trailing zero char and the 0x80 byte. - if (i === blocks - 1) { - decryptedBlock = decryptedBlock.replace(/(\x80)(0*)$/g, ""); - } - decryptedString += decryptedBlock; - + decryptedString += this.num2block32(xL) + this.num2block32(xR); } decryptedString = this.utf8Encode(decryptedString); @@ -338,16 +327,8 @@ ivL = ivLtmp; ivR = ivRtmp; - - var decryptedBlock; - decryptedBlock = this.num2block32(xL) + this.num2block32(xR); - // if this is the last block, strip off all trailing zero char and the 0x80 byte. - if (i === blocks - 1) { - decryptedBlock = decryptedBlock.replace(/(\x80)(0*)$/g, ""); - } - decryptedString += decryptedBlock; - - } + decryptedString += this.num2block32(xL) + this.num2block32(xR); + } decryptedString = this.utf8Encode(decryptedString); return decryptedString; @@ -728,14 +709,11 @@ return output; }, - /** - * Удаляет символы \0 в конце строки - * It is no more required encripting new data because now the blocks are padded - * with 0x80 followed by zero bytes (OneAndZeroes Padding) (See http://www.di-mgt.com.au/cryptopad.html) - * The function is necesary to decript data encrypted before 2015-03-07 - * @param {string} input - * @return {string} - */ + /** + * Удаляет символы \0 в конце строки + * @param {string} input + * @return {string} + */ trimZeros: function(input) { return input.replace(/\0+$/g, ""); }