AES加密解密 SHA1、SHA加密 MD5加密
二话不说马上附上代码:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.luo.util; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.UnsupportedEncodingException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.security.InvalidKeyException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.security.MessageDigest; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.security.NoSuchAlgorithmException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.security.SecureRandom; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.BadPaddingException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.Cipher; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.IllegalBlockSizeException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.KeyGenerator; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.NoSuchPaddingException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.SecretKey; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> javax.crypto.spec.SecretKeySpec; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">DecriptUtil</span> {</span> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span>:罗国辉 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @date</span>: 2015年12月17日 上午9:16:22 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @description</span>: AES加密 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @parameter</span>: str:待加密字符串,secretKeyBase:用于生成密钥的基础字符串 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>: 加密字节数组 **/</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] <span class="hljs-title" style="box-sizing: border-box;">encryptAES</span>(String str, String secretKeyBase) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { KeyGenerator kgen = KeyGenerator.getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>); kgen.init(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SecureRandom(secretKeyBase.getBytes())); SecretKey secretKey = kgen.generateKey(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SecretKeySpec(enCodeFormat, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>); Cipher cipher = Cipher.getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建密码器</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] byteContent = str.getBytes(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"UTF-8"</span>); cipher.init(Cipher.ENCRYPT_MODE, key);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] result = cipher.doFinal(byteContent); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> result; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 加密</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchAlgorithmException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchPaddingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (InvalidKeyException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (UnsupportedEncodingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IllegalBlockSizeException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (BadPaddingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span>:罗国辉 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @date</span>: 2015年12月17日 上午9:16:22 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @description</span>: AES解密 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @parameter</span>: strByteArray:待解密字节数组, *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @parameter</span>: secretKeyBase:用于生成密钥的基础字符串, 需要注意的是EAS是对称加密,所以secretKeyBase在加密解密时要一样的 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>: 解密后字符串 **/</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> String <span class="hljs-title" style="box-sizing: border-box;">decryptAES</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] strByteArray, String secretKeyBase) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { KeyGenerator kgen = KeyGenerator.getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>); kgen.init(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SecureRandom(secretKeyBase.getBytes())); SecretKey secretKey = kgen.generateKey(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SecretKeySpec(enCodeFormat, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>); Cipher cipher = Cipher.getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"AES"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建密码器</span> cipher.init(Cipher.DECRYPT_MODE, key);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化</span> String result = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> String(cipher.doFinal(strByteArray),<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"UTF-8"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> result; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 加密</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchAlgorithmException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchPaddingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (InvalidKeyException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IllegalBlockSizeException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (BadPaddingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (UnsupportedEncodingException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span>:罗国辉 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @date</span>: 2015年12月17日 上午9:24:43 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @description</span>: SHA、SHA1加密 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @parameter</span>: str:待加密字符串 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>: 加密串 **/</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> String <span class="hljs-title" style="box-sizing: border-box;">SHA1</span>(String str) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { MessageDigest digest = java.security.MessageDigest .getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SHA-1"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果是SHA加密只需要将"SHA-1"改成"SHA"即可</span> digest.update(str.getBytes()); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span> messageDigest[] = digest.digest(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Create Hex String</span> StringBuffer hexStr = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuffer(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 字节数组转换为 十六进制 数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (shaHex.length() < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) { hexStr.append(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); } hexStr.append(shaHex); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> hexStr.toString(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchAlgorithmException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @author</span>:罗国辉 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @date</span>: 2015年12月17日 上午9:24:43 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @description</span>: MD5加密 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @parameter</span>: str:待加密字符串 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span>: 加密串 **/</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> String <span class="hljs-title" style="box-sizing: border-box;">MD5</span>(String str) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得MD5摘要算法的 MessageDigest 对象</span> MessageDigest mdInst = MessageDigest.getInstance(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"MD5"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用指定的字节更新摘要</span> mdInst.update(str.getBytes()); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获得密文</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">byte</span>[] md = mdInst.digest(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 把密文转换成十六进制的字符串形式</span> StringBuffer hexString = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> StringBuffer(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 字节数组转换为 十六进制 数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < md.length; i++) { String shaHex = Integer.toHexString(md[i] & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xFF</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (shaHex.length() < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) { hexString.append(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); } hexString.append(shaHex); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> hexString.toString(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (NoSuchAlgorithmException e) { e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } }</code>