I have to decrypt some strings which are AES encrypted.
Example encrypted string: 129212143036071008133136215105140171136216244116
I have a key, and a vector (iv) supplied to me in a byte-array format:
Key: [ 123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209 ]
Vector (iv): [ 146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136 ]
I should be able to decrypt the string and get:
Correct output: testtest
I'm trying to use Crypto.js but I can't find a way to use the supplied key and vector. I can't find a way to convert the byte-arrays to hex.
var encrypted = '129212143036071008133136215105140171136216244116';
var key = CryptoJS.enc.Hex.parse([ 123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209 ]);
var iv = CryptoJS.enc.Hex.parse([ 146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136 ]);
var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv });
console.log('Output: '+decrypted.toString(CryptoJS.enc.Utf8)); //Should be "testtest"
I would be so grateful if anyone could show me how to decrypt the example string using the key and vector with Crypto.js OR any other js method.
Thanks so much for any help, Kind regards
If you tend to pass custom key and IV in using CryptoJS, make sure that (assuming that CryptoJS.enc.Base64.parse () gives HEX string, which is used in CryptoJS.AES.encrypt () ). Length of the key is 32 bytes for AES-256. (16 bytes if you want to get AES-128.
To decrypt a byte array, we basically need to perform the same steps as in the encrypt function, but this time, we chose the Cipher.DECRYPT_MODE. The only extra steps that we need to perform is to extract the nonce and the nonce length from the beginning of the byte array.
Taking this example, with Base64 key and iv (length=22), which CryptoJS encrypts as AES-256: Length of the key is 32 bytes for AES-256. (16 bytes if you want to get AES-128.
where key is 32 bytes, IV is 16. CryptoJS requires to parse it, and after CryptoJS.enc.Base64.parse () I get 48 and 24 bytes accordingly. I expect that those values will get truncated to required 256-bit AES length, and further expansion to n bytes will be irrelevant, and so resulting ciphertext will be the same.
I can't manage to decrypt your original string, but I can successful use it to encrypt and decrypt a new string. In your initial implementation, an error occurs in aes.js
because it expects key
and iv
to be strings rather than arrays. I have corrected this code example below:
//var encrypted = '129212143036071008133136215105140171136216244116';
var key = CryptoJS.enc.Hex.parse(CryptoJS.lib.ByteArray([123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209]));
var iv = CryptoJS.enc.Hex.parse(CryptoJS.lib.ByteArray([146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136]));
var message = 'testest'
var encrypted = CryptoJS.AES.encrypt(message, key, {
'iv': iv
});
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
'iv': iv
});
document.write('Output: ' + decrypted.toString(CryptoJS.enc.Utf8)); //Should be "testtest"
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-nopadding-min.js"></script>
<script src="https://greasyfork.org/scripts/6696-cryptojs-lib-bytearray/code/CryptoJSlibByteArray.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With