• AES256对称加密


    需要引入bouncycastle库的jar包

    package test;
    
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.SecureRandom;
    import java.security.Security;
    import java.util.Base64;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Aes256Util {
        private final static String PROVIDER_BC = "BC";
        private final static String CHARSET = "UTF-8";
        private final static String ALGORITHM = "AES/ECB/PKCS5Padding";
        private final static String KEY_ALGORITHM = "AES";
        
        
        /**
         * 引入BC库
         */
        static {
            try {
                Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 初始化一个256位对称加密的秘钥
         * @param keyStr 如果加密解密的方法直接用一个String.getByte()的话,那么这个key需要达到128/192/256 bits
         * @return
         */
        public static byte[] getRandomKey(String keyStr){
            KeyGenerator keyGenerator = null;
            SecureRandom secureRandom = null;
            try {
                keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
                secureRandom = SecureRandom.getInstance("SHA1PRNG");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            secureRandom.setSeed(keyStr.getBytes());  
            keyGenerator.init(256, secureRandom);
            return keyGenerator.generateKey().getEncoded();
        }
        
        /**
         * 加密
         * @param content 原明文串
         * @param key 对称加密秘钥
         * @return 最后对byte进行了base64encode
         */
        public static String encrypt(String content, byte[] key){
            String encryptStr = "";
            
            try {
                //密码器初始化
                Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER_BC);
                SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
                byte[] encryptByte = cipher.doFinal(content.getBytes(CHARSET));
                
                encryptStr = Base64.getEncoder().encodeToString(encryptByte);
                
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            
            return encryptStr;
        }
        
        /**
         * 解密
         * @param contentByte 加密后的密文byte
         * @param key
         * @return
         */
        public static String decrypt(byte[] contentByte, byte[] key){
            String decryptStr = "";
            //密码器初始化
            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER_BC);
                SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
                byte[] encryptByte = cipher.doFinal(contentByte);
                
                decryptStr = new String(encryptByte, CHARSET);
                
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            
            return decryptStr;
        }
        
        public static void main(String[] args) {
            String keyStr = "1111111111111111111111111111111";
            
            byte[] rkey = getRandomKey(keyStr);
            
            String a = encrypt("test", rkey);
            
            String b = decrypt(Base64.getDecoder().decode(a), rkey);
            
            System.out.println(a);
            System.out.println(b);
        }
    }
  • 相关阅读:
    Qt中 .pro 文件和 .pri 文件简介
    [Android Pro] 完美Android Cursor使用例子(Android数据库操作)
    [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析
    [Android Pro] Android开发实践:为什么要继承onMeasure()
    [Android Pro] Scroller使用分析
    [Android Pro] 精确记录和恢复ListView滑动位置
    [Android Pro] Android TypedValue.applyDimension()的用法
    [Android Pro] http://blog.csdn.net/wuyinlei/article/category/5773375
    [Android Pro] 判断Uri对应的ContentProvider所操作的数据库u存在,及DownloadManager的暂停,继续
    [Android Pro] 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/10572780.html
Copyright © 2020-2023  润新知