終於有機會生產JAVA的東東了。java
有點興奮。python
花了一天搞完。。算法
java(關鍵key及算法有縮減):測試
package com.security; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * 實現AES加密解密 * cg882 * 2016-11-16 */ public class AESencrp { // 加密算法 private String ALGO = "AES"; private String ALGO_MODE = "AES/CBC/NoPadding"; private String akey = "16bit"; private String aiv = "16bit"; /** * 用來進行加密的操做 * * @param Data * @return * @throws Exception */ public String encrypt(String Data) throws Exception { try { Cipher cipher = Cipher.getInstance(ALGO_MODE); int blockSize = cipher.getBlockSize(); byte[] dataBytes = Data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO); IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8")); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); String EncStr = new sun.misc.BASE64Encoder().encode(encrypted); return EncStr ; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 用來進行解密的操做 * * @param encryptedData * @return * @throws Exception */ public String decrypt(String encryptedData) throws Exception { try { byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(encryptedData); Cipher cipher = Cipher.getInstance(ALGO_MODE); SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO); IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8")); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } catch (Exception e) { e.printStackTrace(); return null; } } }
測試JAVA的代碼:編碼
package com.security; /** * 實現AES加密 * cg882 * 2016-11-16 */ public class Checker { public static void main(String[] args) throws Exception { // 建立加解密 AESencrp aes = new AESencrp(); // 要進行加密的密碼 String password = "password^*(&( 09-8ADF"; // 進行加密後的字符串 String passwordEnc = aes.encrypt(password); String passwordDec = aes.decrypt(passwordEnc); System.out.println("原來的密碼 : " + password); System.out.println("加密後的密碼 : " + passwordEnc); System.out.println("解密後的原密碼 : " + passwordDec); } }
python就簡單多羅。。。。加密
#!/usr/bin/env python # -*- coding: utf-8 -*- from Crypto.Cipher import AES import base64 import os BLOCK_SIZE = 16 PADDING = '\0' pad_it = lambda s: s+(16 - len(s)%16)*PADDING key = '16bit' iv = '16bit' #使用aes算法,進行加密解密操做 #爲跟java實現一樣的編碼,注意PADDING符號自定義 def encrypt_aes(sourceStr): generator = AES.new(key, AES.MODE_CBC, iv) crypt = generator.encrypt(pad_it(sourceStr)) cryptedStr = base64.b64encode(crypt) return cryptedStr def decrypt_aes(cryptedStr): generator = AES.new(key, AES.MODE_CBC, iv) cryptedStr = base64.b64decode(cryptedStr) recovery = generator.decrypt(cryptedStr) decryptedStr = recovery.rstrip(PADDING) return decryptedStr sourceStr = 'password^*(&( 09-8ADF' print encrypt_aes(sourceStr) print decrypt_aes(encrypt_aes(sourceStr))
結果圖:spa
有幾個小技巧跟進解決一下:code
1,若是安裝PYTHON擴展庫時,須要C庫進行編譯,而本身又沒安裝權限或是BAT文件不對時,blog
直接從網上找預編譯好的WHL文件,或者能夠解決問題。ip
2,JAVA代碼手工編譯源碼,執行程序,生成jar的例子以下(注意按包結構建好目錄,執行命令時,在包頂層目錄執行相關命令)
A編譯java源代碼
javac -cp com/security com/security/*.java
B執行main文件,並指定包路徑
java -cp . com/security/Checker
C指定class生成jar包
jar cvf AESencrp.jar com/security/AESencrp.class