AES && DES加解密

MD5加密一般不可逆,只能暴力突破。所以这边记录一下一些关于字符串的加解密的两种方法,以便自己学习

AES

   public class AESHelper
{
public static string AESEncrypt(string text)
{
return AESEnc(text, KeyStruct.strKey);
} public static string AESDecrypt(string text)
{
return AESDes(text, KeyStruct.strKey);
}
//加密
private static string AESEnc(string text, string mkey)
{
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Mode = CipherMode.ECB;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.KeySize = ;
rijndael.BlockSize = ;
byte[] pwdBytes = Encoding.Default.GetBytes(mkey);
byte[] keyBytes = new byte[];
int len = pwdBytes.Length > keyBytes.Length ? keyBytes.Length : pwdBytes.Length;
Array.Copy(pwdBytes, keyBytes, len);
rijndael.Key = keyBytes;
using (ICryptoTransform tranform = rijndael.CreateEncryptor())
{
byte[] txtBytes = Encoding.Default.GetBytes(text);
return Convert.ToBase64String(tranform.TransformFinalBlock(txtBytes, , txtBytes.Length));
}
}
}
//解密
private static string AESDes(string text, string key)
{
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Mode = CipherMode.ECB;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.KeySize = ;
rijndael.BlockSize = ;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = Encoding.Default.GetBytes(key);
byte[] keyBytes = new byte[];
int len = pwdBytes.Length > keyBytes.Length ? keyBytes.Length : pwdBytes.Length;
Array.Copy(pwdBytes, keyBytes, len);
rijndael.Key = keyBytes;
using (ICryptoTransform tranform = rijndael.CreateDecryptor())
{
var a = tranform.TransformFinalBlock(encryptedData, , encryptedData.Length);
return Encoding.Default.GetString(a);
}
}
} private struct KeyStruct
{
public static string strKey = "****";
}
}

DES

public class DesHelper
{
private static string desKey = "****";
public static string DesEncrypt(string text)
{
return DesEnc(text, desKey);
}
public static string DesDescript(string text)
{
return DesDes(text, desKey);
}
private static string DesEnc(string text, string dkey)
{
using (DESCryptoServiceProvider descry = new DESCryptoServiceProvider())
{
byte[] txtBytes = Encoding.Default.GetBytes(text);
byte[] keyBytes =ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(dkey, "MD5").Substring(, ));
descry.Key = keyBytes;
descry.IV = keyBytes; //获取或设置对称算法的初始化向量,这边默认向量为key
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, descry.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(txtBytes, , txtBytes.Length);
cs.FlushFinalBlock();
}
//StringBuilder ret = new StringBuilder();
//foreach (byte b in ms.ToArray())
//{
// ret.AppendFormat("{0:X2}", b);
//}
return Convert.ToBase64String(ms.ToArray());
}
}
} private static string DesDes(string text, string dkey)
{
try
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] txtBytes = Convert.FromBase64String(text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(dkey, "MD5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(dkey, "MD5").Substring(, ));
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(txtBytes, , txtBytes.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
上一篇:一个密码经过多次MD5加密能否提高安全性?Java MD5盐值加解密


下一篇:android平台上AES,DES加解密及问题