错误:“Base-64 char数组的长度无效。” [英] Error : "Invalid length for a Base-64 char array."

查看:93
本文介绍了错误:“Base-64 char数组的长度无效。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#用不同的密钥加密和解密纯文本

当我运行此代码时,它会给出错误无效

 rijndaelCipher.Key = Convert.FromBase64String(  =code-string> ABC); 



我也试过

 rijndaelCipher.Key = Encoding.ASCII.GetBytes (  ABC); 
// 但它会出现此错误指定的密钥不是此算法的有效大小。



我的代码是:

 public string EncryptString(string plainText)
{
//实例化一个新的RijndaelManaged对象以执行字符串对称加密
RijndaelManaged rijndaelCipher = new RijndaelManaged();

//设置密钥和IV
rijndaelCipher.Key = Convert.FromBase64String(ABC); //错误在这里

MemoryStream memoryStream = new MemoryStream();
ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream,rijndaelEncryptor,CryptoStreamMode.Write);
byte [] plainBytes = Encoding.ASCII.GetBytes(plainText);
cryptoStream.Write(plainBytes,0,plainBytes.Length);
cryptoStream.FlushFinalBlock();
byte [] cipherBytes = memoryStream.ToArray();

memoryStream.Close();
cryptoStream.Close();

string cipherText = Convert.ToBase64String(cipherBytes,0,cipherBytes.Length);
返回cipherText;
}





每个建议都会有所体现。



问候

Jayanta。

解决方案

通过添加此方法将字符串转换为字节数组解决了这个问题: br />


 私人  static   byte  [] CreateKey( string 密码)
{
var salt = new byte [] { 1 2 23 234 37 48 134 63 248 4 };

const int Iterations = 9872 ;
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(密码,盐,迭代次数);
return rfc2898DeriveBytes.GetBytes( 32 );

}



上一代码是:

 rijndaelCipher.Key = Convert.FromBase64String( ABC); 



解码后的代码为:

 rijndaelCipher.Key = CreateKey(ABC); 


Hi, I want to Encrypt and Decrypt plain Text with C# with different keys.
When I run this code its gives the error Invalid length for a Base-64 char array on

rijndaelCipher.Key = Convert.FromBase64String("ABC");


I also tried

 rijndaelCipher.Key = Encoding.ASCII.GetBytes("ABC");
// but it gives this error "Specified key is not a valid size for this algorithm."


My code is :

public string EncryptString(string plainText)
        {
            // Instantiate a new RijndaelManaged object to perform string symmetric encryption
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            // Set key and IV
            rijndaelCipher.Key = Convert.FromBase64String("ABC"); //Error is here                       

            MemoryStream memoryStream = new MemoryStream();
            ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
            byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherBytes = memoryStream.ToArray();

            memoryStream.Close();
            cryptoStream.Close();

            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
            return cipherText;
        }



Every suggestion would be appreciate.

Regards
Jayanta.

解决方案

Solved this problems by adding this method for converting string to byte array :

private static byte[] CreateKey(string password)
        {
            var salt = new byte[] { 1, 2, 23, 234, 37, 48, 134, 63, 248, 4 };

            const int Iterations = 9872;
            var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, Iterations);
                return rfc2898DeriveBytes.GetBytes(32);

        }


Previous Code is :

rijndaelCipher.Key = Convert.FromBase64String("ABC");


After solved code is :

rijndaelCipher.Key =CreateKey("ABC");


这篇关于错误:“Base-64 char数组的长度无效。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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