-1

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;
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • Well have you examined the string you're passing in? Presumably it's not valid base64... my guess is that it's not the result of a previous call to `passwordEncrypt`. (I'd strongly encourage you to revisit your exception handling, btw... this is not an idiomatic way of handling exceptions in C#.) – Jon Skeet Jul 07 '17 at 10:25
  • https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string – Vladimir Arustamian Jul 07 '17 at 10:25
  • 2
    Please post an [mcve] and the exact string you are trying to encrypt/decrypt. – Mike Nakis Jul 07 '17 at 10:26
  • 2
    You should not be using DESCryptoServiceProvider for anything where security is important, if you do really need to encrypt rather than hash use AES. You are also using the same values or the key & IV which breaks it even more. – Alex K. Jul 07 '17 at 10:28
  • So how would I check that it is validate before I try to convert it? and if it isn't valid, how do I make it valid? – Paul Alexander Jul 07 '17 at 10:31
  • Paul, I gave you a link earlier which answers the question "How do I encode and decode a base64 string?" – Vladimir Arustamian Jul 07 '17 at 10:32
  • Thank you Vladimir! I will give it a try now – Paul Alexander Jul 07 '17 at 10:34
  • @VladimirArustamian still no luck with this I'm afraid. I tried the solution in the link but still received the invalid length error – Paul Alexander Jul 07 '17 at 10:46
  • You will need to take the time to understand Base64, see [Base64](https://en.wikipedia.org/wiki/Base64). Then you will be able to determine if the string is a valid Base64 encoding. – zaph Jul 07 '17 at 12:32
  • 1
    **Future reader warning:** DES should not be used for new work, it is not secure and has been superseded by AES, use AES instead. Old work should be migrated to AES as soon as possible. – zaph Jul 07 '17 at 12:38
  • 1
    Solved using @VladimirArustamian's link. Will post solution code when possible. – Paul Alexander Jul 07 '17 at 12:45

1 Answers1

1

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);
        }
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69