• 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);
        }
    }
    

      

  • 相关阅读:
    event与WaitForSingleObject、MsgWaitForMultipleObjects等
    vc不包含MFC就不打印内存泄露?
    使用visual leak detector(vld)查找内存泄露
    C#里面中将字符串转为变量名
    如何编写nopCommerce插件
    object成员,不见了!
    NopCommerce 定制系列(一):增加 Sha256+Base64 加密
    c#中的二维数组与锯齿数组
    待搞清楚
    NopCommerce 2.5的部署
  • 原文地址:https://www.cnblogs.com/areful/p/10348809.html
Copyright © 2020-2023  润新知