In CryptoJS, iv will be assigned automatically if iv is not given. But in PHP, NULL vectors will be used as iv...
So the result generated by CryptoJS cannot be decoded by PHP.
Hence, I've been digging up the solution to overcome this problem....
And the following code segment is what I found and tested.
Feel free to copy this. [ JsFiddle Test ]
BTW, This site can be used to test the encoding result. ( This site uses PHP as its backend... )
(function () {
// Simple Object to be encrypted...
var test = {
a: "a",
b: "b",
c: 1,
d: 100,
e: 3.1415926,
f: [0, 1, 2, 3, 4]
};
// Serialize the object first!!!
var serialized = JSON.stringify(test);
// Encrypt the serialized data string with keyphrase using TripleDES algorithm
// Note that the keyphrase must be decoded as WordArray first...
var encObj = CryptoJS.TripleDES.encrypt(serialized, CryptoJS.enc.Utf8.parse("TEST1234567890TSET"), {
mode: CryptoJS.mode.CBC, // Using CBC Mode
padding: CryptoJS.pad.Pkcs7, // Using Pkcs7 key
// In PHP, iv(initial vector) is not assigned, an array of 16 bytes "\0" strings will be used.. Tricky part...
iv: CryptoJS.lib.WordArray.create([0x00000000, 0x00000000, 0x00000000, 0x00000000])
});
var encStr = encObj.toString();
console.log(encStr);
// Decryption must use the same parameters...
var dec = CryptoJS.TripleDES.decrypt(encStr, CryptoJS.enc.Utf8.parse("TEST1234567890TSET"), {
padding: CryptoJS.pad.Pkcs7,
iv: CryptoJS.lib.WordArray.create([0x00000000, 0x00000000, 0x00000000, 0x00000000])
});
console.log(dec.toString(CryptoJS.enc.Utf8));
})();
No comments:
Post a Comment