我想用RSA加密和解密帶有小字符串的文件。 加密字符串後,程序寫入文件私鑰和加密文本。RSA解密錯誤
代碼加密:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096);
string pub = rsa.ToXmlString(false);
string priv = rsa.ToXmlString(true);
string to_crypt = "Hello world, try to crypt me";
byte[] dataToEncrypt = Encoding.ASCII.GetBytes(to_crypt);
rsa.FromXmlString(pub);
dataToEncrypt = rsa.Encrypt(dataToEncrypt, false);
string s = System.Text.Encoding.UTF8.GetString(dataToEncrypt, 0, dataToEncrypt.Length);
WriteFile("crypt", priv+s);
另一個程序在陣列讀取文本並試圖解密:
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096))
{
byte[] to_decrypt = File.ReadAllBytes(args[0]);
byte[] key = new byte[3219];
byte[] text = new byte[to_decrypt.Length - key.Length];
Buffer.BlockCopy(to_decrypt, 0, key, 0, key.Length);
Buffer.BlockCopy(to_decrypt, key.Length, text, 0, text.Length);
string skey = System.Text.Encoding.UTF8.GetString(key, 0, key.Length);
rsa.FromXmlString(skey);
rsa.Decrypt(text, false); <---- Error: Bad data
...
我有錯誤:
CryptographicException:Bad data
我犯的錯誤文本數組?錯誤的數組大小?
您還需要發佈加密代碼 –