gpt4 book ai didi

javascript - 加密和解密最初是 JSON 的对象

转载 作者:行者123 更新时间:2023-12-03 07:09:55 39 4
gpt4 key购买 nike

我正在尝试加密一个 JSON 对象,为了做到这一点,我首先将其转换为字符串,对其进行加密,当解密时,它会以随机符号的形式返回,因为它无法转换它。

我尝试加密:

 { a: 'A',
L: 'GET ALL USERS',
Po:
{ ttp: 'localhost:3000/ttp/allusers',
b: 'localhost:8000/server/allusers',
Mhash: '6d2e5cc3a67ae82ae7edf6fb6054f977' } }


var mensajeToBbignum = bignum.fromBuffer(new Buffer(x));
console.log('\n\n\nCleartext:',
mensajeToBbignum.toBuffer().toString(),'\n');
var mensajeToBcrip = keys2.publicKey.encrypt(mensajeToBbignum);
console.log('encryption with public:', '\n',
mensajeToBcrip.toBuffer().toString('base64'), '\n');

从字符串到加密我有以下内容:

{"a":"A","L":"GET ALL USERS","Po":{"ttp":"localhost:3000/ttp/allusers","b":"localhost:8000/server/allusers","Mhash":"6d2e5cc3a67ae82ae7edf6fb6054f977"}}

最后以base64编码:

 augf1Fuv2GwOYy0aipv1u6LZ3nWvGVz4M9JoA8uhlJgbuoGtYxe0GLSW+u6s1/kiOIqeF0s0cmCFgzpj/oKdF+0k9+OC/TVBgmk+1mO19pWnhcfS42j5OKPpy27mx0tRymQcS7TVDDsak2JptEv7O3POAvWVAKZRJ13zGMwP4qU=

我知道它使用以下代码接收到相同的 base64 字符串:

var recibidoBignum = bignum(req.body.mensaje);
console.log('recibidoBignum:', '\n',
recibidoBignum.toBuffer().toString('base64'), '\n');
var reqdecrip = keys.privateKey.decrypt(recibidoBignum);
console.log('decryption with private:', '\n', reqdecrip.toBuffer().toString(), '\n\n\n\n\n\n');

以及以下日志:

encryption with public: 

augf1Fuv2GwOYy0aipv1u6LZ3nWvGVz4M9JoA8uhlJgbuoGtYxe0GLSW+u6s1/kiOIqeF0s0cmCFgzpj/oKdF+0k9+OC/TVBgmk+1mO19pWnhcfS42j5OKPpy27mx0tRymQcS7TVDDsak2JptEv7O3POAvWVAKZRJ13zGMwP4qU=

decryption with private:
�8����P�`��t�> �x)���m��S���n�l� �:�17^�����l�}%��綷K�N�Y�a-5��J���p���8�@�b�Vs�

看来它可以加密 JSON 对象,但无法解密

唯一使用的外部模块是bignum,rsa实现如下

rsa = {
publicKey: function(bits, n, e) {
this.bits = bits;
this.n = n;
this.e = e;
},
privateKey: function(p, q, d, publicKey) {
this.p = p;
this.q = q;
this.d = d;
this.publicKey = publicKey;
},
generateKeys: function(bitlength) {
var p, q, n, phi, e, d, keys = {};
// if p and q are bitlength/2 long, n is then bitlength long
this.bitlength = bitlength || 2048;
console.log("Generating RSA keys of", this.bitlength, "bits");
p = bignum.prime(this.bitlength / 2);
do {
q = bignum.prime(this.bitlength / 2);
} while (q.cmp(p) === 0);
n = p.mul(q);

phi = p.sub(1).mul(q.sub(1));

e = bignum(65537);
d = e.invertm(phi);

keys.publicKey = new rsa.publicKey(this.bitlength, n, e);
keys.privateKey = new rsa.privateKey(p, q, d, keys.publicKey);
return keys;
}
};


rsa.publicKey.prototype = {
encrypt: function(m) {
return m.powm(this.e, this.n);
},
decrypt: function(c) {
return c.powm(this.e, this.n);
}
};

rsa.privateKey.prototype = {
encrypt: function(m) {
return m.powm(this.d, this.publicKey.n);
},
decrypt: function(c) {
return c.powm(this.d, this.publicKey.n);
}
};

module.exports = rsa;

最佳答案

这是一个编码问题。 bignum(s) 假设 s 是 Base 10 的整数,但根据您的描述 req.body.mensaje 是 Base 64 编码的var recibidoBignum = bignum(req.body.mensaje); 中的字符串。

完整代码如下:

var bignum = require("bignum");

var rsa = {
publicKey: function(bits, n, e) {
this.bits = bits;
this.n = n;
this.e = e;
},
privateKey: function(p, q, d, publicKey) {
this.p = p;
this.q = q;
this.d = d;
this.publicKey = publicKey;
},
generateKeys: function(bitlength) {
var p, q, n, phi, e, d, keys = {};
// if p and q are bitlength/2 long, n is then bitlength long
this.bitlength = bitlength || 2048;
console.log("Generating RSA keys of " + this.bitlength + " bits");
p = bignum.prime(this.bitlength / 2);
do {
q = bignum.prime(this.bitlength / 2);
} while (q.cmp(p) === 0);
n = p.mul(q);

phi = p.sub(1).mul(q.sub(1));

e = bignum(65537);
d = e.invertm(phi);

keys.publicKey = new rsa.publicKey(this.bitlength, n, e);
keys.privateKey = new rsa.privateKey(p, q, d, keys.publicKey);
return keys;
}
};


rsa.publicKey.prototype = {
encrypt: function(m) {
return m.powm(this.e, this.n);
},
decrypt: function(c) {
return c.powm(this.e, this.n);
}
};

rsa.privateKey.prototype = {
encrypt: function(m) {
return m.powm(this.d, this.publicKey.n);
},
decrypt: function(c) {
return c.powm(this.d, this.publicKey.n);
}
};

var keys = rsa.generateKeys(2048);

var pt = '{"a":"A","L":"GET ALL USERS","Po":{"ttp":"localhost:3000/ttp/allusers","b":"localhost:8000/server/allusers","Mhash":"6d2e5cc3a67ae82ae7edf6fb6054f977"}}';

console.log("Plaintext: " + pt);

var mensajeToBbignum = bignum.fromBuffer(new Buffer(pt));
var mensajeToBcrip = keys.publicKey.encrypt(mensajeToBbignum);
var ct = mensajeToBcrip.toBuffer().toString('base64');

console.log("Ciphertext: " + ct);

var recibidoBignum = bignum.fromBuffer(new Buffer(ct, "base64"));
var reqdecrip = keys.privateKey.decrypt(recibidoBignum);
console.log('Recovered plaintext: ' + reqdecrip.toBuffer().toString());

Runnable Demo

关于javascript - 加密和解密最初是 JSON 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36649425/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com