C# で RSA アルゴリズムを実装しようとしています。以下のコードは、p と q が小さい場合は機能しますが、p と q が非常に大きい RSA-100 以上を複製しようとすると機能しません。
たとえば、次の場合:
p = 61, q = 53, n = 3233, phi(n) = 3120, e = 17, d = 2753
復号化すると、正しい元のメッセージが得られます。これらの値は、RSA ウィキペディアのページから取得しました。このコードは、p と q の他の小さな値に対しても機能します。
ただし、RSA-100以上を使用すると、元のメッセージが返されません。指数 (e) に異なる値を使用してみましたが、それが phi(n) と互いに素であることを確認しましたが、正しい結果が得られません。シンプルで明白なものがありませんか?
よろしくお願いします。
//p and q for RSA-100
//string p = "37975227936943673922808872755445627854565536638199";
//string q = "40094690950920881030683735292761468389214899724061";
string p = "61";
string q = "53";
//Convert string to BigInteger
BigInteger rsa_p = BigInteger.Parse(p);
BigInteger rsa_q = BigInteger.Parse(q);
//n = p * q
BigInteger rsa_n = BigInteger.Multiply(rsa_p, rsa_q);
//phi(n) = (p-1)*(q-1)
BigInteger rsa_fn = BigInteger.Multiply((rsa_p - 1), (rsa_q - 1));
BigInteger rsa_e = 17;
//Compute d
BigInteger rsa_d = BigInteger.ModPow(rsa_e, (rsa_fn - 1), rsa_fn);
//Encrypt the message, in this case 3007
//C = (3007^rsa_e) mod rsa_n
BigInteger C = BigInteger.ModPow(3007, rsa_e, rsa_n);
//Decrypt the message, M should equal 3007
//M = (3007^rsa_d) mod rsa_n
BigInteger M = BigInteger.ModPow(C, rsa_d, rsa_n);