c# Desc 和Aes
private static string DescEncrypt(string str)
{
string key = "12345678";///必须是8位
byte[] p_byte_key = System.Text.Encoding.Default.GetBytes(key);
byte[] p_byte_key2 = System.Text.Encoding.Default.GetBytes(key);
byte[] p_byte_data = System.Text.Encoding.Default.GetBytes(str);
try
{
MemoryStream p_stream_ms = new MemoryStream();
CryptoStream p_cryptstream_stream = new CryptoStream(p_stream_ms, new DESCryptoServiceProvider().CreateEncryptor(p_byte_key, p_byte_key2), CryptoStreamMode.Write);
p_cryptstream_stream.Write(p_byte_data, 0, p_byte_data.Length);
p_cryptstream_stream.FlushFinalBlock();
byte[] p_bt_temp = p_stream_ms.ToArray();
string p_str = Convert.ToBase64String(p_bt_temp);
return p_str;
}
catch
{
return null;
}
}
private static string DescDecrypt(string str)
{
try
{
string key = "12345678";///必须是8位
byte[] p_byte_key = System.Text.Encoding.Default.GetBytes(key);
byte[] p_byte_key2 = System.Text.Encoding.Default.GetBytes(key);
byte[] p_byte_data2 = Convert.FromBase64String(str);///变换
MemoryStream p_stream_msold = new MemoryStream(p_byte_data2);///内存
CryptoStream p_cryptstream_stream = new CryptoStream(p_stream_msold, new DESCryptoServiceProvider().CreateDecryptor(p_byte_key, p_byte_key2), CryptoStreamMode.Read);//解密
byte[] p_bt_temp = new byte[200];
MemoryStream p_stream_ms = new MemoryStream();
int i = 0;
///按照内存数组大小读取数据 如果有值那就可以处理了.
while ((i = p_cryptstream_stream.Read(p_bt_temp, 0, p_bt_temp.Length)) > 0)
{
p_stream_ms.Write(p_bt_temp, 0, i);///放到内存里了。
}
string p_strs = Encoding.Default.GetString(p_stream_ms.ToArray());
return p_strs;
}
catch (Exception e)
{
return null;
}
}
////必须是32位byte
public static string AesEncrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="str">明文(待解密)</param>
/// <param name="key">密文 必须是32位byte</param>
/// <returns></returns>
public static string AesDecrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(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);
}

浙公网安备 33010602011771号