c# aes,des,md5加密等解密算法

一:可逆加密,即是能加密也能解密

  1. 对称可逆加密:加密后能解密回原文,加密key和解密key是一个
  2. 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文
  3. 加密解密的速度快,问题是密钥的安全

1:DES加密解密

 /// <summary>
/// DES AES Blowfish
///  对称加密算法的优点是速度快,
///  缺点是密钥管理不方便,要求共享密钥。
/// 可逆对称加密 密钥长度8
/// </summary>
public class DesEncrypt
{
private static byte[] _rgbKey = ASCIIEncoding.ASCII.GetBytes("");
private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes("AES00000");
/// <summary>
/// DES 加密
/// </summary>
/// <param name="text">需要加密的值</param>
/// <returns>加密后的结果</returns>
public static string Encrypt(string text)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(crypStream);
sWriter.Write(text);
sWriter.Flush();
crypStream.FlushFinalBlock();
memStream.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), , (int)memStream.Length);
}
} /// <summary>
/// DES解密
/// </summary>
/// <param name="encryptText"></param>
/// <returns>解密后的结果</returns>
public static string Decrypt(string encryptText)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
byte[] buffer = Convert.FromBase64String(encryptText); using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
crypStream.Write(buffer, , buffer.Length);
crypStream.FlushFinalBlock();
return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
}
}
}

2:AES加密解密

c#代码:

 public class AesEncrypts
{
/// <summary>
/// AES 加密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesEncrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
byte[] ivs = new byte[];
RijndaelManaged rm = new RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.CBC, //位置偏移,如果是ECB则不需要ivs
Padding = PaddingMode.PKCS7,
IV=ivs
}; ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Convert.ToBase64String(resultArray, , resultArray.Length);
} /// <summary>
/// AES 解密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesDecrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);
byte[] ivs = new byte[];
RijndaelManaged rm = new RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode =CipherMode.CBC,
Padding =PaddingMode.PKCS7,
IV=ivs
}; ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray);
}
}

java代码:

 //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package com.lvmama.intlflight.api.common.util; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class AESUtil {
public AESUtil() {
} public static String encrypt(String sSrc, String sKey) throws Exception {
if(sKey == null) {
System.out.print("Key为空null");
return null;
} else if(sKey.length() != ) {
System.out.print("Key长度不是16位");
return null;
} else {
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] ivs = new byte[];
IvParameterSpec iv = new IvParameterSpec(ivs);
cipher.init(, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
return (new BASE64Encoder()).encode(encrypted);
}
} public static String decrypt(String sSrc, String sKey) throws Exception {
try {
if(sKey == null) {
System.out.print("Key为空null");
return null;
} else if(sKey.length() != ) {
System.out.print("Key长度不是16位");
return null;
} else {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] ivs = new byte[];
IvParameterSpec iv = new IvParameterSpec(ivs);
cipher.init(, skeySpec, iv);
byte[] encrypted1 = (new BASE64Decoder()).decodeBuffer(sSrc); try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception var10) {
System.out.println(var10.toString());
return null;
}
}
} catch (Exception var11) {
System.out.println(var11.toString());
return null;
}
} public static void main(String[] args) throws Exception {
String cKey = "";
String cSrc = "abcdefghigklmnopqrstuvwxyz0123456789";
System.out.println(cSrc);
long lStart = System.currentTimeMillis();
String enString = encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
lStart = System.currentTimeMillis();
String DeString = decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
}

二:不可逆加密,即是没有办法解密,其特点为如下几个:

  • 相同原文加密的结果是一样的
  • 不同长度的内容加密后加过都是32位
  • 原文差别很小,结果差别很大
  • 不管文件多大,都能产生32位长度摘要

1:MD5加密代码:

  #region MD5
/// <summary>
/// MD5加密,和动网上的16/32位MD5加密结果相同,
/// 使用的UTF8编码
/// </summary>
/// <param name="source">待加密字串</param>
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string Encrypt(string source, int length = )//默认参数
{
if (string.IsNullOrEmpty(source)) return string.Empty;
HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
byte[] bytes = Encoding.UTF8.GetBytes(source);//这里需要区别编码的
byte[] hashValue = provider.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case ://16位密文是32位密文的9到24位字符
for (int i = ; i < ; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
case :
for (int i = ; i < ; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = ; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
return sb.ToString();
}
#endregion MD5

2:MD5的用途

A:防篡改:
//发个文档,事先给别人一个MD5,是文档的摘要,
//源代码管理器svn--即使电脑断网了,文件有任何改动都能被发现--本地存了一个文件的MD5--文件有更新,就再对比下MD5
//急速秒传--扫描文件的MD5--跟已有的文件MD5比对--吻合表示文件已存在不用再上传

B: 密码保存,防止看到明文
//密码应该只有用户知道----数据库不能存明文---但是又需要验证
//MD5加密下原始密码---数据库存密文---下次登录把密码MD5后再比对
//密文是可见的,所以要求密码不能太简单,加盐(123456+wss)

C 防止抵赖,数字签名
//把一些内容摘要一下,由权威的第三方去保障,将来这个文件就是你做的,不能抵赖

  #region MD5摘要
/// <summary>
/// 获取文件的MD5摘要
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string AbstractFile(string fileName)
{
using (FileStream file = new FileStream(fileName, FileMode.Open))
{
return AbstractFile(file);
}
}
/// <summary>
/// 根据stream获取文件摘要
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static string AbstractFile(Stream stream)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(stream); StringBuilder sb = new StringBuilder();
for (int i = ; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
#endregion
上一篇:Android.20.OKHttp


下一篇:Java学习路线_Java知识分享