求大神帮我把这个用C#写成一个程序。感激不尽
本人新手,求教应该如何将如下代码写成完整的C#程序,用来加密和解密XML文件,代码如下,感谢。报文采用标准的AES加密,加密的密钥: MYgGnQE2+DAS973vd1DFHg==
请查看下面的代码示例进行文件加密
2.2 C#版代码示例
public static class AESHelper
{
/// <summary>
/// 密钥
/// </summary>
private static String Key = "MYgGnQE2+DAS973vd1DFHg==";
/// <summary>
/// AES 解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decrypt(string str)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Convert.FromBase64String(Key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <returns></returns>
public static string Encrypt(string toEncrypt)
{
byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Convert.FromBase64String(Key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
}