/// <summary> /// 解密操作 /// </summary> /// <param name="c">密文</param> /// <returns>明文</returns> public BigInteger DecryptBlock(BigInteger c) { if (c >= publicKey.n || c < 0) { throw new ArgumentException("内容越界"); } // 普通解密算法 // return BigInteger.ModPow(c, d, publicKey.n); // 快速解密算法 BigInteger c1 = c % p, c2 = c % q; BigInteger d1 = d % (p - 1), d2 = d % (q - 1); BigInteger m1 = BigInteger.ModPow(c1, d1, p); BigInteger m2 = BigInteger.ModPow(c2, d2, q); return(Mathbase.CRT(m1, p, m2, q)); }
/// <summary> /// 生成新密钥、公钥对 /// </summary> /// <param name="bytes">RSA算法的字节数(bit数除以8)</param> public RSA(int bytes) { ByteCount = bytes; BigInteger phi; BigInteger e = 65537; BigInteger n; do { p = Mathbase.GeneratePrime(bytes / 2); q = Mathbase.GeneratePrime(bytes / 2); n = p * q; phi = (p - 1) * (q - 1); } while (phi <= e || BigInteger.GreatestCommonDivisor(e, phi) != 1); d = Mathbase.GetInverse(e, phi); publicKey = new RSAPublicKey(e, n, ByteCount); }