1
我想加密一个字符串,然后将其转换成一个base64_encode字符串以传递给一个网站,以便它可以解密它。使用AES和base64_encode的问题结果
我下面的函数似乎没有正常工作,它不会将base64_encode字符串解密回原始字符串。
任何人都看到问题了吗?
感谢
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;
}
}
非常感谢,做了一个梦! – arbme 2010-07-22 19:26:55