C# - 使用RSA加密和解密数据 [英] C# - Encrypting and Decrypting Data using RSA

查看:291
本文介绍了C# - 使用RSA加密和解密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有以下代码:

I have the following code in C#:

主类

X509Certificate2 cert = new X509Certificate2("C:/test.pfx", "hello", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

            Encryption enc = new Encryption();
            string encrypted = enc.Encrypt("hello there", cert);
            string decrypted = enc.Decrypt(encrypted, cert);
            Console.WriteLine("Encrypted Text: " + encrypted);
            Console.WriteLine("Decrypted Text: " + decrypted);

加密类

public string Encrypt(string plainText, X509Certificate2 cert)
{
    RSACryptoServiceProvider publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
    byte[] encryptedBytes = publicKey.Encrypt(plainBytes, false);
    string encryptedText = encryptedBytes.ToString();
    return encryptedText;
}

public string Decrypt(string encryptedText, X509Certificate2 cert)
{
    RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
    byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedText);
    byte[] decryptedBytes = privateKey.Decrypt(encryptedBytes, false);
    string decryptedText = decryptedBytes.ToString();
    return decryptedText;
}



如你所见,在主类中我正在导入证书。然后我创建一个加密类的实例。然后我通过明文加密方法连同证书,以获得加密的文本。

As you can see, in the main class I am importing a certificate. Then I am creating an instance of the Encryption class. Then I pass plaintext to the Encrypt method along with the certificate in order to get the encrypted text. Afterwards, I pass the encrypted text to the Decrypt method to get the plaintext back.

我的问题是打印加密文本的结果是System。[] Byte()如果我注释掉解密调用)。如果我不注释掉解密调用,我得到一个加密异常:解密方法中的坏数据。

My problem is that the result of printing the encrypted text is System.[]Byte (if I comment out the decryption call). If I do not comment out the decryption call, I get a Cryptographic Exception: Bad Data in the decryption method.

我猜想encryptedBytes数组没有被正确转换串。此外,我不知道如果我正确形成RSAEncryptionProvider。如何解决这个问题?

I guess that the encryptedBytes array is not being converted correctly to string. Furthermore, I am not sure if I am forming the RSAEncryptionProvider correctly. How can I solve this please?

更新

当从字节数组转换为字符串时,我不得不使用Encoding.UTF8.GetString(EncryptedBytes)。现在的问题是,解密方法给我另一个加密异常(要解密的数据超过这个128字节的模数的最大值)。

I solved one issue. When converting from byte array to string, I had to use Encoding.UTF8.GetString(EncryptedBytes). The problem now is that the decrypt method is giving me another cryptographic exception (he data to be decrypted exceeds the maximum for this modulus of 128 bytes).

有人知道为什么这是发生和如何解决它?

Does anybody know why this is happening and how to solve it?

推荐答案

不要将加密的数据作为字符串。加密算法处理二进制数据,并生成二进制数据,不能将其解释为字符串。认为UTF-8或任何其他编码将能够将任何给定的二进制数据块解释为有效的字符串是天真的。

Don't treat encrypted data as a string. Encryption algorithms work on binary data, and produce binary data, which cannot be interpreted as a string. It's naive to think that UTF-8 or any other encoding will be able to interpret any given chunk of binary data as a valid character string.

在你的情况下,如果你需要输出加密的数据到控制台用于调试目的,继续使用 byte [] 并以十六进制形式转储,如下:

In your case, if you need to output encrypted data to the console for debugging purposes, go ahead with byte[] and dump it in hexadecimal, like this:

for (int i = 0; i < data.Length; i++)
{
    Console.Write(data[i].ToString("X2"));
    Console.Write(" ");
    if ((i+1) % 16 == 0) Console.WriteLine();
}

这篇关于C# - 使用RSA加密和解密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆