diff --git a/js/test.js b/js/test.js index 889ccf00b7..b5103fe22d 100644 --- a/js/test.js +++ b/js/test.js @@ -53,6 +53,18 @@ describe("Cryptographic primitives", function() { }); }); + describe("Encrypt AES-CBC", function() { + it('works', function(done) { + var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'); + var iv = hexToArrayBuffer('000102030405060708090a0b0c0d0e0f'); + var plaintext = hexToArrayBuffer('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710'); + var ciphertext = hexToArrayBuffer('f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644'); + return window.textsecure.subtle.encrypt({name: "AES-CBC", iv: iv}, key, plaintext).then(function(result) { + assert.strictEqual(getString(result), getString(ciphertext)); + }).then(done).catch(done); + }); + }); + describe("Decrypt AES-CBC", function() { it('works', function(done) { var key = hexToArrayBuffer('603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'); diff --git a/js/webcrypto.js b/js/webcrypto.js index 71d3ce23be..b64fadad23 100644 --- a/js/webcrypto.js +++ b/js/webcrypto.js @@ -62,10 +62,10 @@ window.textsecure.subtle = (function() { assertIsArrayBuffer(plaintext); assertIsArrayBuffer(key); assertIsArrayBuffer(iv); - return CryptoJS.AES.encrypt(btoa(getString(plaintext)), + return CryptoJS.AES.encrypt(CryptoJS.enc.Latin1.parse(getString(plaintext)), CryptoJS.enc.Latin1.parse(getString(key)), {iv: CryptoJS.enc.Latin1.parse(getString(iv))}) - .toString(CryptoJS.enc.Latin1); + .ciphertext.toString(CryptoJS.enc.Latin1); }; function decryptAESCBC(ciphertext, key, iv) { assertIsArrayBuffer(ciphertext);