I want to connect to a bluetooth device. Communication is via Hex-Strings only. I need to encode a 16 byte value. As a result I also expect a 16 byte value. In my implementation CryptoJS always returns a longer result. According to the documentation the IV is not needed. ("All the 16-byte data must be encrypted with the Customer Master Key currently stored in the device, using AES128 CBC cipher mode") Therefore I set the IV to 00000000000000000000000000000000 because CryptoJS seems to require it. What am I doing wrong?
const CryptoJS = require('crypto-js');
const value = CryptoJS.enc.Hex.parse('5ff58680541c5a5903f4833dfaa4281f');
const key = CryptoJS.enc.Hex.parse('41435231323535552d4a312041757458'); // known master key
const ivvar = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
const encryptedString = CryptoJS.AES.encrypt(value, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}).toString();
// current result is edijc9R7sl3zwZVrBBBrFQ==
Sure, the result is only a string, but it is too long anyway.
79d8a373d47bb25df3c1956b04106b15
, e.g. here. Instead of a Base64 encoding you can also directly use a hexadecimal encoding. To do so, you just have to replacetoString()
withciphertext.toString()
, e.g. here.0
-IV, the connection is likely to fail (but since according to the description no IV is needed, a0
-IV is plausible). By the way, for a 1 block (16 bytes for AES) plaintext, CBC with a0
-IV is identical to ECB, so an implementation without IV would also be possible:CryptoJS.AES.encrypt(value, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding}).ciphertext.toString();
const value = CryptoJS.enc.Hex.parse('79d8a373d47bb25df3c1956b04106b15'); const key = CryptoJS.enc.Hex.parse('41435231323535552d4a312041757458'); const decryptedStringHex = CryptoJS.AES.decrypt(value, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});
decryptedStringHex is empty, seems we give the wrong input. Help appreciated.decrypt
,value
must be replaced by{ciphertext: value}
. The hexadecimally encoded plaintext is then contained indecryptedStringHex.toString()
.