RSA异常{&QUOT;坏Length.\r\\\<br/>&QUOT;}河内不管我的密钥大小 [英] RSA Exception {&quot;Bad Length.\r\n&quot;} Noi Matter What My Key Size Is

查看:366
本文介绍了RSA异常{&QUOT;坏Length.\r\\\<br/>&QUOT;}河内不管我的密钥大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到运行时错误{坏Length.\r\\\
}就行了:

I am getting the runtime error {"Bad Length.\r\n"} on the line:

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><Modulus>mfXS3Na0XfkjhpjS3sL5XcC9o+j6KXi1LB9yBc4SsTMo1Yk/pFsXr74gNj4aRxKB45+hZH/lSo933NCDEh25du1iMsaH4TGQNkCqi+HDLQjOrdXMMNmaQrLXGlY7UCCfFUnkEUxX51AlyVLzqLycaAt6zm5ljnDXojMC7JoCrTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyFile>");
                return rsa.Encrypt(bytes, true);
            }
        }



我使用的8192的密钥大小:

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);



XML文件是小的。据在运行时的长度,它只有225个字节:

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);



据的这个SO帖子,4096字节的密钥大小应该已经足够了:

According to this SO post, a key size of 4096 bytes should have been sufficient:

((KeySize - 384) / 8) + 7

我有什么密钥大小使用?为什么没有8096字节的工作吗?我怎样才能得到这个工作?

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.

更​​改此

字节[] = fileBytes Encoding.Default.GetBytes( xmlDoc.OuterXml);

byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.OuterXml);

这个

字节[] = fileBytes Encoding.Default.GetBytes(xmlDoc.ToString());

byte[] fileBytes = Encoding.Default.GetBytes(xmlDoc.ToString());

我也建议你改变从默认Encoding.ASCII或某事您的编码更明确。它会更容易将其转换为一个字符串保存,然后回一个字节数组解密。

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异常{&QUOT;坏Length.\r\\\<br/>&QUOT;}河内不管我的密钥大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆