Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

When I am running the decryption method it is showing the exception as
"Invalid length for a Base-64 char array "

byte[] toEncryptArray = Convert.FromBase64String(cipherString);
byte[] toEncryptArray = Convert.FromBase64String(cipherString.Replace("", "+"));

The above 2 statements are not working


Please give the proper solution for the above problem


Thanks in advance
Posted
Updated 13-Jun-13 2:31am
v2
Comments
Prasad_Kulkarni 13-Jun-13 8:30am    
Can you post your code snippets.?
Member 8557048 13-Jun-13 23:16pm    
byte[] keyArray;
//get the byte code of the string

//byte[] toEncryptArray = Convert.FromBase64String(cipherString);
byte[] toEncryptArray = Convert.FromBase64String(cipherString.Replace("", "+"));

System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
//Get your key from config file to open the lock!
//string key = (string)settingsReader.GetValue("SecurityKey",
// typeof(String));
string key = "123";

if (useHashing)
{
//if hashing was used get the hash code with regards to your key
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//release any resource held by the MD5CryptoServiceProvider

hashmd5.Clear();
}
else
{
//if hashing was not implemented get the byte code of the key
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)

tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray, 0, toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//return the Clear decrypted TEXT
return UTF8Encoding.UTF8.GetString(resultArray);
Member 8557048 13-Jun-13 23:17pm    
The above mentioned is the method that i am using
Zoltán Zörgő 13-Jun-13 9:02am    
1) Base64 encoding is not encryption
2) What's in cipherString? Give an example!
3) What has this to do with stored procedures or MVC? Please correct the tags!

The error means what it says. Your string is not valid, it's not even the right length.
 
Share this answer
 
.FromBase64String() is a function that takes a specially formatted string. This string is basically data encoded in base 64, you can't pass any old string into this function and get out a byte array.

In order to get a byte array from a string, use the following function:

C#
System.Text.ASCIIEncoding.ASCII.GetBytes(cipherString)


Also, I don't know what you are .Replacing in the string, since the first replace string is empty, it will throw an ArgumentException.
 
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