zoukankan      html  css  js  c++  java
  • Java AES加解密

    import java.util.Base64;
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    public class CipherEncrptTest {
    
        public static void main(String[] args) {
            //默认加密的salt key
            String key = "0123abcd0123efmn";
            String text = "123456";
            try {
                byte[] raw = key.getBytes("utf-8");
                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec iv = new IvParameterSpec(key.getBytes());
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
                byte[] original = cipher.doFinal(text.getBytes());
                System.out.println(new String(Base64.getEncoder().encode(original)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void decrpt() {
            try {
                String sKey = "0123abcd0123efmn";
                byte[] raw = sKey.getBytes("utf-8");
                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec iv = new IvParameterSpec(sKey.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
                byte[] encrypted1 = Base64.getDecoder().decode(endSrc);//先用base64解密
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                logger.info("解密结果:{}", originalString);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        }
    }
  • 相关阅读:
    快速幂
    1112个人赛,最长回文串常见算法讨论
    11-05-sdust-个人赛赛后随想
    UVA 1149 Bin Packing
    UVa 1608,Non-boring sequences
    UVa 10954,Add All
    UVa 714,Copying Books
    Careercup
    Careercup
    Careercup
  • 原文地址:https://www.cnblogs.com/wenwujuncheng/p/11812318.html
Copyright © 2011-2022 走看看