Java AES加密解密的简单实现方法
程序员文章站
2023-12-20 10:03:34
废话不多说,直接上代码
package com.mstf.aes;
import java.io.unsupportedencodingexception...
废话不多说,直接上代码
package com.mstf.aes; import java.io.unsupportedencodingexception; import java.security.invalidkeyexception; import java.security.nosuchalgorithmexception; import java.security.securerandom; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.keygenerator; import javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; /** * aes加密解密 * @author ceet * */ public class aesuntil { /** * 加密 * * */ public static string ecodes(string content, string key) { if (content == null || content.length() < 1) return null; try { keygenerator kgen = keygenerator.getinstance("aes"); kgen.init(128, new securerandom(key.getbytes())); secretkey secretkey = kgen.generatekey(); byte[] encodeformat = secretkey.getencoded(); secretkeyspec secretkeyspec = new secretkeyspec(encodeformat, "aes"); cipher cipher = cipher.getinstance("aes"); byte[] bytecontent = content.getbytes("utf-8"); cipher.init(cipher.encrypt_mode, secretkeyspec); byte[] byterresult = cipher.dofinal(bytecontent); stringbuffer sb = new stringbuffer(); for (int i = 0; i < byterresult.length; i++) { string hex = integer.tohexstring(byterresult[i] & 0xff); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.touppercase()); } return sb.tostring(); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (nosuchpaddingexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (illegalblocksizeexception e) { e.printstacktrace(); } catch (badpaddingexception e) { e.printstacktrace(); } return null; } /** * 解密 * * */ public static string dcodes(string content, string key) { if (content == null || content.length() < 1) return null; if (content.trim().length() < 19) return content; byte[] byterresult = new byte[content.length() / 2]; for (int i = 0; i < content.length() / 2; i++) { int high = integer.parseint(content.substring(i * 2, i * 2 + 1), 16); int low = integer.parseint(content.substring(i * 2 + 1, i * 2 + 2), 16); byterresult[i] = (byte) (high * 16 + low); } try { keygenerator kgen = keygenerator.getinstance("aes"); kgen.init(128, new securerandom(key.getbytes())); secretkey secretkey = kgen.generatekey(); byte[] encodeformat = secretkey.getencoded(); secretkeyspec secretkeyspec = new secretkeyspec(encodeformat, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, secretkeyspec); byte[] result = cipher.dofinal(byterresult); return new string(result); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (nosuchpaddingexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (illegalblocksizeexception e) { e.printstacktrace(); } catch (badpaddingexception e) { e.printstacktrace(); } return null; } /** * 详细解释 * 【ceet为加密的密匙】 * 【admin为需要加密的字符串】 * 【67be5ed967dba9b9810c295be6def5d5为解密后的字符串】 * 【如果更改ceet,那么67be5ed967dba9b9810c295be6def5d5字符串会发生变化】 * @param args */ // 调用测试 public static void main(string[] args) { system.out.println("需要加密的内容:"+ecodes("admin", "ceet")); system.out.println("经过解密的内容:"+dcodes("67be5ed967dba9b9810c295be6def5d5", "ceet")); } }
以上这篇java aes加密解密的简单实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。