AES加密解密
2015-12-20 04:22:39 访问(1128) 赞(0) 踩(0)
相关下载:百度网盘 SlowX.AESEncryptOperApp[code] SlowX.AESEncryptOperApp[release]
-

-
#region AES 加密
/// <summary>
/// 默认密钥向量
/// </summary>
private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES 加密
/// </summary>
/// <param name="EncryptKey"></param>
/// <param name="str">明文</param>
/// <param name="_encoding"></param>
/// <returns></returns>
public static string AESEncrypt
(
string EncryptKey,
string str,
System.Text.Encoding _encoding
)
{
// 分组加密算法
SymmetricAlgorithm des = Rijndael.Create();
// 得到需要加密的字节数组
byte[] inputByteArray = _encoding.GetBytes(str);
//设置密钥及密钥向量
des.Key = _encoding.GetBytes(EncryptKey);
des.IV = _key1;
byte[] cipherBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
}
}
return Convert.ToBase64String(cipherBytes);
}
#endregion AES 加密
#region AES 解密
/// <summary>
/// AES 解密
/// </summary>
/// <param name="EncryptKey"></param>
/// <param name="str"></param>
/// <param name="_encoding"></param>
/// <returns></returns>
public static string AESDecrypt
(
string EncryptKey,
string str,
System.Text.Encoding _encoding
)
{
byte[] cipherText = Convert.FromBase64String(str);
SymmetricAlgorithm des = Rijndael.Create();
des.Key = _encoding.GetBytes(EncryptKey);
des.IV = _key1;
byte[] decryptBytes = new byte[cipherText.Length];
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
}
}
// 将字符串后尾的'\0'去掉
return _encoding.GetString(decryptBytes).Replace("\0", "");
}
#endregion AES 解密
/// <summary>
/// 字符串转成byte[]数组的String
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToByteArrayString(string str)
{
if (str == null || str.Length == 0)
return "";
byte[] byteArray = Encoding.Default.GetBytes(str);
return ByteArrayToString(byteArray);
}
/// <summary>
/// 字符串转成byte[]数组的String
/// </summary>
/// <param name="str"></param>
/// <param name="_encoding"></param>
/// <returns></returns>
public static string StringToByteArrayString(string str, System.Text.Encoding _encoding)
{
if (str == null || str.Length == 0)
return "";
if (_encoding == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:System.Text.Encoding _encoding 为null。"
);
}
byte[] byteArray = _encoding.GetBytes(str);
return ByteArrayToString(byteArray);
}
/// <summary>
///
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
public static string ByteArrayToString(byte[] byteArray)
{
if (byteArray == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:byte[] byteArray 为null。"
);
}
StringBuilder theResult = new StringBuilder();
foreach (byte b in byteArray)
{
theResult.Append(b.ToString().PadLeft(3, '0'));
}
return theResult.ToString();
}
/// <summary>
/// 字符串转成byte[]数组
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] StringToByteArray(string str)
{
if (str == null || str.Length == 0)
return null;
int iLen = str.Length;
if (iLen % 3 != 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "iLen % 3 != 0,传入的字符串不是合理的byte[]值。"
);
}
int iArrayLen = iLen / 3;
byte[] theResult = new byte[iArrayLen];
string strItem = string.Empty;
byte bItem = 1;
for (int i = 0; i < iArrayLen; ++i)
{
strItem = str.Substring(i * 3, 3);
bItem = byte.Parse(strItem);
theResult[i] = bItem;
}
return theResult;
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ByteArrayStringToString(string str)
{
byte[] bArray = StringToByteArray(str);
return Encoding.Default.GetString(bArray);
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="_encoding"></param>
/// <returns></returns>
public static string ByteArrayStringToString(string str, System.Text.Encoding _encoding)
{
if (_encoding == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:System.Text.Encoding _encoding 为null。"
);
}
byte[] bArray = StringToByteArray(str);
return _encoding.GetString(bArray);
}
标签:
AES加密解密 


上一条:
下一条:
相关评论
发表评论