zoukankan      html  css  js  c++  java
  • 聊聊、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

  • 相关阅读:
    C# dataGridView控件某单元格,间隔地变换背景色
    VS2005 DataGridView 和 GirdView 横向大比拼
    WinForm 修改Internet选项安全属性 。。。。。。。。。。
    [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
    C# 采集 :设计一个可超时的阻塞方法
    c# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.
    windows下的正则式工具介绍之一:RegexBuddy
    delegate 与异步调用。。。。。。。。。。。
    C# 轻松实现水印: 利用原图和水印图的重叠 !!!!
    DataGridView常用属性 《一》
  • 原文地址:https://www.cnblogs.com/xums/p/10604353.html
Copyright © 2011-2022 走看看