mail/src/js/crypto/aes-cbc.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-04-02 09:02:57 -04:00
/**
* A Wrapper for Forge's AES-CBC encryption
2013-04-02 09:02:57 -04:00
*/
2013-05-16 10:54:56 -04:00
var AesCBC = function(forge) {
'use strict';
var utl = forge.util;
2013-03-13 11:58:46 -04:00
/**
2013-04-02 09:02:57 -04:00
* Encrypt a String using AES-CBC-Pkcs7 using the provided keysize (e.g. 128, 256)
* @param plaintext [String] The input string in UTF-16
2013-04-02 09:02:57 -04:00
* @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV
* @return [String] The base64 encoded ciphertext
2013-03-13 11:58:46 -04:00
*/
2013-04-02 09:02:57 -04:00
this.encrypt = function(plaintext, key, iv) {
// decode args to utf8 and encrypt
var cipher = forge.aes.createEncryptionCipher(utl.decode64(key));
cipher.start(utl.decode64(iv));
cipher.update(utl.createBuffer(utl.encodeUtf8(plaintext)));
2013-05-14 06:49:27 -04:00
cipher.finish();
// encode to base64
return utl.encode64(cipher.output.getBytes());
2013-04-02 09:02:57 -04:00
};
2013-04-02 09:02:57 -04:00
/**
* Decrypt a String using AES-CBC-Pkcs7 using the provided keysize (e.g. 128, 256)
* @param ciphertext [String] The base64 encoded ciphertext
* @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV
* @return [String] The decrypted plaintext in UTF-16
2013-04-02 09:02:57 -04:00
*/
this.decrypt = function(ciphertext, key, iv) {
// decode args input to utf8 decrypt
var cipher = forge.aes.createDecryptionCipher(utl.decode64(key));
cipher.start(utl.decode64(iv));
cipher.update(utl.createBuffer(utl.decode64(ciphertext)));
2013-05-14 06:49:27 -04:00
cipher.finish();
// decode to utf16
return utl.decodeUtf8(cipher.output.getBytes());
2013-05-14 06:49:27 -04:00
};
2013-05-16 10:54:56 -04:00
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = AesCBC;
} else {
app.crypto.AesCBC = AesCBC;
}