[英]using CryptoJS with hex-string
I want to connect to a bluetooth device.我想连接到蓝牙设备。 Communication is via Hex-Strings only.
通信仅通过十六进制字符串。 I need to encode a 16 byte value.
我需要编码一个 16 字节的值。 As a result I also expect a 16 byte value.
因此,我还期望有一个 16 字节的值。 In my implementation CryptoJS always returns a longer result.
在我的实现中,CryptoJS 总是返回更长的结果。 According to the documentation the IV is not needed.
根据文档,不需要 IV。 ("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.
(“必须使用 AES128 CBC 密码模式,使用当前存储在设备中的客户主密钥对所有 16 字节数据进行加密”)因此,我将 IV 设置为 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 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== // 当前结果是 edijc9R7sl3zwZVrBBBrFQ==
Sure, the result is only a string, but it is too long anyway.当然,结果只是一个字符串,但它太长了。
For the sake of completeness I would like to add the solution, which works for me.为了完整起见,我想添加对我有用的解决方案。 (Thanks to @Topaco)
(感谢@Topaco)
encrypt(valueStringHex, keyStringHex) {
const CryptoJS = require('crypto-js');
const value = CryptoJS.enc.Hex.parse(valueStringHex);
const key = CryptoJS.enc.Hex.parse(keyStringHex);
const ivvar = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
const encryptedStringHex = CryptoJS.AES.encrypt(value, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}).ciphertext.toString();
return encryptedStringHex;
}
// encrypt('5ff58680541c5a5903f4833dfaa4281f', '41435231323535552d4a312041757458')
// returns 79d8a373d47bb25df3c1956b04106b15
decrypt(valueStringHex, keyStringHex) {
const CryptoJS = require('crypto-js');
const value = CryptoJS.enc.Hex.parse(valueStringHex);
const key = CryptoJS.enc.Hex.parse(keyStringHex);
const ivvar = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
const decryptedStringHex = CryptoJS.AES.decrypt({ciphertext: value}, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding});
return decryptedStringHex.toString();
}
// decrypt('79d8a373d47bb25df3c1956b04106b15', '41435231323535552d4a312041757458')
// returns 5ff58680541c5a5903f4833dfaa4281f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.