static string Password = "XXXXX";
public static string Decrypt(string source)
{
string s;
if (String.IsNullOrEmpty(source))
throw new ArgumentException("No data given");
byte[] inputData = Convert.FromBase64String(source);
if (inputData.Length < 8)
throw new ArgumentException("Invalid input data");
byte[] salt = new byte[8];
for (int i = 0; i < salt.Length; i++)
{
salt[i] = inputData[i];
}
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(Password, salt);
Rijndael rijndael = Rijndael.Create();
rijndael.Padding = PaddingMode.ISO10126;
rijndael.IV = rfc2898DeriveBytes.GetBytes(rijndael.BlockSize / 8);
rijndael.Key = rfc2898DeriveBytes.GetBytes(rijndael.KeySize / 8);
using (MemoryStream memoryStream = new MemoryStream())
using (
CryptoStream cryptoStream =
new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(inputData, 8, inputData.Length - 8);
cryptoStream.Close();
byte[] bytes = memoryStream.ToArray();
s = Encoding.Unicode.GetString(bytes);
}
return s;
}
对加密解密这块不熟,请有经验的朋友解答,谢谢。public static string Decrypt(string source)
{
string s;
if (String.IsNullOrEmpty(source))
throw new ArgumentException("No data given");
byte[] inputData = Convert.FromBase64String(source);
if (inputData.Length < 8)
throw new ArgumentException("Invalid input data");
byte[] salt = new byte[8];
for (int i = 0; i < salt.Length; i++)
{
salt[i] = inputData[i];
}
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(Password, salt);
Rijndael rijndael = Rijndael.Create();
rijndael.Padding = PaddingMode.ISO10126;
rijndael.IV = rfc2898DeriveBytes.GetBytes(rijndael.BlockSize / 8);
rijndael.Key = rfc2898DeriveBytes.GetBytes(rijndael.KeySize / 8);
using (MemoryStream memoryStream = new MemoryStream())
using (
CryptoStream cryptoStream =
new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(inputData, 8, inputData.Length - 8);
cryptoStream.Close();
byte[] bytes = memoryStream.ToArray();
s = Encoding.Unicode.GetString(bytes);
}
return s;
}
如果需要,我有几个样例数据与加密后的结果。