聊聊、AES 和 DES

AES 和 DES 都是对称加密的一种,但是 DES 的 Key 是 56 位,而 AES 的 Key 有 128,256,512 可选。 

AES 


 加密AES 

String randomKey = "12345678";    
public static String ENAES() {
        try {
            KeyGenerator keyGene = KeyGenerator.getInstance("AES");
            keyGene.init(128, new SecureRandom(randomKey.getBytes()));
            SecretKey key = keyGene.generateKey();
            byte[] bytes = key.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
            Cipher ciper = Cipher.getInstance("AES");
            ciper.init(Cipher.ENCRYPT_MODE, keySpec);

            bytes = ciper.doFinal("hello".getBytes());            
            String result = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);            
            return result;            
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
          return null;
    }

 解密AES

String randomKey = "12345678";    
  public static String DEAES(String str) {
        try {
            byte[] srcBytes = org.apache.commons.codec.binary.Base64
                    .decodeBase64(str);
            KeyGenerator keyGene = KeyGenerator.getInstance("AES");
            keyGene.init(128, new SecureRandom(randomKey.getBytes()));
            SecretKey key = keyGene.generateKey();
            byte[] bytes = key.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
            Cipher ciper = Cipher.getInstance("AES");
            ciper.init(Cipher.DECRYPT_MODE, keySpec);

            bytes = ciper.doFinal(srcBytes);
            String strs = new String(bytes);

            return strs;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

 DES 


 

加密DES

String randomKey = "12345678";    

public static String ENDES() { SecureRandom random = new SecureRandom(); try { DESKeySpec desKey = new DESKeySpec(randomKey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, random); byte[] bytes = cipher.doFinal("hello".getBytes()); String result = Base64.getEncoder().encodeToString(bytes); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; }

 

解密DES

 
  
String randomKey = "12345678"; 
public static String DEDES(String str) {
        byte[] bytes = Base64.getDecoder().decode(str);
        SecureRandom random = new SecureRandom();
        try {
            DESKeySpec desKey = new DESKeySpec(randomKey.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, securekey, random);
            bytes = cipher.doFinal(bytes);
            String result = new String(bytes);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

mian 方法

public static void main(String[] args) throws NoSuchAlgorithmException {
       
        System.out.println("AES ENCRYPT:" + ENAES());
        System.out.println("AES DECRYPT:" + DEAES(ENAES()));
        System.out.println("DES ENCRYPT:" + ENDES());
        System.out.println("DES DECRYPT:" + DEDES(ENDES()));

} 

 



运行结果

AES ENCRYPT:70IScgmG93zMpkKvsNs+TQ==
AES DECRYPT:hello
DES ENCRYPT:uhbGoCVxJa8=
DES DECRYPT:hello

 

转载于:https://www.cnblogs.com/xums/p/10604353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值