Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to decrypt a string which is also base64 encoded, but I am receiving an when I try to decrypt the string.

The error I am receiving is:

{System.FormatException: Invalid length for a Base-64 char array or string.
at this line in the decrypt function below:
MemoryStream ms = new MemoryStream(Convert.FromBase64String(inString));

Encrpyt/Decrypt functions:

//ENCRYPT    
public static bool stringEncrypt(string inString,ref string outstring)
{
    try
    {
        if(String.IsNullOrEmpty(inString)){return false;}
        
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms,provider.CreateEncryptor(PWbytes,PWbytes),CryptoStreamMode.Write);
        StreamWriter sw = new StreamWriter(cs);
        
        sw.Write(inString);
        sw.Flush();
        cs.FlushFinalBlock();
        sw.Flush();
        
        outstring = Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
        
        return true;
    }
    catch(Exception ex)
    {
        clsCommonBase.AppendToExceptionFile("Encrypt : " + ex.Message);
        return false;
    }
}
        
//DECRPYT
public static bool stringDecrypt(string inString,ref string outstring)
{
    try
    {
        if(String.IsNullOrEmpty(inString)){return false;};
        
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        
        MemoryStream ms = new MemoryStream(Convert.FromBase64String(inString));
        CryptoStream cs = new CryptoStream(ms, provider.CreateDecryptor(PWbytes,PWbytes),CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cs);
        
        outstring = sr.ReadToEnd();
        
        return true;
    }
    catch(Exception ex)
    {
        clsCommonBase.AppendToExceptionFile("Decrypt : " + ex.Message);
        return false;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

Solved using the simple solution in the following link How do I encode and decode a base64 string?

Also added some code in the encoding function to ensure the plain text string will be converted to a valid length base64 string.

Code:

public static string Base64Encode(string plainText)
{

            //check plain text string and pad if needed
            int mod4 = plainText.Length % 4;
            if (mod4 > 0)
            {
                plainText += new string('=', 4 - mod4);
            }

            //convert to base64 and return
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }

        public static string Base64Decode(string base64EncodedData)
        {         
            //decode base64 and return as string
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...