AES加密樣本

來源:互聯網
上載者:User

最近用到對常值內容進行加密,於是查了一下常用的密碼編譯演算法:

DES(Data Encryption Standard):對稱演算法,資料加密標準,速度較快,適用於加密大量資料的場合;
3DES(Triple DES):是基於DES的對稱演算法,對一塊資料用三個不同的密鑰進行三次加密,強度更高;
RC2和 RC4:對稱演算法,用變長金鑰組大量資料進行加密,比 DES 快;
IDEA(International Data Encryption Algorithm)國際資料加密演算法,使用 128 位密鑰提供非常強的安全性;
RSA:由 RSA 公司發明,是一個支援變長密鑰的公用密鑰演算法,需要加密的檔案塊的長度也是可變的,非對稱演算法;
DSA(Digital Signature Algorithm):數位簽章演算法,是一種標準的 DSS(數位簽章標準 (DSS)),嚴格來說不算密碼編譯演算法;
AES(Advanced Encryption Standard):進階加密標準,對稱演算法,是下一代的密碼編譯演算法標準,速度快,安全層級高,目前 AES 標準的一個實現是 Rijndael 演算法;
BLOWFISH,它使用變長的密鑰,長度可達448位,運行速度很快;
MD5:嚴格來說不算密碼編譯演算法,只能說是摘要演算法。

以下是Java 實現AES加解密的樣本:

加密:

/* * encrypt * @param content:  * @param password: */private static byte[] encrypt(String content, String password) {try {KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] encodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");Cipher cipher = Cipher.getInstance("AES");byte[] byteContent = content.getBytes("utf-8");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;}

解密:

/* * decrypt * @param content:  * @param password: */private static byte[] decrypt(byte[] content, String password) {try {KeyGenerator kgen = KeyGenerator.getInstance("AES");kgen.init(128, new SecureRandom(password.getBytes()));SecretKey secretKey = kgen.generateKey();byte[] encodeFormat = secretKey.getEncoded();SecretKeySpec key = new SecretKeySpec(encodeFormat, "AES");Cipher cipher = Cipher.getInstance("AES");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;}

樣本:

String content = "test";  String password = "12345678";  //加密   System.out.println("加密前:" + content);  byte[] encryptResult = encrypt(content, password);  //解密   byte[] decryptResult = decrypt(encryptResult,password);  System.out.println("解密後:" + new String(decryptResult)); 

輸出:

加密前:test解密後:test

如果加密後想十六進位顯示,可以添加兩個函數,二進位與十六進位轉換

二進位轉換為十六進位:

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();}

十六進位轉換為二進位:

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;}

樣本:

String content = "test";  String password = "12345678";  //加密   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);  System.out.println("解密後:" + new String(decryptResult));

輸出:

加密前:test加密後:73C58BAFE578C59366D8C995CD0B9D6D解密後:test

註:代碼參考於網路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.