无法解密RSA加密的密钥。

3
长话短说,我正在使用DES,并使用RSA进行密钥交换加密密码,密码不超过16个字符。问题在于,当我加密密钥时,加密后的大小变得太大,无法解密。以下是我的RSA加密和解密代码:
加密:--我一直尝试将localpwd设置为“asd”。
    byte[] plaintext = utf8.GetBytes(localpwd);
    byte[] ciphertext = rsaservice.Encrypt(plaintext, false);
    string cipherresult = Convert.ToBase64String(ciphertext);

然后我在文本框中打印加密的密钥并尝试解密。
    byte[] ciphertext = utf8.GetBytes(filetest.Text);
    byte[] plain = rsaservice.Decrypt(ciphertext, true);
    string plaintext = utf8.GetString(plain);

我收到了“要解密的数据超过了256字节的最大模数”错误提示。 我尝试增加密钥大小以便能够加密和解密更大的密钥,但增加密钥只会增加加密数据的大小,导致同样的错误。 请帮忙!


5
我看不到 ToBase64String() 的反转。 - H H
我正在使用utf8编码来将其转换为字节,然后再转换回字符串。 - Adel Ahmed
1个回答

6
//byte[] ciphertext = utf8.GetBytes(filetest.Text);
  byte[] ciphertext = Convert.FromBase64String(filetest.Text);

哇,从来没有想过会这么简单,您真是救了我的命 :D 谢谢。 - Adel Ahmed
我能使用Unicode吗?我正在尝试通过网络流发送信息,但对于Base64字符串无法实现。 - Adel Ahmed
1
我不知道该如何感谢你。 - klaudyuxxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接