简体   繁体   中英

Invalid length for a Base-64 char array or string

Here are my encryption and decryption methods. I have two databases and I copied the encrypted password from one database to the other. The code was in vb but I converted it into C#.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Security.Cryptography;
 using System.IO;
 namespace AccountSystem.Class{
class ClEncrDecr
{
    private TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();

    private byte[] TruncateHash(string key, int length)
    {
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        //Hash the Key
        byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
        byte[] hash = sha1.ComputeHash(keyBytes);

        // truncate or pad the hash
        Array.Resize(ref hash, length);
        return hash;
    }

    public ClEncrDecr()
    {
        string key = "ABCD";
        tripleDESCryptoServiceProvider.Key = TruncateHash(key, tripleDESCryptoServiceProvider.KeySize / 8 );
        tripleDESCryptoServiceProvider.IV = TruncateHash("", tripleDESCryptoServiceProvider.BlockSize / 8 );
    }

    public string EncryptData(string plainText)
    {
        byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plainText);
        MemoryStream ms = new MemoryStream();
        CryptoStream encStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
        encStream.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }

    public string DecryptData(string encryptedtext)
    {
        byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
        MemoryStream ms = new MemoryStream();
        CryptoStream decStream = new CryptoStream(ms, tripleDESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
        decStream.FlushFinalBlock();
        return System.Text.Encoding.Unicode.GetString(ms.ToArray());
    }
}
}

Login Code :

MessageBox.Show(crypto.DecryptData(obj.password))

When we call DecryptData(string encryptedtext) it throws an exception saying Invalid length for a Base-64 char array or string . What can I do?

If you have the following encrypted Password:

dfghfgdfgd667878nnvghv

It can't be converted to a byte array from Base64, because it's not a valid Base64String. A valid Base64String would be:

dfghfgdfgd667878nnvghv==

As Claudio mentioned in the comment, your encryptedtext variable is not a base64 encoded string, perhaps it's at least missing padding character(s) at the end.

It's not visible from the example how it's created, but you might want to look for example this SO question: How do I encode and decode a base64 string?

About padding: http://en.wikipedia.org/wiki/Base64#Padding

I had this exact issue. Turned out that I had forgot to increase the varchar size of both my columns in the database. Resulted in the values being truncated. Never rule out your own stupidity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM