Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i am using this code to Encrypt and Decrypt password but giving error in decrypt Portion "invalid length for a base-64 char array or string" on line "byte[] cipherBytes = Convert.FromBase64String(cipherText);".

C#
private string Encrypt(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }

    private string Decrypt(string cipherText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }


please help...........
Posted
Comments
CPallini 17-Oct-14 3:34am    
The posted code doesn't show how to use the methods.
Member 11014751 17-Oct-14 3:49am    
Com.Parameters.AddWithValue("@Password", Decrypt(Login1.Password));
Member 11014751 17-Oct-14 4:16am    
please help
Member 11014751 21-Oct-14 7:01am    
problem Still not Solved it is giving error "invalid length for a base-64 char array or string"

 
Share this answer
 
Comments
Member 11014751 21-Oct-14 7:00am    
problem Still not Solved it is giving error "invalid length for a base-64 char array or string"
C#
string strKey = "12345@password.com";
try
{
string strEncrypt = Encrypt(strKey); //Encrypt1(HttpUtility.UrlEncode(strKey));
string strDecrypt = Decrypt(strEncrypt);//Decrypt1(HttpUtility.UrlDecode(strEncrypt));
 if (strEncrypt == strDecrypt)
{
 Response.Write("String Matched");
}
else
 {
 Response.Write("String Not  Matched");
}
}
 catch(Exception e1)
{
}


Result Set::

C#
strKey = "12345@password.com";
strEncrypt="Encrypted String";
strDecrypt="12345@password.com";

If facing Problem With the StringKey code..then try this.

Encrypt(HttpUtility.UrlEncode(strKey));
Decrypt(HttpUtility.UrlDecode(strEncrypt));
 
Share this answer
 
v2
Comments
Member 11014751 21-Oct-14 7:00am    
problem Still not Solved it is giving error "invalid length for a base-64 char array or string"
/\jmot 21-Oct-14 7:23am    
i think you are trying to decrypt the code strkry="12345@passsword.com"??
try to decrypt the encrypted code.
MukeshSagar 22-Oct-14 2:47am    
No this is the key on the basis of which the data is encrypted
/\jmot 22-Oct-14 3:31am    
show me...
how you are calling these method???

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