java加密

一、BASE64

这里选用Java自带的BASE64Encoder和BASE64Decoder进行BASE64编码,除此之外,可以选择commons-codec.jar等第三方jar包进行实现。

BASE64Encoder和BASE64Decoder为实现BASE64的API,为了解决高并发问题,提高运行效率,本例将encoder和decoder作为全局静态属性,并通过ThreadLocal进行管理。

 
[java] view plain copy
 
  1. package com.fhp.testencrypt;  
  2.   
  3. import sun.misc.BASE64Encoder;  
  4. import java.io.IOException;  
  5. import sun.misc.BASE64Decoder;  
  6.   
  7. public class Base64Utils {  
  8.       
  9.     private static ThreadLocal<BASE64Encoder> encoder = new ThreadLocal<BASE64Encoder>() {  
  10.         @Override  
  11.         protected BASE64Encoder initialValue() {  
  12.             return new BASE64Encoder();  
  13.         }  
  14.     };  
  15.       
  16.     private static ThreadLocal<BASE64Decoder> decoder = new ThreadLocal<BASE64Decoder>() {  
  17.         @Override  
  18.         protected BASE64Decoder initialValue() {  
  19.             return new BASE64Decoder();  
  20.         }  
  21.     };  
  22.       
  23.     public static String encode(byte[] bytes) {  
  24.         return encoder.get().encode(bytes);  
  25.     }  
  26.       
  27.     public static byte[] decode(String str) {  
  28.         try {  
  29.             return decoder.get().decodeBuffer(str);  
  30.         } catch (IOException e) {  
  31.             throw new RuntimeException("Cannot decode string:" + str, e);  
  32.         }  
  33.     }  
  34. }  

二、DES

[java] view plain copy
 
  1. package com.fhp.testencrypt;  
  2.   
  3. import java.security.InvalidKeyException;  
  4. import java.security.Key;  
  5. import java.security.NoSuchAlgorithmException;  
  6. import java.security.SecureRandom;  
  7. import java.util.concurrent.ExecutorService;  
  8. import java.util.concurrent.Executors;  
  9.   
  10. import javax.crypto.BadPaddingException;  
  11. import javax.crypto.Cipher;  
  12. import javax.crypto.IllegalBlockSizeException;  
  13. import javax.crypto.KeyGenerator;  
  14. import javax.crypto.NoSuchPaddingException;  
  15. import javax.crypto.SecretKey;  
  16.   
  17. public class DESUtils {  
  18.       
  19.     // 算法名称    
  20.     private static final String KEY_ALGORITHM = "DES";    
  21.     // 算法名称/加密模式/填充方式    
  22.     private static final String CIPHER_ALGORITHM_ECB = "DES/ECB/PKCS5Padding";  
  23.       
  24.     private static final Key defaultKey = generateKey();  
  25.       
  26.     /** 
  27.      * 生成密钥 
  28.      */  
  29.     public static Key generateKey() {  
  30.         KeyGenerator keyGenerator;  
  31.         try {  
  32.             keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);  
  33.         } catch (NoSuchAlgorithmException e) {  
  34.             throw new RuntimeException("Unable to generate DES key.", e);  
  35.         }  
  36.         keyGenerator.init(56);//这里必须填56  
  37.         SecretKey secretKey = keyGenerator.generateKey();  
  38.         return secretKey;  
  39.     }  
  40.       
  41.     /** 
  42.      * 使用默认密钥对字节数组进行加密,返回加密后的字节数组 
  43.      */  
  44.     public static byte[] encrypt(byte[] source) {  
  45.         return encrypt(source, null);  
  46.     }  
  47.       
  48.     /** 
  49.      * 使用默认密钥对字节数组进行解密,返回解密后的字节数组 
  50.      */  
  51.     public static byte[] decrypt(byte[] source) {  
  52.         return decrypt(source, null);  
  53.     }  
  54.       
  55.     /** 
  56.      * 使用指定密钥对字节数组进行加密,返回加密后的字节数组 
  57.      */  
  58.     public static byte[] encrypt(byte[] source, Key key) {  
  59.         try {  
  60.             Key currentKey = key == null ? defaultKey : key;  
  61.             Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
  62.             cipher.init(Cipher.ENCRYPT_MODE, currentKey, new SecureRandom());    
  63.             return cipher.doFinal(source);    
  64.         } catch (NoSuchAlgorithmException e) {  
  65.             throw new RuntimeException("Unable to encrypt.", e);  
  66.         } catch (NoSuchPaddingException e) {  
  67.             throw new RuntimeException("Unable to encrypt.", e);  
  68.         } catch (InvalidKeyException e) {  
  69.             throw new RuntimeException("Unable to encrypt.", e);  
  70.         } catch (IllegalBlockSizeException e) {  
  71.             throw new RuntimeException("Unable to encrypt.", e);  
  72.         } catch (BadPaddingException e) {  
  73.             throw new RuntimeException("Unable to encrypt.", e);  
  74.         }    
  75.     }  
  76.       
  77.      /** 
  78.      * 使用指定密钥对字节数组进行解密,返回解密后的字节数组 
  79.      */  
  80.     public static byte[] decrypt(byte[] source, Key key) {  
  81.         try {  
  82.             Key currentKey = key == null ? defaultKey : key;  
  83.             Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
  84.             cipher.init(Cipher.DECRYPT_MODE, currentKey, new SecureRandom());    
  85.             return cipher.doFinal(source);    
  86.         } catch (NoSuchAlgorithmException e) {  
  87.             throw new RuntimeException("Unable to decrypt.", e);  
  88.         } catch (NoSuchPaddingException e) {  
  89.             throw new RuntimeException("Unable to decrypt.", e);  
  90.         } catch (InvalidKeyException e) {  
  91.             throw new RuntimeException("Unable to decrypt.", e);  
  92.         } catch (IllegalBlockSizeException e) {  
  93.             throw new RuntimeException("Unable to decrypt.", e);  
  94.         } catch (BadPaddingException e) {  
  95.             throw new RuntimeException("Unable to decrypt.", e);  
  96.         }    
  97.     }  
  98.       
  99.     public static void main(String[] args) {  
  100.         String str = "测试字符串";  
  101.         byte[] encrypted = DESUtils.encrypt(str.getBytes());  
  102.                   
  103.         byte[] decrypted = DESUtils.decrypt(encrypted);  
  104.         System.out.println("cipher text:" + new String(encrypted));  
  105.         System.out.println("normal text:" + new String(decrypted));  
  106.     }  
  107. }  
原文地址:https://www.cnblogs.com/ghc666/p/8657505.html