I have written a code for encryption and decryption. Its throwing error "invalid length for a base-64 char array while decrypting the string". My code is below. Please help me to solve this issue.
private byte[] key = { };
private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
private string sEncryptionKey = "r0b1nr0y";
public string Decrypt(string stringToDecrypt)
{
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt.Replace(" ","+"));
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception e)
{
return e.Message;
}
}
public string Encrypt(string stringToEncrypt)
{
try
{
key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception e)
{
return e.Message;
}
}
jack_shivani...
Member
293 Points
75 Posts
invalid length for a base-64 char array while decrypting the string
Nov 19, 2012 01:48 PM|LINK
Hi Friends,
I have written a code for encryption and decryption. Its throwing error "invalid length for a base-64 char array while decrypting the string". My code is below. Please help me to solve this issue.
private byte[] key = { }; private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; private string sEncryptionKey = "r0b1nr0y"; public string Decrypt(string stringToDecrypt) { byte[] inputByteArray = new byte[stringToDecrypt.Length]; try { key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(stringToDecrypt.Replace(" ","+")); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception e) { return e.Message; } } public string Encrypt(string stringToEncrypt) { try { key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception e) { return e.Message; } }ignatandrei
All-Star
137690 Points
22151 Posts
Moderator
MVP
Re: invalid length for a base-64 char array while decrypting the string
Nov 19, 2012 01:52 PM|LINK
1. remove the try/catch and let the error flow
2. how did you tried this code?
BrockAllen
All-Star
28072 Points
4996 Posts
MVP
Re: invalid length for a base-64 char array while decrypting the string
Nov 19, 2012 04:39 PM|LINK
Don't do your own crypto in ASP.NET -- there are built-in helpers for this:
http://tonyarcieri.com/all-the-crypto-code-youve-ever-written-is-probably-broken
http://brockallen.com/2012/06/21/use-the-machinekey-api-to-protect-values-in-asp-net/
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/