【发布时间】:2023-04-07 00:36:01
【问题描述】:
我正在尝试在我的 C# 程序中实现 RSA 加密。
这是我的代码:
using System;
using System.Security.Cryptography;
string dataToEncrypt = "Data to Encrypt here";
string modulus= "Public Key here";
string exponent= "Public Key Kxpiry here";
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = Encoding.UTF8.GetBytes(modulus);
RSAKeyInfo.D = Encoding.UTF8.GetBytes(dataToEncrypt);
RSAKeyInfo.Exponent = Encoding.UTF8.GetBytes(exponent);
RSA.ImportParameters(RSAKeyInfo);
我收到此错误代码:
System.Security.Cryptography.CryptographicException: 'Bad Data.'
这一行给出了错误:RSA.ImportParameters(RSAKeyInfo);
其他信息:
- Visual Studio 2019
- Windows 窗体应用程序(.NET 框架)
- 操作系统:Windows 10 专业版
我错过了什么/做错了什么?
【问题讨论】:
-
RSAKeyInfo.D = Encoding.UTF8.GetBytes(dataToEncrypt);。好吧,这没有意义,现在是吗?dataToEncrypt不会去那里。你可以试试RSA类的Encrypt()方法。
标签: c# encryption rsa