JAVA实现AES加密,解密
短信预约 -IT技能 免费直播动态提醒
java提供了加解密工具,可以将字节转成加密字节.
因此在加解密前需要对内容进行转换.
一般情况我们希望以字符串的形式展示加密串
可以将byte[] 转换为base64字符串 也可以转换为16进制字符串
这里提供两个工具类
加密串为base64字符串
可以直接在数据库中进行解密,无需查到后再遍历解密
public class AESUtil { // 加密 public static String Encrypt(String sclass="lazy" data-src, String sKey) throws Exception { if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sclass="lazy" data-src.getBytes("utf-8")); return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。 } // 解密 public static String Decrypt(String sclass="lazy" data-src, String sKey) throws Exception { try { // 判断Key是否正确 if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sclass="lazy" data-src);//先用base64解密 try { byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original,"utf-8"); return originalString; } catch (Exception e) { System.out.println(e.toString()); return null; } } catch (Exception ex) { System.out.println(ex.toString()); return null; } } public static void main(String[] args) throws Exception { String cKey = "jkl;POIU1234++=="; // 需要加密的字串 String cclass="lazy" data-src = "www.gowhere.so"; System.out.println(cclass="lazy" data-src); // 加密 String enString = MyAESUtil.Encrypt(cclass="lazy" data-src, cKey); System.out.println("加密后的字串是:" + enString); // 解密 String DeString = MyAESUtil.Decrypt(enString, cKey); System.out.println("解密后的字串是:" + DeString); }}
加密串为16进制字符串
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class MyAESUtil { private static final Logger logger = LoggerFactory.getLogger(MyAESUtil.class); public static String Encrypt(String sclass="lazy" data-src, String sKey) throws Exception { if (sKey == null) { logger.error("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { logger.error("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sclass="lazy" data-src.getBytes("utf-8")); return encodeHexString(encrypted); } public static String encodeHexString(byte[] byteArray) { StringBuffer hexStringBuffer = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { hexStringBuffer.append(byteToHex(byteArray[i])); } return hexStringBuffer.toString(); } public static String byteToHex(byte num) { char[] hexDigits = new char[2]; hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16); hexDigits[1] = Character.forDigit((num & 0xF), 16); return new String(hexDigits); } public static String Decrypt(String sclass="lazy" data-src, String sKey) { try { // 判断Key是否正确 if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encrypted1 = parseHexStr2Byte(sclass="lazy" data-src); //new BASE64Decoder().decodeBuffer(sclass="lazy" data-src);//先用base64解密 try { byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original,"utf-8"); return originalString; } catch (Exception e) { logger.error(e.toString()); return null; } } catch (Exception ex) { logger.error(ex.toString()); return null; } } public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length()/2]; for (int i = 0;i< hexStr.length()/2; i++) { int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); result[i] = (byte) (high * 16 + low); } return result; } // 测试 public static void main(String[] args) { try { String key = "8ISIwyJodASysWTM"; String encrypt = MyAESUtil.Encrypt("123456", key); // 加密 System.out.println(encrypt); String decrypt = MyAESUtil.Decrypt(encrypt, key); // 解密 System.out.println(decrypt); }catch (Exception e){ e.printStackTrace(); } }}
参考文章:https://blog.csdn.net/fuhanghang/article/details/124010261
来源地址:https://blog.csdn.net/Passer_hua/article/details/128827608
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341