import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class  AESUtil {
,,,private static final String IV_STRING =,“sdf4ddfsFD86Vdf2";
,,,private static final String encoding =,“UTF-8";
,,,public static String encryptAES (String 内容,String 键)
,,,,,,,,,,,throws InvalidKeyException,, NoSuchAlgorithmException,
,,,,,,,,,,,,,NoSuchPaddingException UnsupportedEncodingException,
,,,,,,,,,,,,,InvalidAlgorithmParameterException IllegalBlockSizeException, BadPaddingException {
,,,,,,,byte [], byteContent =, content.getBytes(编码);
,,,,,,,//,注意,为了能与,iOS 统一
,,,,,,,//,这里的,key 不可以使用,KeyGenerator, SecureRandom, SecretKey 生成
,,,,,,,byte [], enCodeFormat =, key.getBytes(编码);
,,,,,,,SecretKeySpec SecretKeySpec =, new SecretKeySpec (enCodeFormat,“AES");
,,,,,,,byte [], initParam =, IV_STRING.getBytes(编码);
,,,,,,,IvParameterSpec IvParameterSpec =, new IvParameterSpec (initParam);
,,,,,,,//,指定加密的算法,工作模式和填充方式
,,,,,,,Cipher Cipher =, . getinstance (“AES/CBC/PKCS5Padding");
,,,,,,,cipher.init (Cipher.ENCRYPT_MODE, secretKeySpec,, ivParameterSpec);
,,,,,,,byte [], encryptedBytes =, cipher.doFinal (byteContent);
,,,,,,,//,同样对加密后数据进行,base64 编码
,,,,,,,String base64 =, new Base64 () .encodeToString (encryptedBytes);
,,,,,,,//进行url编码,去掉=,?,,
,,,,,,,return URLEncoder.encode (base64编码);
,,,}
,,,public static String decryptAES (String 内容,String 键)
,,,,,,,,,,,throws InvalidKeyException,, NoSuchAlgorithmException,
,,,,,,,,,,,,,NoSuchPaddingException InvalidAlgorithmParameterException,
,,,,,,,,,,,,,IllegalBlockSizeException BadPaddingException, IOException {
,,,,,,,//URL解码
,,,,,,,content =, URLDecoder.decode(内容、编码);
,,,,,,,//,base64 解码
,,,,,,,byte [], encryptedBytes =, Base64.decodeBase64(内容);
,,,,,,,byte [], enCodeFormat =, key.getBytes(编码);
,,,,,,,SecretKeySpec secretKey =, new SecretKeySpec (enCodeFormat,“AES");
,,,,,,,byte [], initParam =, IV_STRING.getBytes(编码);
,,,,,,,IvParameterSpec IvParameterSpec =, new IvParameterSpec (initParam);
,,,,,,,Cipher Cipher =, . getinstance (“AES/CBC/PKCS5Padding");
,,,,,,,cipher.init (Cipher.DECRYPT_MODE, secretKey,, ivParameterSpec);
,,,,,,,byte [], result =, cipher.doFinal (encryptedBytes);
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null