【发布时间】:2020-11-23 06:49:40
【问题描述】:
我正在尝试为我和我的朋友设置一个混乱的服务器,但遇到了 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); }}
【问题讨论】:
-
return System.Text.Encoding.ASCII.GetString(cipher);这永远行不通。加密的结果是一个不代表有效 ASCII 编码字符串的任意字节序列。解码器将简单地用有效的东西替换不可能的编码并继续前进。结果是损坏的数据。字节数组 (byte[]) 没有任何问题,它们可以轻松地在函数之间和通过网络流传递。但是,如果您必须转换为字符串,请使用适当的编码,例如 base64。 -
谢谢詹姆斯,你说得对,我不知道为什么我试图将它解析为字符串。
标签: c# encryption rsa sos