【數據加密】易懂易用的MD5加密(可直接運行) (1)

 概述:
出於安全考慮,網絡的傳輸中經常對傳輸數據做加密和編碼處理,其中涉及以下幾種: 
1、md5加密,該加密算法是單向加密,即加密的數據不能再通過解密還原。相關類包含在java.security.MessageDigest包中。 
2、3-DES加密,該加密算法是可逆的,解密方可以通過與加密方約定的密鑰匙進行解密。相關類包含在javax.crypto.*包中。 
3、base64編碼,是用於傳輸8bit字節代碼最常用的編碼方式。相關類在sun.misc.BASE64Decoder 和sun.misc.BASE64Encoder 中。 
4、URLEncoder編碼,是一種字符編碼,保證被傳送的參數由遵循規範的文本組成。相關類在java.net.URLEncoder包中。

  1. 細節:
  2. 1、進行MD5加密,得到byte[]
  3. /**
  4.   * 進行MD5加密
  5.   * @param  String 原始的SPKEY
  6.   * @return  byte[] 指定加密方式爲md5後的byte[]
  7.  */
  8. private byte[] md5(String strSrc)
  9. {
  10. byte[] returnByte = null;
  11. try
  12. {
  13. MessageDigest md5 = MessageDigest.getInstance("MD5"); 
  14. returnByte = md5.digest(strSrc.getBytes("GBK"));
  15. }
  16. catch(Exception e)
  17. {
  18. e.printStackTrace();
  19. }
  20. return returnByte;
  21. }  
  22. 2、得到3-DES的密鑰匙
  23. /**
  24.  * 得到3-DES的密鑰匙
  25.  * 根據根據需要,如密鑰匙爲24個字節,md5加密出來的是16個字節,因此後面補8個字節的0
  26.          * @param  String 原始的SPKEY
  27.  * @return  byte[] 指定加密方式爲md5後的byte[]
  28.  */
  29. private byte[] getEnKey(String spKey)
  30. {
  31. byte[] desKey=null;
  32. try
  33. {
  34.   byte[] desKey1 = md5(spKey);
  35.   desKey = new byte[24];
  36.   int i = 0;
  37.   while (i < desKey1.length && i < 24) {
  38. desKey[i] = desKey1[i];
  39. i++;
  40.   }
  41.   if (i < 24) {         
  42. desKey[i] = 0;
  43. i++;
  44.   }
  45. }
  46. catch(Exception e){
  47.   e.printStackTrace();
  48. }
  49. return desKey;
  50. }
  51. 3、3-DES加密
  52. /**
  53.   * 3-DES加密
  54.   * @param byte[] src 要進行3-DES加密的byte[]
  55.   * @param   byte[] enKey 3-DES加密密鑰
  56.   * @return  byte[] 3-DES加密後的byte[]
  57.   */
  58.  public byte[] Encrypt(byte[] src,byte[] enKey)
  59.  {
  60.   byte[] encryptedData = null;
  61.   try
  62.   {
  63.   DESedeKeySpec dks = new DESedeKeySpec(enKey); 
  64.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); 
  65.   SecretKey key = keyFactory.generateSecret(dks); 
  66.   Cipher cipher = Cipher.getInstance("DESede"); 
  67.   cipher.init(Cipher.ENCRYPT_MODE, key); 
  68.   encryptedData = cipher.doFinal(src); 
  69.   }
  70.   catch(Exception e)
  71.   {
  72.   e.printStackTrace();
  73.   }
  74.   return encryptedData;
  75.  }
  76. 4、對字符串進行Base64編碼
  77. /**
  78.   * 對字符串進行Base64編碼
  79.   * @param byte[] src 要進行編碼的字符
  80.   * 
  81.   * @return  String 進行編碼後的字符串
  82.   */
  83.  public String getBase64Encode(byte[] src)
  84.  {
  85.   String requestValue="";
  86.   try{
  87.     BASE64Encoder base64en = new BASE64Encoder();
  88.     requestValue=base64en.encode(src); 
  89.     //System.out.println(requestValue);
  90.   }
  91.   catch(Exception e){
  92.     e.printStackTrace();
  93.   }
  94.   
  95.   return requestValue;
  96.  }
  97. 5、根據需要可以去掉字符串的換行符號
  98. /**
  99.  * 去掉字符串的換行符號
  100.  * base64編碼3-DES的數據時,得到的字符串有換行符號,根據需要可以去掉
  101.      */
  102.  private String filter(String str)
  103.  {
  104.   String output = null;
  105.   StringBuffer sb = new StringBuffer();
  106.   for(int i = 0; i < str.length(); i++)
  107.   {
  108.   int asc = str.charAt(i);
  109.   if(asc != 10 && asc != 13)
  110.   sb.append(str.subSequence(i, i + 1));
  111.   }
  112.   output = new String(sb);
  113.   return output;
  114.  }
  115. 6、對字符串進行URLDecoder.encode(strEncoding)編碼
  116. /**
  117.   * 對字符串進行URLDecoder.encode(strEncoding)編碼
  118.   * @param String src 要進行編碼的字符串
  119.   * 
  120.   * @return  String 進行編碼後的字符串
  121.   */
  122.  public String getURLEncode(String src)
  123.  {
  124.   String requestValue="";
  125.   try{
  126.   
  127.   requestValue = URLEncoder.encode(src);
  128.   }
  129.   catch(Exception e){
  130.     e.printStackTrace();
  131.   }
  132.   
  133.   return requestValue;
  134.  }
  135. 7、對字符串進行URLDecoder.decode(strEncoding)解碼
  136. /**
  137.   * 對字符串進行URLDecoder.decode(strEncoding)解碼
  138.   * @param String src 要進行解碼的字符串
  139.   * 
  140.   * @return  String 進行解碼後的字符串
  141.   */
  142.  public String getURLDecoderdecode(String src)
  143.  {   
  144.   String requestValue="";
  145.   try{
  146.   
  147.   requestValue = URLDecoder.decode(src);
  148.   }
  149.   catch(Exception e){
  150.     e.printStackTrace();
  151.   }
  152.   
  153.   return requestValue;
  154.  }
  155. 8、進行3-DES解密(密鑰匙等同於加密的密鑰匙)
  156. /**
  157.   * 
  158.   *進行3-DES解密(密鑰匙等同於加密的密鑰匙)。 
  159.   * @param byte[]  src 要進行3-DES解密byte[] 
  160.   * @param   String spkey分配的SPKEY
  161.   * @return  String 3-DES解密後的String
  162.   */
  163.  public String deCrypt(byte[] debase64,String spKey)
  164.  {
  165.   String strDe = null;
  166.   Cipher cipher = null
  167.   try
  168.   {
  169.   cipher=Cipher.getInstance("DESede");
  170.   byte[] key = getEnKey(spKey); 
  171.   DESedeKeySpec dks = new DESedeKeySpec(key);
  172.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
  173.   SecretKey sKey = keyFactory.generateSecret(dks);
  174.   cipher.init(Cipher.DECRYPT_MODE, sKey); 
  175.   byte ciphertext[] = cipher.doFinal(debase64);
  176.   strDe = new String(ciphertext,"UTF-16LE"); 
  177.   }
  178.   catch(Exception ex)
  179.   {
  180.    strDe = "";
  181.    ex.printStackTrace();
  182.   }
  183.   return strDe;
  184. 經過以上步驟就可以完成MD5加密,3-DES加密、base64編碼傳輸、base64解碼、3-DES解密得到原文。



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章