我正在尝试使用RSA公钥加密邮件并使用我的私钥对其进行解密。它加密了邮件,但我无法解密它。在最后的过程之后,消息仍然是加密的。当我导出私钥时,它还包括公钥。我试图删除公钥,但它不起作用。 这是私钥和公钥
//This is the public key
private const string public_key = "<RSAKeyValue><Modulus>uznzVPilsR1rWPkpq6m6IALaafDnVZTDDcnEyBD3A/PBx2JZTKM0DTgiTDDwCEmQBNBpPILcIBdtg3aSUgicair+2ksYrVFT+uiy0Zy1nU6qoJ+SsapLKrpCa1zHpV4LMO/pFo4Foqzw0C1FNe56FXo1xj77GPgeYl0MHUVtAUc=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
//This is the private and public key.
private const String private_key = "<RSAKeyValue><Modulus>uznzVPilsR1rWPkpq6m6IALaafDnVZTDDcnEyBD3A/PBx2JZTKM0DTgiTDDwCEmQBNBpPILcIBdtg3aSUgicair+2ksYrVFT+uiy0Zy1nU6qoJ+SsapLKrpCa1zHpV4LMO/pFo4Foqzw0C1FNe56FXo1xj77GPgeYl0MHUVtAUc=</Modulus><Exponent>AQAB</Exponent><P>+jPKs9JxpCSzNY+YNanz49Eo/A6RaU1DZWoFm/bawffZOompeL1jzpUlJUIrKVZJkNFvlxE90uXVwjxWBLv9BD==</P><Q>v5CVWKZ5Wo7W0QyoEOQS/OD8tkKS9DjzZnbnuo6lhcMaxsBrCWLstac1Xm2oFNtZgLtrPGbPfCNC5Su4Rz/P5w==</Q><DP>ZnyikmgobqEt20m3gnvcUDxT+nOJMsYYTklQhONoFj4M+EJ9bdy+Lle/gHSLM4KJ3c08VXgVh/bnSYnnfkb20Q==</DP><DQ>sSYGRfWk0W64Dpfyr7QKLxnr+Kv186zawU2CG44gWWNEVrnIAeUeWxnmi41CWw9BZH9sum2kv/pnuT/F6PWEzw==</DQ><InverseQ>XpWZQKXa1IXhF4FX3XRXVZGnIQP8YJFJlSiYx6YcdZF24Hg3+Et6CZ2/rowMFYVy+o999Y5HDC+4Qa1yWvW1vA==</InverseQ><D>Kkfb+8RrJqROKbma/3lE3xXNNQ7CL0F5CxQVrGcN8DxL9orvVdyjlJiopiwnCLgUHgIywceLjnO854Q/Zucq6ysm2ZRq36dpGLOao9eg+Qe8pYYO70oOkEe1HJCtP1Laq+f3YK7vCq7GkgvKAI9uzOd1vjQv7tIwTIADK19ObgE=</D></RSAKeyValue>";
//Encrypting the text using the public key
private RSACryptoServiceProvider cipher = null;
cipher = new RSACryptoServiceProvider();
cipher.FromXmlString(public_key);
byte[] data = Encoding.UTF8.GetBytes(txtUnencrypt.Text);
byte[] cipherText = cipher.Encrypt(data, false);
lblUnencryptMessage.Text = Convert.ToBase64String(cipherText);
// decryptText();
//Trying to decrypt the text using the private key
cipher = new RSACryptoServiceProvider();
cipher.FromXmlString(private_key);
byte[] ciphterText = Convert.FromBase64String(lblUnencryptMessage.Text);
byte[] plainText = cipher.Decrypt(ciphterText, false);
lblDecript.Text = Convert.ToBase64String(plainText);
我在这里缺少什么?
于上午11:37更新
例如,我加密的字 “测试”,加密的文本是kkqs + UGHNI7 / 3cKhQvSnJrKzNeCBQX9xHX2VrlyMvnwtszJAoFuViBZlfwmpVhqddnVUrlaqqkD7971E8L3wWltfGetK9nIljeo0GeietLYljoY0Gy3gatU ++ JPrqajAKxpIB75tvVlKXuYIs0qE3XWZu9bj0zAa4BVT2MhVNQM =“
解密后的文字是 dGVzdGluZw ==
答案 0 :(得分:8)
加密/解密代码似乎没有任何问题,只是处理解密数据的方式。特别是这一行:
lblDecript.Text = Convert.ToBase64String(plainText);
你正在使用解密数据和Base64编码,这就是你得到的原因:dGVzdGluZw ==(因为这是字符串“testing”的Base64编码版本)。
您需要使用以下内容:
lblDecript.Text = Encoding.UTF8.GetString(plainText);
这应该正确地将解密的字节数组转换为原始字符串。
答案 1 :(得分:-3)
我在这里找到了一个包含帮助类的完整教程
这将解释如何在asp.net c#环境中验证 JWS 令牌,同时验证时间到期。
在以下链接中查看
http://www.techdoubts.net/2015/01/json-web-signature-jws-rsa-public-key-aps-net.html,它也会对初学者有帮助。