加密和解密首先是 JSON 的对象
encrypting and decripting and object that first was a JSON
我正在尝试加密一个 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
是 var recibidoBignum = bignum(req.body.mensaje);
.
中的 Base 64 编码字符串
完整代码如下:
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());
我正在尝试加密一个 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
是 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());