RSA异常{“Bad Length.\r\\\<br/>”} Noi Matter我的密钥大小是 [英] RSA Exception {"Bad Length.\r\n"} Noi Matter What My Key Size Is
问题描述
我正在收到运行时错误{Bad Length.\r\\\
}:
return rsa.Encrypt(bytes,true);
这是在函数中:
private static byte [] Encrypt(byte [] bytes)
{
using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
string test = Properties.Settings.Default.PublicKeyXml;
rsa.FromXmlString(< RSAKeyValue><模量> mfXS3Na0XfkjhpjS3sL5XcC9o + j6KXi1LB9yBc4SsTMo1Yk / pFsXr74gNj4aRxKB45 + HZH / lSo933NCDEh25du1iMsaH4TGQNkCqi + HDLQjOrdXMMNmaQrLXGlY7UCCfFUnkEUxX51AlyVLzqLycaAt6zm5ljnDXojMC7JoCrTM = LT; /模量><指数> AQAB< /指数>< / RSAKeyFile>中);
return rsa.Encrypt(bytes,true);
}
}
我使用的键大小为8192: p>
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName =XML_ENC_RSA_KEY;
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(8192,cspParams);
string keyXml = rsaKey.ToXmlString(true);
XML文件很小。根据运行时的长度,它只有225个字节:
string fileName = System.IO.Path.Combine(Application。 StartupPath,alphaService.xml);
XDocument doc = new XDocument();
XElement xml = new XElement(Info,
new XElement(DatabaseServerName,txtServerName.Text),
new XElement(DatabaseUserName,txtDatabaseUserName.Text),
新的XElement(DatabasePassword,txtDatabasePassword.Text),
new XElement(ServiceAccount,txtAccount.Text),
new XElement(ServicePassword,txtServicePassword.Text),
new XElement (RegistrationCode,txtRegistrationCode.Text));
doc.Add(xml);
doc.Save(fileName);
//将XML文档转换为字节流
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
byte [] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);
int fileBytesLength = fileBytes.Length;
加密(fileBytes);根据此SO文章,4096字节的关键大小应该已经足够了: ((KeySize - 384)/ 8)+ 7
我必须使用什么关键的大小?为什么8096字节不工作?我如何才能得到这个工作?
解决方案这是一个错误,我发现当我尝试一个或其他的代码帖子。
更改此
byte [] fileBytes = Encoding.Default.GetBytes xmlDoc.OuterXml);
到这个
byte [] fileBytes = Encoding.Default.GetBytes(xmlDoc.ToString());
我还建议您更改你的编码从默认到Encoding.ASCII或更多的定义。将它更容易地转换为字符串以保存,然后再返回到一个字节数组进行解密。
I am getting the runtime error {"Bad Length.\r\n"} on the line:
return rsa.Encrypt(bytes, true);
This is in the function:
private static byte[] Encrypt(byte[] bytes)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
string test = Properties.Settings.Default.PublicKeyXml;
rsa.FromXmlString("<RSAKeyValue><Modulus>mfXS3Na0XfkjhpjS3sL5XcC9o+j6KXi1LB9yBc4SsTMo1Yk/pFsXr74gNj4aRxKB45+hZH/lSo933NCDEh25du1iMsaH4TGQNkCqi+HDLQjOrdXMMNmaQrLXGlY7UCCfFUnkEUxX51AlyVLzqLycaAt6zm5ljnDXojMC7JoCrTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyFile>");
return rsa.Encrypt(bytes, true);
}
}
I am using a key size of 8192:
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_ENC_RSA_KEY";
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(8192, cspParams);
string keyXml = rsaKey.ToXmlString(true);
The XML file is small. According to the length at runtime, it is only 225 bytes:
string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
XDocument doc = new XDocument();
XElement xml = new XElement("Info",
new XElement("DatabaseServerName", txtServerName.Text),
new XElement("DatabaseUserName", txtDatabaseUserName.Text),
new XElement("DatabasePassword", txtDatabasePassword.Text),
new XElement("ServiceAccount", txtAccount.Text),
new XElement("ServicePassword", txtServicePassword.Text),
new XElement("RegistrationCode", txtRegistrationCode.Text));
doc.Add(xml);
doc.Save(fileName);
// Convert XML doc to byte stream
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);
int fileBytesLength = fileBytes.Length;
Encrypt(fileBytes);
According to this SO post, a key size of 4096 bytes should have been sufficient:
((KeySize - 384) / 8) + 7
What key size do I have to use? Why doesn't 8096 bytes work? How can I get this to work?
解决方案 This is the same error I found when I tried the code of one or your other post.
Change this
byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);
to this
byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.ToString());
I would also suggest that you change your encoding from default to Encoding.ASCII or something more defined. It will be easier to convert it to a string to save and then back to a byte array to decrypt.
这篇关于RSA异常{“Bad Length.\r\\\<br/>”} Noi Matter我的密钥大小是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!