used utf-16 string as input for aes crypto and cleaned up code

This commit is contained in:
Tankred Hase 2013-05-14 18:38:24 +02:00
parent c264dfc994
commit d2e59c7f32
1 changed files with 16 additions and 20 deletions

View File

@ -4,25 +4,24 @@
app.crypto.AesCBC = function(forge) { app.crypto.AesCBC = function(forge) {
'use strict'; 'use strict';
var utl = forge.util;
/** /**
* Encrypt a String using AES-CBC-Pkcs7 using the provided keysize (e.g. 128, 256) * Encrypt a String using AES-CBC-Pkcs7 using the provided keysize (e.g. 128, 256)
* @param plaintext [String] The input string in UTF8 * @param plaintext [String] The input string in UTF-16
* @param key [String] The base64 encoded key * @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV * @param iv [String] The base64 encoded IV
* @return [String] The base64 encoded ciphertext * @return [String] The base64 encoded ciphertext
*/ */
this.encrypt = function(plaintext, key, iv) { this.encrypt = function(plaintext, key, iv) {
// parse base64 input to utf8 // decode args to utf8 and encrypt
var keyUtf8 = forge.util.decode64(key); var cipher = forge.aes.createEncryptionCipher(utl.decode64(key));
var ivUtf8 = forge.util.decode64(iv); cipher.start(utl.decode64(iv));
cipher.update(utl.createBuffer(utl.encodeUtf8(plaintext)));
// encrypt
var cipher = forge.aes.createEncryptionCipher(keyUtf8);
cipher.start(ivUtf8);
cipher.update(forge.util.createBuffer(plaintext));
cipher.finish(); cipher.finish();
return forge.util.encode64(cipher.output.getBytes()); // encode to base64
return utl.encode64(cipher.output.getBytes());
}; };
/** /**
@ -30,20 +29,17 @@ app.crypto.AesCBC = function(forge) {
* @param ciphertext [String] The base64 encoded ciphertext * @param ciphertext [String] The base64 encoded ciphertext
* @param key [String] The base64 encoded key * @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV * @param iv [String] The base64 encoded IV
* @return [String] The decrypted plaintext in UTF8 * @return [String] The decrypted plaintext in UTF-16
*/ */
this.decrypt = function(ciphertext, key, iv) { this.decrypt = function(ciphertext, key, iv) {
// parse base64 input to utf8 // decode args input to utf8 decrypt
var ctUtf8 = forge.util.decode64(ciphertext); var cipher = forge.aes.createDecryptionCipher(utl.decode64(key));
var keyUtf8 = forge.util.decode64(key); cipher.start(utl.decode64(iv));
var ivUtf8 = forge.util.decode64(iv); cipher.update(utl.createBuffer(utl.decode64(ciphertext)));
var cipher = forge.aes.createDecryptionCipher(keyUtf8);
cipher.start(ivUtf8);
cipher.update(forge.util.createBuffer(ctUtf8));
cipher.finish(); cipher.finish();
return cipher.output.getBytes(); // decode to utf16
return utl.decodeUtf8(cipher.output.getBytes());
}; };
}; };