c# - RSA.Cryptography “不正确的参数”抛出 C#
问题描述
我正在尝试为我和我的朋友设置一个混乱的服务器,但我遇到了 RSA 解密的问题。
使用了正确的密钥
如果我启用 OAEP 填充,我会收到一个错误,简单地指出“OAEPpadding”
我对这个错误失去了理智,我在下面发布了脚本。
加密工作正常,只是解密有问题
请帮忙
using System; using System.Net.Sockets; using System.Net; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Security.Cryptography; using System.Xml.Serialization; namespace Server_WIN_ { class Program { public static XmlSerializer xs = new XmlSerializer(typeof(RSAParameters)); public static TcpListener server = new TcpListener(IPAddress.Parse("192.168.1.93"), 78); public static TcpClient client = null; public static NetworkStream canwetalk = null; public static RSACryptoServiceProvider csp = new RSACryptoServiceProvider(4096); public static RSAParameters publickey; public static RSAParameters privatekey; static Program() { server.Start(); csp.PersistKeyInCsp = false; publickey = csp.ExportParameters(false); privatekey = csp.ExportParameters(true); client = server.AcceptTcpClient(); canwetalk = client.GetStream(); } public static void Main(string[] args) { string strHostName = ""; strHostName = Dns.GetHostName(); // Then using host name, get the IP address list.. IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; Random ran = new Random(); HashAlgorithm sha = SHA256.Create(); string msg = ""; byte[] buffer = new byte[4096]; msg = "test"; msg = Encrypt(msg); msg = Decrypt(msg); Console.WriteLine(msg); } public static string PublicKeyString() { byte[] bytes = new byte[4096]; var sw = new StringWriter(); var xs = new XmlSerializer(typeof(RSAParameters)); xs.Serialize(sw, publickey); return sw.ToString(); } public static string PrivateKeyString() { byte[] bytes = new byte[4096]; var sw = new StringWriter(); var xs = new XmlSerializer(typeof(RSAParameters)); xs.Serialize(sw, privatekey); return sw.ToString(); } public static string Encrypt(string msg) { csp.ImportParameters(publickey); byte[] data = System.Text.Encoding.ASCII.GetBytes(msg); byte[] cipher = csp.Encrypt(data, false); return System.Text.Encoding.ASCII.GetString(cipher); } public static string Decrypt(string msg) { try { csp.ImportParameters(privatekey); byte[] decrypted = csp.Decrypt(System.Text.Encoding.ASCII.GetBytes(msg), false); return System.Text.Encoding.Unicode.GetString(decrypted); } catch(CryptographicException e) { string p = e.ToString(); Console.WriteLine(p); } return ""; } public static void ExportPublicKey() { string msg = PublicKeyString(); byte[] buffer = new byte[4096]; byte[] msg1 = System.Text.Encoding.ASCII.GetBytes(msg); canwetalk.Write(msg1, 0, msg1.Length); } public static void ToStream(string msg, bool Encryption) { if (Encryption) { msg = Encrypt(msg); byte[] msgbytes = System.Text.Encoding.ASCII.GetBytes(msg); canwetalk.Write(msgbytes, 0, msgbytes.Length); } else { byte[] msgbytes = System.Text.Encoding.ASCII.GetBytes(msg); canwetalk.Write(msgbytes, 0, msgbytes.Length); } } public static string ReadStream() { byte[] buffer = new byte[4096]; int i = canwetalk.Read(buffer,0,buffer.Length); return System.Text.Encoding.ASCII.GetString(buffer,0,i); }
}
解决方案
您会发现这个 stackoverflow 问题很有帮助,但它已经过时了解码 OAEP 填充时发生错误
推荐阅读
- machine-learning - 回归像量化随机森林中变量的重要性
- reactjs - 在 Jenkins 构建中实现 Jest 测试的正确方法
- node.js - 如何在sails.js 模型中添加具有整数数据类型数组的属性?
- android - 检查 Facebook 上的共享帖子成功或丢弃
- php - 当我尝试绑定值(甚至准备语句)时,为什么这个 SQL 查询返回 false?
- javascript - JSON 处理 - Javascript
- angular-material - 设置角度分量默认值
- ruby-on-rails - 带有反应导轨和设备 JWT 的基本身份验证:401
- java - 如何使用spring将数据从mysql检索到jsp
- hbase - Hbase按插入顺序扫描多个版本