Java加密共通函数


Java加密共通函数
  1. import java.io.File;   
  2. import java.io.IOException;   
  3. import java.net.URL;   
  4. import java.security.InvalidKeyException;   
  5. import java.security.NoSuchAlgorithmException;   
  6. import java.security.Security;   
  7.   
  8. import javax.crypto.BadPaddingException;   
  9. import javax.crypto.Cipher;   
  10. import javax.crypto.IllegalBlockSizeException;   
  11. import javax.crypto.NoSuchPaddingException;   
  12. import javax.crypto.spec.SecretKeySpec;   
  13.   
  14. import org.apache.commons.io.FileUtils;   
  15. import org.apache.commons.lang.StringUtils;   
  16.   
  17.   
  18. public class DesUtils {   
  19.     static {   
  20.         Security.addProvider(new com.sun.crypto.provider.SunJCE());   
  21.     }   
  22.     static boolean debug = true;   
  23.     private static String Algorithm = "AES";   
  24.     private static String secutiyKey = "1234567812345678";   
  25.     private static String fileName1="DESPW1.class";   
  26.     private static String fileName2="DESPW2.class";   
  27.   
  28.     public static String getKey() throws Exception {   
  29.         String ret = "";   
  30.            
  31.         URL url=DesUtils.class.getResource("");   
  32.            
  33.         // 1.read the key from 1st file   
  34.         ret =new String(hex2byte(FileUtils.readFileToString(new File(url.getPath()+"/"+fileName1))));    
  35.         // 2.read the key from 2nd file   
  36.         // 3.combine the keys   
  37.         ret = ret + new String(hex2byte(FileUtils.readFileToString(new File(url.getPath()+"/"+fileName2))));    
  38.   
  39.         // 4.change them to the byte array   
  40.         return ret;   
  41.     }   
  42.        
  43.     public static void writeKeyToFile(){   
  44.         String key=StringUtils.strip(secutiyKey);   
  45.         int lenght=key.length();   
  46.         String prefix="";   
  47.         String afterFix="";   
  48.         if(lenght>0){   
  49.             prefix=StringUtils.substring(key, 0, lenght/2);   
  50.             afterFix=StringUtils.substring(key, lenght/2+1, lenght);   
  51.             URL url=DesUtils.class.getResource("");   
  52.             try {   
  53.                 FileUtils.writeStringToFile(new File(url.getPath()+"/"+fileName1), byte2hex(prefix.getBytes()));   
  54.                 FileUtils.writeStringToFile(new File(url.getPath()+"/"+fileName2), byte2hex(afterFix.getBytes()));   
  55.             } catch (IOException e) {   
  56.                    
  57.                 // TODO Auto-generated catch block   
  58.                 e.printStackTrace();   
  59.                    
  60.             }   
  61.         }   
  62.     }   
  63.   
  64.     public static byte[] encodeConfig(byte[] set, byte[] key) {   
  65.         byte[] ret = null;   
  66.   
  67.         // byte[] key = "tnts".getBytes();   
  68.   
  69.         try {   
  70.             SecretKeySpec sk = new SecretKeySpec(key, Algorithm);   
  71.             Cipher c1;   
  72.             c1 = Cipher.getInstance(Algorithm);   
  73.             c1.init(Cipher.ENCRYPT_MODE, sk);   
  74.             ret = c1.doFinal(set);   
  75.   
  76.         } catch (NoSuchAlgorithmException e) {   
  77.             e.printStackTrace();   
  78.         } catch (NoSuchPaddingException e) {   
  79.             e.printStackTrace();   
  80.         } catch (IllegalBlockSizeException e) {   
  81.             e.printStackTrace();   
  82.         } catch (BadPaddingException e) {   
  83.             e.printStackTrace();   
  84.         } catch (InvalidKeyException e) {   
  85.             e.printStackTrace();   
  86.         } catch (Exception e) {   
  87.   
  88.             // TODO Auto-generated catch block   
  89.             e.printStackTrace();   
  90.   
  91.         }   
  92.   
  93.         return ret;   
  94.     }   
  95.   
  96.     public static byte[] decodeConfig(byte[] set, byte[] key) {   
  97.         byte[] ret = null;   
  98.   
  99.         try {   
  100.             // byte[] key = getKey();   
  101.             SecretKeySpec sk = new SecretKeySpec(key, Algorithm);   
  102.             Cipher c1;   
  103.             c1 = Cipher.getInstance(Algorithm);   
  104.             c1.init(Cipher.DECRYPT_MODE, sk);   
  105.             ret = c1.doFinal(set);   
  106.         } catch (NoSuchAlgorithmException e) {   
  107.             e.printStackTrace();   
  108.         } catch (NoSuchPaddingException e) {   
  109.             e.printStackTrace();   
  110.         } catch (IllegalBlockSizeException e) {   
  111.             e.printStackTrace();   
  112.         } catch (BadPaddingException e) {   
  113.             e.printStackTrace();   
  114.         } catch (InvalidKeyException e) {   
  115.             e.printStackTrace();   
  116.         } catch (Exception e) {   
  117.             // TODO Auto-generated catch block   
  118.             e.printStackTrace();   
  119.   
  120.         }   
  121.   
  122.         return ret;   
  123.     }   
  124.   
  125.     // byte数组转换为16进制字符串   
  126.     public static String byte2hex(byte[] data) {   
  127.         StringBuffer sb = new StringBuffer();   
  128.         for (int i = 0; i < data.length; i++) {   
  129.             String temp = Integer.toHexString(((int) data[i]) & 0xFF);   
  130.             for (int t = temp.length(); t < 2; t++) {   
  131.                 sb.append("0");   
  132.             }   
  133.             sb.append(temp);   
  134.         }   
  135.         return sb.toString();   
  136.     }   
  137.   
  138.     // 16进制转换为byte数组   
  139.     public static byte[] hex2byte(String hexStr) {   
  140.         byte[] bts = new byte[hexStr.length() / 2];   
  141.         for (int i = 0, j = 0; j < bts.length; j++) {   
  142.             bts[j] = (byte) Integer.parseInt(hexStr.substring(i, i + 2), 16);   
  143.             i += 2;   
  144.         }   
  145.         return bts;   
  146.     }   
  147.   
  148.        
  149. }  

相关内容