Problem on encryption and decryption

  • Thread starter Stephanie Yao via .NET 247
  • Start date
S

Stephanie Yao via .NET 247

Hi, I've got some problem on encryption and decryption,here is my code:

public string Encrypt(string eptData)
{
MemoryStream ms = new MemoryStream();

transformer.IV = initVec;
transformer.Key = encKey;
ICryptoTransform transform = transformer.GetCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms,
transform,
CryptoStreamMode.Write);

//??here is the problem, the rest code are not performed, it said System.FormatException:Base-64 char array invalid length
try
{
byte[] eptBytes = Convert.FromBase64String(eptData);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}

try
{
encStream.Write(eptBytes, 0, eptBytes.Length);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
encStream.FlushFinalBlock();
try
{
encStream.Close();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}

string eptResult = Convert.ToBase64String(ms.ToArray());
return eptResult;
}

anyone can help me?
 
J

Jon Skeet [C# MVP]

Stephanie Yao via .NET 247 said:
Hi, I've got some problem on encryption and decryption,here is my code:

public string Encrypt(string eptData)
{
MemoryStream ms = new MemoryStream();

transformer.IV = initVec;
transformer.Key = encKey;
ICryptoTransform transform = transformer.GetCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms,
transform,
CryptoStreamMode.Write);

//??here is the problem, the rest code are not performed, it said
System.FormatException:Base-64 char array invalid length

So it looks like it's actually not an encryption problem at all -
you're just passing it a string which *isn't* a base-64 encoded binary,
and trying to decode it.

Is your string *meant* to be base-64 encoded binary?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top