zoukankan      html  css  js  c++  java
  • JAVA Aes加密解密

    Aes 加密两种方式为什么加出来的不一样?求大神讲解

    第一种

     1    /**
     2      * 加密
     3      * @param content
     4      * @param keyBytes
     5      * @param iv
     6      * @return
     7      * @throws Exception
     8      */
     9     public String AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception{
    10         try{
    11             KeyGenerator keyGenerator= KeyGenerator.getInstance("AES");
    12             keyGenerator.init(128, new SecureRandom(keyBytes) );
    13             SecretKey key=keyGenerator.generateKey();
    14             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
    15             cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
    16             byte[] result=cipher.doFinal(content);
    17             return Base64.encodeBase64String(result);
    18         }catch (Exception e) {
    19             e.printStackTrace();
    20             throw e;
    21         }
    22     }
    23 
    24     /**
    25      * 解密
    26      * @param content
    27      * @param keyBytes
    28      * @param iv
    29      * @return
    30      * @throws Exception
    31      */
    32     public String AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception{
    33         try{
    34 
    35             KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
    36             keyGenerator.init(128, new SecureRandom(keyBytes));//key长可设为128,192,256位,这里只能设为128
    37             SecretKey key=keyGenerator.generateKey();
    38             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
    39             cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
    40 
    41             byte[] result=cipher.doFinal(content);
    42             return Base64.encodeBase64String(result);
    43         }catch (Exception e) {
    44             e.printStackTrace();
    45             throw e;
    46         }
    47     }

    第二种

     1 /**
     2      * 提供密钥和向量进行加密
     3      *
     4      * @param sSrc
     5      * @param key
     6      * @param iv
     7      * @return
     8      * @throws Exception
     9      */
    10     public static String Encrypt(String sSrc, byte[] key, byte[] iv) throws Exception {
    11         SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    12         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
    13         IvParameterSpec _iv = new IvParameterSpec(iv);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
    14         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv);
    15         byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
    16         return Base64.encodeBase64String(encrypted);
    17     }
    18     /**
    19      * 提供密钥和向量进行解密
    20      *
    21      * @param sSrc
    22      * @param key
    23      * @param iv
    24      * @return
    25      * @throws Exception
    26      */
    27     public static String Decrypt(String sSrc, byte[] key, byte[] iv) throws Exception {
    28         SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    29         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    30         IvParameterSpec _iv = new IvParameterSpec(iv);
    31         cipher.init(Cipher.DECRYPT_MODE, skeySpec, _iv);
    32         byte[] encrypted = Base64.decodeBase64(sSrc);
    33         byte[] original = cipher.doFinal(encrypted);
    34         return new String(original, "utf-8");
    35     }
  • 相关阅读:
    uGUI知识点剖析之RectTransform
    C#中的结构体要使用new来实例化吗?
    LOL数值分析
    【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则
    AnimationCurve
    Unity3D 自动添加Fbx Animation Event
    unity 代码添加AnimationEvent
    untiy AnimationEvent添加返回参数
    Windows CreateEvent,SetEvent,WaitForSingleObject的用法
    C++ Socket编程步骤
  • 原文地址:https://www.cnblogs.com/duanxianyouyang/p/6541403.html
Copyright © 2011-2022 走看看