[ad_1]
我正在尝试使用 C# 内置库使用 RSA 算法加密字符串
System.Net.Cryptograhy.RSACryptoServiceProvider。
假设我有一个私钥(例如):
` -- -- - BEGIN RSA PRIVATE KEY-- -- - MIICXQIBAAKBgQCFf3jKXbhn2GOiCzvAxLTmZ34BdFQyNDKt / 7 B3qQ1thmSPG / NK nrYG8OIHhDqIsGkT1YcEOQKEHPhgmtGj78Fxrv3AEi56VsYfJUTOJn9GTmqPrpk / zscIiD5HkWzx8JfOQS5uPHWwGmQpYftDod82nYinlwuZdAVOcz3 + 7 dShlQIDAQAB AoGAT2sCJQGUTtMAkso0Bf3 / 0 IIJCDx4SlWbQMA37zf28Q1ejXSHTAEXMRHkpawY VnD8UfxZkj2ILag + atovlWNwpJ + Qgw4 + t3KvPXEQB1LtMDa0q1fA + I3qU / 5 uiOCq 07 y1IWfhJ6UsfhXYV5gOmADeVrwnuGz6YUIBLQ6at3ebHgECQQDPo + 0 UUFC9z6ZX x2a6UNIFZB9YPmxy599GMjOQOf3tEYH + qn6tYz9oZu7cWrQzkoEJ7Uw7xQsqXADU ekfaL2f1AkEApJb6G4 + zB8gCWQy6DrqiK4Y6d1CN3s67uuZXMcpQEuju9jNBHwAj g8zdwNAf98dksRyt7XICROrxv5i7mvFvIQJAPau5eaQVJxsqk720xDfG + TTwnlwE B + jYhtfYOskOoz / k9UecUlE8 / vw7i5UhGoQz8UfasZHnrBAhavpapvIXlQJBAIPE q2 + tV / N + 2 QJaAgsQuAyIZf5n62zZrcov5tbh1cGN4uIQnq4m + kfmmBJpjW5dn + t7 vnM + tzpdrjVqHagqaoECQQCAZl + BLRqEQa9cKH5lpJ4k9L + 3 sX4LQjuwNkFyZTr / K6Tw5lZv4eBDWtlYToEL3mtgLR7BKEled5cHCZ6pr1nm -- -- - END RSA PRIVATE KEY-- -- -`
我想用这个私钥来加密该字符串。
我在互联网上看到过大多数这样的例子
C#
`string data = "ABC"; RSACryptoServiceProvider crypto = new RSACryptoServiceProvider(); string cipherText = Encrypt(data, rsaCryptoServiceProvider.ExportParameters(false)); public static string Encrypt(string data, RSAParameters key) { using(var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(PrivateKey); rsa.ImportParameters(key); var byteData = Encoding.UTF8.GetBytes(data); var encryptData = rsa.Encrypt(byteData, false); return Convert.ToBase64String(encryptData); } }`
但在这种方法中,我没有使用提供的私钥。
任何人都可以帮助使用提供的私钥进行加密吗?
我期望从我的私钥中获得正确的密文。
预先感谢您。如有任何帮助,我们将不胜感激。
我尝试过的:
我试过这个
C#
`string data = "ABC"; RSACryptoServiceProvider crypto = new RSACryptoServiceProvider(); string cipherText = Encrypt(data, rsaCryptoServiceProvider.ExportParameters(false)); public static string Encrypt(string data, RSAParameters key) { using(var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(key); var byteData = Encoding.UTF8.GetBytes(data); var encryptData = rsa.Encrypt(byteData, false); return Convert.ToBase64String(encryptData); } }`
解决方案1
解决方案2
public string GetEncryptValue(long connectionNumber) { long creditCardNumber = (connectionNumber); string publicKey = ConfigSettings.GetMobikwickEncrypKey(); string encryptedCreditCard = EncryptWithRSA(creditCardNumber, publicKey); byte[] encryptedBytes = Convert.FromBase64String(encryptedCreditCard); string decryptedData = Encoding.UTF8.GetString(encryptedBytes); return encryptedCreditCard; }
static string EncryptWithRSA(long dataToEncrypt, string publicKey) { try { RSAParameters rsaParameters = GetRSAParametersFromKey(publicKey); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { //byte[] x509Key = ExportX509Key(rsaParameters); //rsa.ImportCspBlob(x509Key); rsa.ImportParameters(rsaParameters); byte[] dataBytes = BitConverter.GetBytes(dataToEncrypt); Array.Resize(ref dataBytes, 8); byte[] encryptedData = rsa.Encrypt(dataBytes, false); string encryptedBase64 = Convert.ToBase64String(encryptedData); return encryptedBase64; } } catch (Exception ex) { Console.WriteLine("Encryption failed: " + ex.Message); } return ""; }
static RSAParameters GetRSAParametersFromKey(string publicKeyValue) { byte[] modulusBytes = Encoding.UTF8.GetBytes(publicKeyValue); Array.Resize(ref modulusBytes, 256); byte[] exponentBytes = { 1, 0, 1 }; RSAParameters rsaParameters = new RSAParameters { Modulus = modulusBytes, Exponent = exponentBytes }; return rsaParameters; }
[ad_2]
コメント