Java encryption and decryption aes des TripleDes, aestripledes
Package xxx. common. util; import org. slf4j. logger; import org. slf4j. loggerFactory; import javax. crypto. badPaddingException; import javax. crypto. cipher; import javax. crypto. illegalBlockSizeException; import javax. crypto. noSuchPaddingException; import javax. crypto. spec. secretKeySpec; import java. io. unsupportedEncodingException; import java. security. invalidAlgorithmParameterException; import java. security. invali DKeyException; import java. security. key; import java. security. noSuchAlgorithmException; import java. security. secureRandom; import java. util. base64;/*** Created by windwant on 2016/12/13. */public class EncryptUtil {private static final Logger logger = LoggerFactory. getLogger (EncryptUtil. class); private static final String DEFAULT_CHARSET = "UTF-8"; private static final String EMPTY_STR = ""; private Static final int AES_KEY_SIZE = 16; // 256/192/128 ~ 32/24/16 public static void main (String [] args) {String tempkey = "! @ # $ % ^ & * () _ + "; String ming =" at sun. reflect. DelegatingMethodAccessorImpl. invoke (DelegatingMethodAccessorImpl. java: 43 )~ [?: 1.8.0 _ 77] "; long begin = System. currentTimeMillis (); for (int I = 0; I <100000; I ++) {String en = tripleDesEncrypt (ming, tempkey); String den = tripleDesDecrypt (en, tempkey);} long end = System. currentTimeMillis (); System. out. println ("TripleDES:" + (end-begin); System. out. println ("TripleDES:" + (end-begin)/100000.0); begin = System. currentTimeMillis (); for (int I = 0; I <100000; I ++ ){ String en = desEncrypt (ming, tempkey); String den = desDecrypt (en, tempkey);} end = System. currentTimeMillis (); System. out. println ("DES:" + (end-begin); System. out. println ("DES:" + (end-begin)/100000.0); begin = System. currentTimeMillis (); for (int I = 0; I <100000; I ++) {String en = aesEncrypt (ming, tempkey); String den = aesDecrypt (en, tempkey);} end = System. currentTimeMillis (); Syst Em. out. println ("AES:" + (end-begin); System. out. println ("AES:" + (end-begin)/100000.0);/* 100000 key :! @ # $ % ^ & * () _ + Src: at sun. reflect. DelegatingMethodAccessorImpl. invoke (DelegatingMethodAccessorImpl. java: 43 )~ [?: 1.8.0 _ 77] TripleDES: 3831 TripleDES: 0.03831 DES: 845 DES: 0.00845 AES: 888 AES: 0.00888 */} private static final String ENCRYPT = "AES "; private static final String CIPHER = "AES/CBC/PKCS5Padding "; /*** AES encryption ** @ param key encryption key * @ param src encrypted content * @ return returns BASE64 ciphertext */public static final String aesEncrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] Bs = getAESResult (key, src. getBytes (DEFAULT_CHARSET), Cipher. ENCRYPT_MODE); if (bs! = Null) {return Base64.getEncoder (). encodeToString (bs) ;}} catch (Exception e) {logger. error (e. getMessage ();} return src ;} /*** AES decryption ** @ param key decryption key * @ param src decryption content * @ return plaintext */public static final String aesDecrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] bs = getAESResult (key, Base64.getDecoder (). decode (src. getBytes (DEFAULT_CHARSET) ), Cipher. DECRYPT_MODE); if (bs! = Null) {return new String (bs, DEFAULT_CHARSET) ;}} catch (Exception e) {e. printStackTrace (); logger. error (e. getMessage ();} return src ;} /*** AES encryption and decryption result * @ param key * @ param textBytes plaintext ciphertext byte array * @ param encryptMode encryption and decryption * @ return */private static byte [] getAESResult (String key, byte [] textBytes, final int encryptMode)
Throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, condition, UnsupportedEncodingException {Key newKey = new SecretKeySpec (buildCLenKey (key, AES_KEY_SIZE), ENCRYPT); Cipher cipher = Cipher. getInstance (ENCRYPT); cipher. init (encryptMode, newKey, new SecureRandom (); return cipher. doFinal (textBytes);} // defines the encryption ALGORITHM, including DES and DESede (3DES) private static final String ALGORITHM = "DESede"; // ALGORITHM name/encryption mode/fill mode pri Vate static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding "; /*** TripleDES Encryption Method * @ param src * @ param key * @ return BASE64 */public static final String tripleDesEncrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] des = getTripleDESResult (key, src. getBytes (), Cipher. ENCRYPT_MODE); if (des! = Null) {return Base64.getEncoder (). encodeToString (des) ;}} catch (Exception e) {logger. error (e. getMessage ();} return src ;} /*** triledes decryption function * @ param src ciphertext byte array * @ param key * @ return String plaintext */public static final String tripleDesDecrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] srcb = Base64.getDecoder (). decode (src); byte [] des = getTripleDESResult (key, srcb, Cipher. DECRYPT_MODE); return new String (des, DEFAULT_CHARSET);} catch (Exception e) {logger. error (e. getMessage ();} return src ;} /*** TripleDES encryption and decryption result * @ param key * @ param textBytes plaintext ciphertext byte array * @ param encryptMode encryption and decryption * @ return */private static byte [] getTripleDESResult (String key, byte [] textBytes, final int encryptMode)
Throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {Key newKey = new SecretKeySpec (buildCLenKey (key, 24), ALGORITHM); Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM_ECB); cipher. init (encryptMode, newKey, new SecureRandom (); return cipher. doFinal (textBytes);}/*** generate a key byte array based on the string * @ param keyStr key string * @ param lgn key length * @ return length key byte array * @ throws Unsu PportedEncodingException */private static byte [] buildCLenKey (String keyStr, int lgn) throws UnsupportedEncodingException {byte [] key = new byte [lgn]; // declare a 24-bit byte array, the default value is 0 byte [] temp = keyStr. getBytes ("UTF-8"); // convert the string to a byte array // execute an array copy if (key. length> temp. length) {// If temp is less than 24 characters long, copy the entire length of the temp array to the key array System. arraycopy (temp, 0, key, 0, temp. length);} else {// If temp is greater than 24 bits, copy the 24-bit length contents of the temp array to System. arraycopy (temp, 0, key, 0, key. length);} return key;} private static final String DES_ALGORITHM = "DES"; public static final String DES_CIPHER_ALGORITHM = "DES "; /*** DES encryption method ** @ param src * @ param key * @ return BASE64 */public static final String desEncrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] des = getDESResult (key, src. getByt Es (), Cipher. ENCRYPT_MODE); if (des! = Null) {return Base64.getEncoder (). encodeToString (des) ;}} catch (Exception e) {logger. error (e. getMessage ();} return src ;} /*** DES decryption function * @ param src ciphertext byte array * @ param key * @ return String plaintext */public static final String desDecrypt (String src, String key) {if (key = null | src = null) {return EMPTY_STR;} try {byte [] srcb = Base64.getDecoder (). decode (src); byte [] des = getDESResult (key, srcb, Cipher. DECRYPT_MODE); return new String (des, DEFAULT_CHARSET);} catch (Exception e) {logger. error (e. getMessage ();} return src;} private static byte [] getDESResult (String key, byte [] textBytes, final int encryptMode)
Throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {Key newKey = new SecretKeySpec (buildCLenKey (key, 8), DES_ALGORITHM); ciphone Cipher = cipher. getInstance (DES_CIPHER_ALGORITHM); cipher. init (encryptMode, newKey, new SecureRandom (); return cipher. doFinal (textBytes );}}