如何将Uint8Array的对象转换为字符串并将字符串转换回相同的对象? [英] How to convert the object of Uint8Array to string and convert back string to same object?

查看:860
本文介绍了如何将Uint8Array的对象转换为字符串并将字符串转换回相同的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eccrypto.js库进行公共密钥加密. 在这里,我想使用B的公钥加密A的消息,然后让B使用B的私钥解密.

I am using eccrypto.js library for public key encryption. Here I want to encrypt the message of A using public key of B and then let B to decrypt using private key of B.

在库中,这是通过以下方式完成的:

In the library this is done by:

// Encrypting the message for B.
eccrypto.encrypt(publicKeyB, Buffer.from("msg to b")).then(function(encrypted) {
  // B decrypting the message.
  eccrypto.decrypt(privateKeyB, encrypted).then(function(plaintext) {
    console.log("Message to part B:", plaintext.toString());
  });
});

如果我用控制台记录加密值,则得到的对象为:

if I console log the encrypted value I got the object as:

{iv: Uint8Array(16), ephemPublicKey: Uint8Array(65), ciphertext: Uint8Array(48), mac: Uint8Array(32)}
ciphertext: Uint8Array(48) [13, 240, 10, 109, 88, 109, 108, 153, 213, 115, 40, 237, 66, 232, 251, 120, 27, 67, 119, 231, 17, 143, 78, 69, 43, 76, 214, 74, 132, 127, 220, 131, 44, 144, 221, 133, 48, 124, 239, 158, 226, 22, 119, 200, 170, 101, 241, 82]
ephemPublicKey: Uint8Array(65) [4, 84, 253, 207, 251, 2, 157, 203, 14, 233, 166, 216, 107, 1, 23, 90, 229, 209, 150, 58, 95, 253, 214, 183, 148, 167, 224, 15, 224, 244, 176, 165, 84, 121, 70, 4, 175, 186, 189, 104, 211, 207, 255, 195, 20, 128, 200, 237, 7, 9, 173, 234, 14, 208, 208, 68, 46, 76, 38, 26, 107, 41, 10, 188, 108]
iv: Uint8Array(16) [230, 246, 79, 17, 203, 191, 117, 7, 57, 149, 198, 68, 193, 220, 159, 56]
mac: Uint8Array(32) [202, 77, 212, 211, 27, 186, 174, 106, 211, 145, 100, 81, 100, 68, 61, 172, 175, 188, 213, 49, 63, 92, 172, 83, 30, 22, 47, 93, 60, 215, 33, 116]

注意:我必须以json格式存储此文件,因此我已将其转换为json数据.

Note: I have to store this in json format so i have converted it into json data.

现在,如果我将此对象转换为json并恢复json数据,则恢复的对象并不相同.

Now if I convert this object to json and recover the json data the recovered object is not same.

Json对象:

{
"iv":{"type":"Buffer","data":[226,253,245,0,227,222,47,37,65,177,171,68,201,142,242,35]},
"ephemPublicKey":{"type":"Buffer","data":[4,9,137,99,138,202,169,89,90,209,92,130,156,105,170,132,192,250,88,232,15,250,33,107,38,13,129,178,21,237,77,136,215,39,215,123,140,226,102,98,39,110,192,209,79,214,138,83,174,192,100,183,157,44,56,128,38,52,170,244,42,213,199,57,232]},
"ciphertext":{"type":"Buffer","data":[135,147,187,164,109,39,204,244,195,161,65,24,178,160,132,146,200,35,113,120,164,140,20,223,225,104,23,111,13,155,193,26,35,73,236,77,209,246,85,16,77,30,250,122,206,242,111,63]},
"mac":{"type":"Buffer","data":[79,195,220,150,230,150,13,187,9,131,12,81,151,107,29,216,138,143,85,52,153,71,179,167,243,141,107,88,97,206,110,107]}}

恢复的对象(JSON.parse(加密))为:

The recovered object (JSON.parse(encrypted)) is:

{iv: {…}, ephemPublicKey: {…}, ciphertext: {…}, mac: {…}}
ciphertext:
data: (48) [135, 147, 187, 164, 109, 39, 204, 244, 195, 161, 65, 24, 178, 160, 132, 146, 200, 35, 113, 120, 164, 140, 20, 223, 225, 104, 23, 111, 13, 155, 193, 26, 35, 73, 236, 77, 209, 246, 85, 16, 77, 30, 250, 122, 206, 242, 111, 63]
type: "Buffer"
__proto__: Object
ephemPublicKey:
data: (65) [4, 9, 137, 99, 138, 202, 169, 89, 90, 209, 92, 130, 156, 105, 170, 132, 192, 250, 88, 232, 15, 250, 33, 107, 38, 13, 129, 178, 21, 237, 77, 136, 215, 39, 215, 123, 140, 226, 102, 98, 39, 110, 192, 209, 79, 214, 138, 83, 174, 192, 100, 183, 157, 44, 56, 128, 38, 52, 170, 244, 42, 213, 199, 57, 232]
type: "Buffer"
__proto__: Object
iv:
data: (16) [226, 253, 245, 0, 227, 222, 47, 37, 65, 177, 171, 68, 201, 142, 242, 35]
type: "Buffer"
__proto__: Object
mac:
data: (32) [79, 195, 220, 150, 230, 150, 13, 187, 9, 131, 12, 81, 151, 107, 29, 216, 138, 143, 85, 52, 153, 71, 179, 167, 243, 141, 107, 88, 97, 206, 110, 107]
type: "Buffer"
__proto__: Object
__proto__: Object

如果我使用恢复的对象进行解密,我会得到错误的错误密码作为公钥.

if i used the recovered object to decrypt I am getting the error as bad public key.

我的代码是:


let PublicKey = Buffer.from("0418c7ced07c0c17f42b132747c70fddb6b31ea0ad349c2e9f800f48f0a73c2ea028d41b239077a48136ce546f9d2811bf1ec311c56e6a41f33906a1fc2472e451", 'hex')
                eccrypto.encrypt(PublicKey, Buffer.from('Message to encrypt').then(function (encrypted) {
                    console.log(encrypted)
                    let encoded = JSON.stringify(encrypted)
                    console.log(encoded)
                    var actual = JSON.parse((encoded))
                    console.log(actual)

                    let pk = Buffer.from("9a2d66404b69023c2c45da81ca4b696a8234b7ae53ea6b7ffc0d6bdd0e0e3279", 'hex')

                    eccrypto.decrypt(pk, actual).then(function (plaintext) {
                        console.log("Message to part B:", plaintext.toString());
                    });

                });

我由于错误的公共密钥而收到错误消息.

I am getting error as bad public key.

如果我在eccrypto.decrypt(pk,encrypted)中使用了加密变量而不是acutal ...

If i used the encrypted variable instead of acutal in the eccrypto.decrypt(pk,encrypted)...

然后我得到了解密后的值.

then i get the decrypted value.

推荐答案

您可以在JSON.parse中添加第二个参数,以允许您注入代码以帮助在反序列化JSON时对其进行解释.在文档中称为reviver.

You can add a second argument to JSON.parse to allow you to inject code to help interpret the JSON as it's deserialized. It's called a reviver in the documentation.

您将在对象中获得每个键,值对.

You will get each key, value pair in the object.

以下是一些示例代码:

JSON.parse('{"p": 5}', (key, value) =>
  typeof value === 'number'
    ? value * 2 // return value * 2 for numbers
    : value     // return everything else unchanged
);

以下是有关MDN的模式信息: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Here is mode info on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

在您的示例中,我认为您想将值标识为buffer类型,然后将其替换为UInt8Array

In your example, I think you want to identify the values as type buffer and then replace them with UInt8Array

这篇关于如何将Uint8Array的对象转换为字符串并将字符串转换回相同的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆