Java tutorial
/** * Copyright (c) 2014 Baidu, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baidu.rigel.biplatform.ac.util; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.Key; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.lang.StringUtils; /** * ?AES? * * @author xiaoming.chen * */ public class AesUtil { public static final String UTF_8 = "utf-8"; /** * KEY_ALGORITHM */ private static final String KEY_ALGORITHM = "AES"; /** * DEFAULT_AES_KEY_VALUE AES160 */ public static final String DEFAULT_AES_KEY_VALUE = "0000000000000000"; /** * construct with */ private AesUtil() { } /** * aesEncrp ? */ private static final AesUtil INSTANCE = new AesUtil(); /** * ?AES * * @return AESencrp */ public static AesUtil getInstance() { return INSTANCE; } /** * ?? * * @param data * @param keyValue * @return ? * @throws IllegalArgumentException * @throws Exception InvalidKeyExceptionIllegalBlockSizeException, BadPaddingException? */ public String encrypt(String data, String keyValue) throws Exception { if (StringUtils.isBlank(data)) { throw new IllegalArgumentException("encode string can not be blank!"); } Key key = generateKey(keyValue); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); ; cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = cipher.doFinal(data.getBytes()); String encryptedValue = Base64.getEncoder().encodeToString(encVal); return encryptedValue; } /** * * * @param data * @return ? * @throws IllegalArgumentException * @throws Exception */ public String encrypt(String data) throws Exception { if (StringUtils.isBlank(data)) { throw new IllegalArgumentException("encode string can not be blank!"); } return encrypt(data, DEFAULT_AES_KEY_VALUE); } /** * AES?URLEncode? * * @param data * @param keyValue * @return ? * @throws IllegalArgumentException * @throws Exception */ public String encryptAndUrlEncoding(String data, String keyValue) throws Exception { String encryptValue = encrypt(data, keyValue); return URLEncoder.encode(encryptValue, UTF_8); } /** * AES?URLEncode? * * @param data * @return ? * @throws IllegalArgumentException * @throws Exception */ public String encryptAndUrlEncoding(String data) throws Exception { String encryptValue = encrypt(data); return URLEncoder.encode(encryptValue, UTF_8); } /** * ?? * * @param encryptedData * @param keyValue * @return ? * @throws IllegalArgumentException * @throws Exception */ public String decrypt(String encryptedData, String keyValue) throws Exception { if (StringUtils.isBlank(encryptedData)) { throw new IllegalArgumentException("decode string can not be blank!"); } Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); ; Key key = generateKey(keyValue); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = Base64.getDecoder().decode(encryptedData); byte[] decValue = cipher.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } /** * * * @param encryptedData * @return ? * @throws IllegalArgumentException * @throws Exception */ public String decrypt(String encryptedData) throws Exception { if (StringUtils.isBlank(encryptedData)) { throw new IllegalArgumentException("decode string can not be blank!"); } return decrypt(encryptedData, DEFAULT_AES_KEY_VALUE); } /** * urlDecode?? * * @param encryptedData * @param keyValue * @return ? * @throws Exception */ public String decodeAnddecrypt(String encryptedData, String keyValue) throws Exception { String decodeString = URLDecoder.decode(encryptedData, UTF_8); return decrypt(decodeString, keyValue); } /** * urlDecode?? * * @param encryptedData ? * @return ? * @throws Exception */ public String decodeAnddecrypt(String encryptedData) throws Exception { String decodeString = URLDecoder.decode(encryptedData, UTF_8); return decrypt(decodeString); } /** * ??Key * * @param keyValue ? * @return KEY */ private Key generateKey(String keyValue) { Key key = new SecretKeySpec(checkKeyValue(keyValue).getBytes(), KEY_ALGORITHM); return key; } /** * <li>16???16?</li> <li>?16???0</li> * * @param keyValue ? * @return ? * @throws IllegalArgumentException */ private String checkKeyValue(String keyValue) { if (StringUtils.isBlank(keyValue)) { throw new IllegalArgumentException("key value can not be blank!"); } // ?16???0 int length = keyValue.length(); if (length >= DEFAULT_AES_KEY_VALUE.length()) { return keyValue.substring(0, DEFAULT_AES_KEY_VALUE.length()); } else { return StringUtils.rightPad(keyValue, DEFAULT_AES_KEY_VALUE.length(), '0'); } } }