zoukankan      html  css  js  c++  java
  • Java使用AES算法

    Java中使用AES(CBC,128位)算法加解密。一般加密后都是用一定编码格式进行传输,此处使用Base64算法进行编解码。实现及测试代码如下:

    AESUtil.java

    package gj.secure;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.Key;
    import java.security.spec.AlgorithmParameterSpec;
    import java.util.Base64;
    
    public class AESUtil {
        private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
        private static final String KEY_ALGORITHM = "AES";
    
        public static byte[] initKey() throws Exception {
            KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            kg.init(128);
            SecretKey secretKey = kg.generateKey();
            return secretKey.getEncoded();
        }
    
        public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            Key k = new SecretKeySpec(key, KEY_ALGORITHM);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, k, paramSpec);
            return cipher.doFinal(data);
        }
    
        public static byte[] decrypt(byte[] bytes, byte[] key, byte[] iv) throws Exception {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            Key k = new SecretKeySpec(key, KEY_ALGORITHM);
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, k, paramSpec);
            return cipher.doFinal(bytes);
        }
    
        public static String encodeToBase64String(String data, byte[] key, byte[] iv) throws Exception {
            return Base64.getEncoder().encodeToString(encrypt(data.getBytes(), key, iv));
        }
    
        public static String decodeFromBase64String(String data, byte[] key, byte[] iv) throws Exception {
            byte[] bytes = Base64.getDecoder().decode(data);
            return new String(decrypt(bytes, key, iv));
        }
    }
    

      

    测试代码:

    AESUtilTest.java

    package gj.secure;
    
    /**
     * created by gj on 2019-12-24 17:17
     **/
    public class AESUtilTest {
        public static void main(String[] args) throws Exception {
            byte[] key = AESUtil.initKey();
            byte[] iv = {0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF,
                    0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF};
            String content = "areful1997";
            String cipher = AESUtil.encodeToBase64String(content, key, iv);
            System.out.println(cipher);
    
            String plain = AESUtil.decodeFromBase64String(cipher, key, iv);
            System.out.println(plain);
        }
    }
    

      

  • 相关阅读:
    第一章:linux命令初步
    请教shell读写XML问题(转)
    讓 BootCamp 下的 Windows XP 也能有 D 硬碟槽(转)
    Linux下的多线程编程(转)
    怎么查看redhat版本
    不透過 Boot Camp 安裝 Windows 7,並切割成多個磁碟槽(转)
    同位语从句用法详解
    更改linux的最大文件描述符限制
    ObjectiveC中 copy, tetain, assign , readonly , readwrite, nonatomic区别
    Linux如何查找文件安装路径
  • 原文地址:https://www.cnblogs.com/areful/p/10348809.html
Copyright © 2011-2022 走看看