Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 2 class
1. crypto class
2. login window class

decryption code is:-
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 decrypt method then give an exception that is Invalid length for a Base-64 char array or string
Posted
Updated 10-Jul-19 18:35pm

For a string to be a valid Base64 string, its length must have a multiple of 4...
Check your input string's length...
http://en.wikipedia.org/wiki/Base64[^]
 
Share this answer
 
Comments
J{0}Y 11-Feb-15 4:33am    
how to check my input string's length
Kornfeld Eliyahu Peter 11-Feb-15 4:42am    
if((encryptedtext.Length % 4) == 0) // good for Base64
J{0}Y 11-Feb-15 6:01am    
http://stackoverflow.com/questions/28450852/invalid-length-for-a-base-64-char-array-or-string
check this link
Kornfeld Eliyahu Peter 11-Feb-15 6:08am    
I did...What about?
J{0}Y 11-Feb-15 6:14am    
actually my question
Change
C#
//byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);
byte[] encryptedBytes = Encoding.ASCII.GetBytes(encryptedtext);

This will eliminate the "Invalid length" error.

Moreover: How can you decrypt without providing a key?
Please have a look here:
Encrypt and Decrypt Data with C#[^]
 
Share this answer
 
Please replace the below function :

public string DecryptData(string encryptedtext)
{
    encryptedtext = encryptedtext.Replace(" ", "+");
    int mod4 = encryptedtext.Length % 4;
    if (mod4 > 0 )
    {
        encryptedtext += new string('=', 4 - mod4);
    }

    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());
}
C#

 
Share this answer
 
v3
byte[] b  = Convert.FromBase64String(CypherText.Replace("", "+"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900