Fix encoding issue with decrypted string in crypto wrapper

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2013-11-29 16:43:28 -05:00
parent 0ccc9e4dc2
commit 05b4262cde
1 changed files with 11 additions and 51 deletions

View File

@ -8,6 +8,7 @@ define(function(require) {
// Rabbit, see http://code.google.com/p/crypto-js/#Rabbi // Rabbit, see http://code.google.com/p/crypto-js/#Rabbi
require("crypto-js/rollups/rabbit"); require("crypto-js/rollups/rabbit");
function CryptoWrappedContext(context, encrypt, decrypt) { function CryptoWrappedContext(context, encrypt, decrypt) {
this.context = context; this.context = context;
this.encrypt = encrypt; this.encrypt = encrypt;
@ -37,51 +38,6 @@ define(function(require) {
this.context.delete(key, callback); this.context.delete(key, callback);
}; };
// Custom formatting for encryption objects <-> strings
var formatter = {
stringify: function(cipherParams) {
// create json object with ciphertext
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
// cache iv and salt
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
// stringify json object
return JSON.stringify(jsonObj);
},
parse: function(jsonString) {
// parse json string
var jsonObj;
try {
jsonObj = JSON.parse(jsonString);
} catch(e) {
throw e;
}
// extract ciphertext from json object, and create cipher params object
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
// extract iv and salt
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv);
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s);
}
return cipherParams;
}
};
function buildCryptoWrapper(encryptionType) { function buildCryptoWrapper(encryptionType) {
// It is up to the app using this wrapper how the passphrase is acquired, probably by // It is up to the app using this wrapper how the passphrase is acquired, probably by
@ -89,12 +45,12 @@ define(function(require) {
function CryptoWrappedProvider(passphrase, provider) { function CryptoWrappedProvider(passphrase, provider) {
this.provider = provider; this.provider = provider;
this.encrypt = function(plain) { this.encrypt = function(plain) {
console.log('encrypt', plain, CryptoJS[encryptionType].encrypt(plain, passphrase).toString()); return CryptoJS[encryptionType].encrypt(plain, passphrase)
return CryptoJS[encryptionType].encrypt(plain, passphrase, {format: formatter}).toString(); .toString();
}; };
this.decrypt = function(encrypted) { this.decrypt = function(encrypted) {
console.log('decrypt', encrypted, CryptoJS[encryptionType].decrypt(encrypted, passphrase).toString()); return CryptoJS[encryptionType].decrypt(encrypted, passphrase)
return CryptoJS[encryptionType].decrypt(encrypted, passphrase, {format: formatter}).toString(); .toString(CryptoJS.enc.Utf8);
}; };
} }
CryptoWrappedProvider.isSupported = function() { CryptoWrappedProvider.isSupported = function() {
@ -105,10 +61,14 @@ define(function(require) {
this.provider.open(callback); this.provider.open(callback);
}; };
CryptoWrappedProvider.prototype.getReadOnlyContext = function() { CryptoWrappedProvider.prototype.getReadOnlyContext = function() {
return new CryptoWrappedContext(this.provider.getReadOnlyContext(), this.encrypt, this.decrypt); return new CryptoWrappedContext(this.provider.getReadOnlyContext(),
this.encrypt,
this.decrypt);
}; };
CryptoWrappedProvider.prototype.getReadWriteContext = function() { CryptoWrappedProvider.prototype.getReadWriteContext = function() {
return new CryptoWrappedContext(this.provider.getReadWriteContext(), this.encrypt, this.decrypt); return new CryptoWrappedContext(this.provider.getReadWriteContext(),
this.encrypt,
this.decrypt);
}; };
return CryptoWrappedProvider; return CryptoWrappedProvider;