我相信這是一個非常基本的問題,但我開始使用JavaScript和RSA進行研究,所以我有點失落。我剛剛下載了Cryptico庫,它爲我提供了一個易於使用的RSA密鑰生成/加密/解密。從Cryptico.js中提取RSA私鑰
publicKeyString(RsaKey)
哪個:
my.publicKeyString = function(rsakey)
{
pubkey = my.b16to64(rsakey.n.toString(16));
return pubkey;
}
的rsakey.n定義所生成的RSA密鑰的公共部分,可以很容易地只使用命令來提取同時在函數中生成密鑰:
function RSAGenerate(B, E)
{
var rng = new SeededRandom();
var qs = B >> 1;
this.e = parseInt(E, 16);
var ee = new BigInteger(E, 16);
for (;;)
{
for (;;)
{
this.p = new BigInteger(B - qs, 1, rng);
if (this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
}
for (;;)
{
this.q = new BigInteger(qs, 1, rng);
if (this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
}
if (this.p.compareTo(this.q) <= 0)
{
var t = this.p;
this.p = this.q;
this.q = t;
}
var p1 = this.p.subtract(BigInteger.ONE);
var q1 = this.q.subtract(BigInteger.ONE);
var phi = p1.multiply(q1);
if (phi.gcd(ee).compareTo(BigInteger.ONE) == 0)
{
this.n = this.p.multiply(this.q);
this.d = ee.modInverse(phi);
this.dmp1 = this.d.mod(p1);
this.dmq1 = this.d.mod(q1);
this.coeff = this.q.modInverse(this.p);
break;
}
}
}
但是私有部分的關鍵,我只是不明白如何提取,所以我將能夠保存公鑰/私鑰部分,並可供以後使用。
庫文件: https://github.com/wwwtyro/cryptico
謝謝你的答案。那麼,你有辦法查看或保存整個對象的'rsakey'嗎?因爲當我保存我只看到[對象對象] ...正如我所說,稍後使用相同的密鑰進行加密/解密,我需要它以某種方式保存。 – user2864778
我添加了私鑰serialiazation –
謝謝。但我仍然沒有成功完成這項工作。現在我感到有點慚愧,因爲你做了一件大事,我仍然不知道如何做這件事。我需要的只是保存密鑰以供後期使用,因爲它的製作方式,我沒有以某種方式保存和加密/解密事情的密鑰(至少是私人密鑰)......你有我嗎? – user2864778