Hi,
I am trying to encrypt a string then covert it into a base64_encode string to be passed to a website so that it can decrypt it.
My functions below dont seem to be working correctly and it won't decrypt the base64_encode string back to the original string.
Anyone see the problem?
Thanks
public static string EncryptToString(string data, string KeyString, string IVString)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] Data = encoding.GetBytes(data);
byte[] Key = encoding.GetBytes(KeyString);
byte[] IV = encoding.GetBytes(IVString);
try
{
MemoryStream fStream = new MemoryStream(Data);
// Set Rijndael object mode.
Rijndael RijndaelAlg;
RijndaelAlg = Rijndael.Create();
RijndaelAlg.Mode = CipherMode.CBC;
// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Read);
// Create a StreamReader using the CryptoStream.
StreamReader sReader = new StreamReader(cStream);
string encryptedString = null;
try
{
// Read the data from the stream
// to decrypt it.
encryptedString = sReader.ReadToEnd();
// Convert result into a base64-encoded string.
byte[] bytes = encoding.GetBytes(encryptedString);
encryptedString = Convert.ToBase64String(bytes, 0, bytes.Length);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
// Close the streams and
// close the file.
sReader.Close();
cStream.Close();
fStream.Close();
RijndaelAlg.Dispose();
}
// Return the string.
return encryptedString;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
}
public static string DecryptToString(string data, string KeyString, string IVString)
{
ASCIIEncoding encoding = new ASCIIEncoding();
// Convert result from a base64-encoded to string.
byte[] Data = Convert.FromBase64String(data);
byte[] Key = encoding.GetBytes(KeyString);
byte[] IV = encoding.GetBytes(IVString);
try
{
MemoryStream fStream = new MemoryStream(Data);
// Set Rijndael object mode.
Rijndael RijndaelAlg;
RijndaelAlg = Rijndael.Create();
RijndaelAlg.Mode = CipherMode.CBC;
// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create a StreamReader using the CryptoStream.
StreamReader sReader = new StreamReader(cStream);
string decryptedString = null;
try
{
// Read the data from the stream
// to decrypt it.
decryptedString = sReader.ReadToEnd();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
// Close the streams and
// close the file.
sReader.Close();
cStream.Close();
fStream.Close();
RijndaelAlg.Dispose();
}
// Return the string.
return decryptedString;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
}