java基於AES對稱加密演算法實現的加密與解密功能範例
本文實例講述了java基於AES對稱加密演算法實現的加密與解密功能。分享給大家供大家參考,具體如下:
package com.soufun.com; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Date; 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; /** * @author WHD */ public class AesUtil { private static final String AES="AES"; private static final String UTF8="UTF-8"; static KeyGenerator kgen =null; static{ try { kgen= KeyGenerator.getInstance(AES); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * @param content: * @param password: */ private static byte[] encrypt(String content, String password) { try { // 使用静态代码块来生成KeyGenerator对象 //KeyGenerator kgen = KeyGenerator.getInstance(AES); // 使用128 位 kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] encodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(AES); // 加密内容进行编码 byte[] byteContent = content.getBytes(UTF8); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, key); // 正式执行加密操作 byte[] result = cipher.doFinal(byteContent); return result; } 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; } /* * @param content: * @param password: */ private static byte[] decrypt(byte[] content, String password) { try {// 使用静态代码块来生成KeyGenerator对象 //KeyGenerator kgen = KeyGenerator.getInstance(AES); // 使用128 位 kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] encodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(AES); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, key); // 正式执行解密操作 byte[] result = cipher.doFinal(content); return 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; } /** * 二进制--》十六进制转化 * @param buf * @return */ private static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 十六进制--》二进制转化 * @param hexStr * @return */ private 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) throws UnsupportedEncodingException { long begin=new Date().getTime(); String content = "aaades加密测试"; String password = "12345678dd"; // 加密 System.out.println("加密前:" + content); byte[] encryptResult = encrypt(content, password); String encryptResultStr = parseByte2HexStr(encryptResult); System.out.println("加密后:" + encryptResultStr); // 解密 byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); byte[] decryptResult = decrypt(decryptFrom, password); // 解密内容进行解码 String result = new String(decryptResult, UTF8); System.out.println("解密后:" + result); long end= new Date().getTime(); System.out.println(end-begin); } }
註:SecureRandom是產生安全隨機數序列,password.getBytes()是種子,只要種子相同,序列就一樣,所以解密只要有password就行,可以復原這個序列。
希望本文所述對大家java程式設計有幫助。
更多java基於AES對稱加密演算法實現的加密與解密功能範例相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
