美文网首页
[Private] AES Encryption

[Private] AES Encryption

作者: 赶时间的闹钟 | 来源:发表于2018-09-01 09:45 被阅读12次
    import android.util.Base64;
    
    import java.security.InvalidParameterException;
    import java.security.spec.AlgorithmParameterSpec;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    /**
     * AES encryption and decryption
     * <p>
     * 1. key's length >= 16<br>
     * 2. iv's length > 16<br>
     * 3. "transformation": AES/CBC/PKCS5Padding<br>
     * 4. iv=12(bytes) length=128(bits)<br>
     * 5. iv=24(bytes) length=192(bits)<br>
     * 6. iv=32(bytes) length=256(bits)<br>
     * <p>
     *
     * @author Anter
     * @date 2018/4/11
     */
    
    public class EncryptUtil {
    
        private EncryptUtil() {
        }
    
        /**
         * Base64 decode then AES decrypt
         *
         * @param data           Data to decrypt
         * @param key            Decrypt key
         * @param iv             Decrypt key
         * @param transformation AES/CBC/PKCS5Padding
         * @return Decrypted bytes
         * @throws Exception Decrypt exception
         */
        public static byte[] decryptBase64EncodeData(byte[] data, byte[] key, byte[] iv, String transformation) throws Exception {
    
            if (data == null || data.length == 0
                    || key == null || key.length < 16
                    || iv == null || iv.length < 16
                    || transformation == null || transformation.length() == 0) {
                throw (new InvalidParameterException());
            }
    
            byte[] textBytes = Base64.decode(data, Base64.DEFAULT);
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
            SecretKeySpec newKey = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
            return cipher.doFinal(textBytes);
        }
    
        /**
         * AES encrypt then base64 decode
         *
         * @param data           Data to encrypt
         * @param key            Encrypt key
         * @param iv             Encrypt key
         * @param transformation AES/CBC/PKCS5Padding
         * @return Encrypted bytes
         * @throws Exception Encrypt exception
         */
        public static byte[] encryptAndBase64Encode(byte[] data, byte[] key, byte[] iv, String transformation) throws Exception {
    
            if (data == null || data.length == 0
                    || key == null || key.length == 0
                    || iv == null || iv.length == 0
                    || transformation == null || transformation.length() == 0) {
                throw (new InvalidParameterException());
            }
    
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
            SecretKeySpec newKey = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
            return Base64.encode(cipher.doFinal(data), Base64.DEFAULT);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:[Private] AES Encryption

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