美文网首页
Android AES+Base64加解密

Android AES+Base64加解密

作者: 搬砖的码农丶 | 来源:发表于2018-03-02 15:58 被阅读0次
    import android.util.Base64;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    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;
    
    /**
     * 加密
     * Created by fxf on 2018/1/9.
     */
    
    public class AESUtil {
    
        /**
         * 秘钥长度
         */
        private static final int SECURE_KEY_LENGTH = 16;
    
        private static final String IV_STRING = "16-Bytes--String";
    
    
        /**
         * 采用AES128加密
         *
         * @param content 要加密的内容
         * @param secureKey 密钥
         * @return
         */
        public static String encrypt(String content, String secureKey) {
            if (content == null) {
                return null;
            }
            try {
                // 获得密匙数据
                byte[] rawKeyData = getAESKey(secureKey);
                // 从原始密匙数据创建KeySpec对象
                SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
                // Cipher对象实际完成加密操作
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                // 用密匙初始化Cipher对象
                byte[] initParam = IV_STRING.getBytes();
                IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
                cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
                // 正式执行加密操作
    
                byte[] encryptByte = cipher.doFinal(content.getBytes());
    
                return  Base64.encodeToString(encryptByte,Base64.NO_WRAP);
    
            }catch (UnsupportedEncodingException e){
    
            }catch (NoSuchAlgorithmException e){
    
            }catch (NoSuchPaddingException E){
    
            }catch (InvalidAlgorithmParameterException e){
    
            }catch (InvalidKeyException e){
    
            }catch (IllegalBlockSizeException e){
    
            }catch (BadPaddingException e){
    
            }
            return null;
    
        }
    
        public static byte[] getAESKey(String key)
                throws UnsupportedEncodingException {
            byte[] keyBytes;
            keyBytes = key.getBytes("UTF-8");
            byte[] keyBytes16 = new byte[SECURE_KEY_LENGTH];
            System.arraycopy(keyBytes, 0, keyBytes16, 0,
                    Math.min(keyBytes.length, SECURE_KEY_LENGTH));
            return keyBytes16;
        }
    
        /**
         * 采用AES128解密
         *
         * @param content
         * @param secureKey
         * @return
         * @throws Exception
         *             ,Exception
         * @throws Exception
         */
        public static String decrypt(String content, String secureKey) {
            if (content == null) {
                return null;
            }
    
            byte[] data =  Base64.decode(content,Base64.NO_WRAP);
    
            try {
                // 获得密匙数据
                byte[] rawKeyData = getAESKey(secureKey); // secureKey.getBytes();
                // 从原始密匙数据创建一个KeySpec对象
                SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
                // Cipher对象实际完成解密操作
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                // 用密匙初始化Cipher对象
                byte[] initParam = IV_STRING.getBytes();
                IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
                cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
                return new String(cipher.doFinal(data),"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
    
            } catch (NoSuchPaddingException e) {
    
            } catch (InvalidKeyException e) {
    
            } catch (InvalidAlgorithmParameterException e) {
    
            } catch (IllegalBlockSizeException e) {
    
            } catch (BadPaddingException e) {
    
            }
    
    
            return null;
        }
    }
    

    相关文章

      网友评论

          本文标题:Android AES+Base64加解密

          本文链接:https://www.haomeiwen.com/subject/lgvaxftx.html