AES加密解密Windows下跟linux下结果不同的解决方案

Java代码  收藏代码
  1. import java.io.UnsupportedEncodingException;  
  2. import java.security.InvalidKeyException;  
  3. import java.security.NoSuchAlgorithmException;  
  4. import java.security.SecureRandom;  
  5.   
  6. import javax.crypto.BadPaddingException;  
  7. import javax.crypto.Cipher;  
  8. import javax.crypto.IllegalBlockSizeException;  
  9. import javax.crypto.KeyGenerator;  
  10. import javax.crypto.NoSuchPaddingException;  
  11. import javax.crypto.SecretKey;  
  12. import javax.crypto.spec.SecretKeySpec;  
  13.   
  14. import org.apache.axis.encoding.Base64;  
  15.   
  16. public class AES {  
  17.     private static int length=128;  
  18.     /** 
  19.      * 加密 
  20.      *  
  21.      * @param content 
  22.      *            需要加密的内容 
  23.      * @param password 
  24.      *            加密密码 
  25.      * @return 
  26.      * @throws NoSuchAlgorithmException 
  27.      * @throws NoSuchPaddingException 
  28.      * @throws UnsupportedEncodingException 
  29.      * @throws InvalidKeyException 
  30.      * @throws BadPaddingException 
  31.      * @throws IllegalBlockSizeException 
  32.      */  
  33.     private static byte[] encrypt(String content, String password)  
  34.             throws Exception {  
  35.   
  36.         KeyGenerator kgen = KeyGenerator.getInstance("AES");  
  37.                 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );   
  38.                 secureRandom.setSeed(password.getBytes());   
  39.         kgen.init(length, secureRandom);  
  40.         SecretKey secretKey = kgen.generateKey();  
  41.         byte[] enCodeFormat = secretKey.getEncoded();  
  42.         SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  
  43.         Cipher cipher = Cipher.getInstance("AES");// 创建密码器  
  44.         byte[] byteContent = content.getBytes("utf-8");  
  45.         cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化  
  46.         byte[] result = cipher.doFinal(byteContent);  
  47.         return result; // 加密  
  48.   
  49.     }  
  50.   
  51.     /** 
  52.      * 解密 
  53.      *  
  54.      * @param content 
  55.      *            待解密内容 
  56.      * @param password 
  57.      *            解密密钥 
  58.      * @return 
  59.      */  
  60.     private static byte[] decrypt(byte[] content, String password)  
  61.             throws Exception {  
  62.   
  63.         KeyGenerator kgen = KeyGenerator.getInstance("AES");  
  64.                  SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );   
  65.                   secureRandom.setSeed(password.getBytes());   
  66.         kgen.init(length, secureRandom);  
  67.         SecretKey secretKey = kgen.generateKey();  
  68.         byte[] enCodeFormat = secretKey.getEncoded();  
  69.         SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  
  70.         Cipher cipher = Cipher.getInstance("AES");// 创建密码器  
  71.         cipher.init(Cipher.DECRYPT_MODE, key);// 初始化  
  72.         byte[] result = cipher.doFinal(content);  
  73.         return result; // 加密  
  74.                   
  75.                
  76.   
  77.     }  
  78.   
  79. //  /**  
  80. //   * 将二进制转换成16进制  
  81. //   *   
  82. //   * @param buf  
  83. //   * @return  
  84. //   */  
  85. //  public static String parseByte2HexStr(byte buf[]) {  
  86. //      StringBuffer sb = new StringBuffer();  
  87. //      for (int i = 0; i < buf.length; i++) {  
  88. //          String hex = Integer.toHexString(buf[i] & 0xFF);  
  89. //          if (hex.length() == 1) {  
  90. //              hex = '0' + hex;  
  91. //          }  
  92. //          sb.append(hex.toUpperCase());  
  93. //      }  
  94. //      return sb.toString();  
  95. //  }  
  96. //  
  97. //  /**  
  98. //   * 将16进制转换为二进制  
  99. //   *   
  100. //   * @param hexStr  
  101. //   * @return  
  102. //   */  
  103. //  public static byte[] parseHexStr2Byte(String hexStr) {  
  104. //      if (hexStr.length() < 1)  
  105. //          return null;  
  106. //      byte[] result = new byte[hexStr.length() / 2];  
  107. //      for (int i = 0; i < hexStr.length() / 2; i++) {  
  108. //          int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);  
  109. //          int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),  
  110. //                  16);  
  111. //          result[i] = (byte) (high * 16 + low);  
  112. //      }  
  113. //      return result;  
  114. //  }  
  115.   
  116.     /** 
  117.      * 加密 
  118.      *  
  119.      * @param content 
  120.      *            需要加密的内容 
  121.      * @param password 
  122.      *            加密密码 
  123.      * @return 
  124.      */  
  125.     public static byte[] encrypt2(String content, String password) {  
  126.         try {  
  127.             SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");  
  128.             Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");  
  129.             byte[] byteContent = content.getBytes("utf-8");  
  130.             cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化  
  131.             byte[] result = cipher.doFinal(byteContent);  
  132.             return result; // 加密  
  133.         } catch (NoSuchAlgorithmException e) {  
  134.             e.printStackTrace();  
  135.         } catch (NoSuchPaddingException e) {  
  136.             e.printStackTrace();  
  137.         } catch (InvalidKeyException e) {  
  138.             e.printStackTrace();  
  139.         } catch (UnsupportedEncodingException e) {  
  140.             e.printStackTrace();  
  141.         } catch (IllegalBlockSizeException e) {  
  142.             e.printStackTrace();  
  143.         } catch (BadPaddingException e) {  
  144.             e.printStackTrace();  
  145.         }  
  146.         return null;  
  147.     }  
  148.   
  149.     public static String encrypt2Str(String content, String password) throws Exception {  
  150.         byte[] encryptResult = encrypt(content, password);  
  151.         return Base64.encode(encryptResult);  
  152.     }  
  153.   
  154.     public static String decrypt2Str(String content, String password) throws Exception {  
  155.   
  156.         byte[] decryptResult = decrypt(Base64.decode(content), password);  
  157.         return new String(decryptResult,"UTF-8");  
  158.     }  
  159.   
  160.     public static void main(String[] args) throws Exception {  
  161.         String content = "t太阳est地";  
  162.         String password = "12345678";  
  163.         // 加密  
  164.         System.out.println("加密前:" + content);  
  165.   
  166.         String tt4 = encrypt2Str(content, password);  
  167.         System.out.println(new String(tt4));  
  168.   
  169.         // 解密  
  170.         String d = decrypt2Str(tt4, password);  
  171.         System.out.println("解密后:" + d);  
  172.           
  173. //      加密前:t太阳est地  
  174. //      Bpf0jyJDj/pVHaRf66+OMA==  
  175. //      解密后:t太阳est地  
  176.     }  
  177. }  

 

其中的org.apache.axis.encoding.Base64,可以直接用附件中的Base64代替(直接从axis反编译而得

原文地址:https://www.cnblogs.com/hzcya1995/p/13317690.html