これはかなり基本的な質問だと思いますが、私は JavaScript と RSA の研究を始めているので、少し迷っています。ライブラリ Cryptico をダウンロードしたところです。これにより、RSA キーの生成/暗号化/復号化を簡単に使用できます。生成された RSA キーの公開部分は、次のコマンドを使用するだけで簡単に抽出できます。
publicKeyString(RsaKey)
それは次のとおりです。
my.publicKeyString = function(rsakey)
{
pubkey = my.b16to64(rsakey.n.toString(16));
return pubkey;
}
rsakey.n は、関数でキーを生成するときに定義されます。
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