/// <summary> /// 公钥加密私钥解密测试,结果能正确解密,RSA可逆 /// </summary> public static void EncryptTest() { var test = new RsaTestClass(); var hashStr = test.GetHash(StrSource); var publicKeyXmlStr = test.ReadPublicKey(PublicKeyPath); var encryptStr = test.RsaEncrypt(publicKeyXmlStr, hashStr); var privateKeyXmlStr = test.ReadPrivateKey(KeyPairPath); var decryptStr = test.RsaDecrypt(privateKeyXmlStr, encryptStr); var isEqure = hashStr == decryptStr; }
/// <summary> /// 签名测试,对摘要用私钥签名,然后用公钥和摘要验证该签名 /// </summary> public static void SignatureTest() { var test = new RsaTestClass(); var hashStr = test.GetHash(StrSource); //故意改变原始字符串,反向测试 var hashStr1 = test.GetHash(StrSource + "--a"); //对hashString进行签名 var privateKeyXmlStr = test.ReadPrivateKey(KeyPairPath); var signatureStr = test.SignatureFormatter(privateKeyXmlStr, hashStr); //验证签名 var publicKeyXmlStr = test.ReadPublicKey(PublicKeyPath); var checkRes1 = test.SignatureDeformatter(publicKeyXmlStr, hashStr, signatureStr); var checkRes2 = test.SignatureDeformatter(publicKeyXmlStr, hashStr1, signatureStr); }
/// <summary> /// 生成公钥私钥测试 /// </summary> public static void CreateKeysTest() { var test = new RsaTestClass(); test.RsaKey(KeyPairPath, PublicKeyPath); }