简体   繁体   English

将 CryptoJS 与十六进制字符串一起使用

[英]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.

相关问题 使用CryptoJS使用字节[32]的Sha256? - Sha256 with byte[32] using CryptoJS? 打字稿:从十六进制数字转换为 CSS 十六进制颜色的字符串 - Typescript: Convert from hex number to string for CSS hex color 如何在 Angular 11 中使用 CryptoJS 来获得相同的解密字符串,如 C# Rfc2898DeriveBytes - How to use CryptoJS in Angular 11 to get same decrypted string like C# Rfc2898DeriveBytes NodeJS Crypto randomBytes转换为字符串的十六进制加倍大小 - NodeJS Crypto randomBytes to string hex doubling size 如何为 HEX 颜色字符串创建独立类型? - How to create standalone type for a HEX color string? 如何在 TypeScript 中将字符串转换为十六进制并在 Python 中转换回字符串 - How to convert a string into Hex in TypeScript and back to String in Python 如果包装在函数中,CryptoJS 不会解密 - CryptoJS not decrypting if wrapped in a function int32 到十六进制字符串不起作用.. 打字稿 - int32 to hex string not working.. Typescript 在 React JS 中使用 `rgb-hex` 库作为输入时,将十六进制 3 位转换为 6 位? - Making hex 3-digit converts it to 6-digits while using `rgb-hex` library as an input in React JS? TypeError:传入的参数必须是12个字节的字符串或24个十六进制字符的字符串 - TypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM