Invalid character in a Base-64 string when using Convert.FromBase64String

Link Alcano 1 Reputation point

Hi, I am having this error

Server was unable to process request. ---> Invalid character in a Base-64 string.
I am decrypting an AES - 256 , apparently 24 byte key length is working but when i am trying 32 byte key it's not.
this is how I converting the 32 byte key to byte.

public static string DecryptBySymmetricKey(string encryptedText)
if (String.IsNullOrEmpty(encryptedText))
return encryptedText;
//var keytest =
byte[] dataToDecrypt = Convert.FromBase64String(encryptedText);

        //string keytoByte = Convert.ToBase64();  

        string key = "asdasdasdasdasdasdasdasdasdaasdasdasdasdada==";  

        byte[] keyAsBase64 = Convert.FromBase64String(key);   
        var keyBytes = keyAsBase64;  
        AesManaged tdes = new AesManaged();  
        tdes.KeySize = 256;  
        tdes.BlockSize = 128;  
        //tdes.BlockSize = 256;  
        tdes.Key = keyBytes;  
        tdes.Mode = CipherMode.ECB;  
        tdes.Padding = PaddingMode.PKCS7;  
        ICryptoTransform decrypt__1 = tdes.CreateDecryptor();  
        byte[] deCipher = decrypt__1.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);  
        //string EK_result = Convert.ToBase64String(deCipher);  
        string EK_result = System.Text.Encoding.UTF8.GetString(deCipher);  
        return EK_result;  

Thank you :)

A set of technologies in the .NET Framework for building web applications and XML web services.
4,376 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,619 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Link Alcano 1 Reputation point

    I've already got the solution. I've just added some padding of the '=' for this 32 byte key.

    string keypadded = key.PadRight(key.Length + (4 - key.Length % 4) % 4, '=');

    Thank you :D,

    0 comments No comments

  2. Bruce ( 61,101 Reputation points

    your key:

    string key = "asdasdasdasdasdasdasdasdasdaasdasdasdasdada==";

    is not a valid base64 string. it does not pass validation. so line 6 thows an error

    0 comments No comments