简体   繁体   English

在 C# 中成功 RSA 加密数据

[英]Successfully RSA Encrypting data in C#

I am trying to implement RSA Encryption into my C# program.我正在尝试在我的 C# 程序中实现RSA 加密

Here is my code :这是我的代码:

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);

I receive this error code :我收到此错误代码:

System.Security.Cryptography.CryptographicException: 'Bad Data.'

This line is giving the error : RSA.ImportParameters(RSAKeyInfo);这一行给出了错误: RSA.ImportParameters(RSAKeyInfo);

Other information :其他信息:

  • Visual Studio 2019视觉工作室 2019
  • Windows Forms App (.NET Framework) Windows 窗体应用程序(.NET 框架)
  • Operating System : Windows 10 Professional操作系统:Windows 10 专业版

What am I missing/doing wrong?我错过了什么/做错了什么?

RSA key generation is a very specific process, which relies or two (or more) prime numbers. RSA 密钥生成是一个非常具体的过程,它依赖于或两个(或更多)素数。 You cannot just pick random values for RSA to be secure.您不能只是为 RSA 选择随机值来保证安全。 Then again, RSA relies on modular exponentiation during encryption, and that may still "work", ie generate an insecure answer.再说一次,RSA 在加密过程中依赖于模取幂,这可能仍然“有效”,即生成不安全的答案。

CSP (cryptographic service providers) may however pose additional restrictions.然而,CSP(加密服务提供商)可能会带来额外的限制。 For instance, the Microsoft CSP has the following restriction regarding key size:例如,Microsoft CSP 对密钥大小有以下限制:

Windows CSPs enable key sizes of 384 to 16384 bits for Windows versions prior to Windows 8.1, and key sizes of 512 to 16384 bits for Windows 8.1. Windows CSP 为 Windows 8.1 之前的 Windows 版本启用 384 到 16384 位的密钥大小,为 Windows 8.1 启用 512 到 16384 位的密钥大小。

As the key size is specified to be the size of the modulus.由于密钥大小被指定为模数的大小。 Furthermore, the key size must be a multiple of 8 as well.此外,密钥大小也必须是 8 的倍数。 This means that the most significant bit in the byte array must be set to 1, and this is never the case for UTF-8.这意味着字节数组中的最高有效位必须设置为 1,而 UTF-8 则不是这种情况。

Furthermore there may be restrictions on the public exponent as well;此外,对公共指数也可能有限制; for the Windows CSP it needs to be smaller than 32 bits.对于 Windows CSP,它需要小于 32 位。 Generally it is simply set to the value 010001 hex (65537 or the fifth prime of Fermat, called F4).通常它只是简单地设置为值 010001 十六进制(65537 或费马的第五个质数,称为 F4)。


The CNG implentation of RSA seems more flexible, so you may try that as well if you want to test some specifics of RSA. RSACNG 实现似乎更灵活,因此如果您想测试 RSA 的某些细节,也可以尝试这样做。

However, in the end you need to use the RSA key pair generation procedures to create a valid RSA key pair;但是,最终您需要使用 RSA 密钥对生成程序来创建有效的 RSA 密钥对; nothing else will be secure.没有其他东西是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM