【数据加密】易懂易用的MD5加密(可直接运行) (1)

来源:互联网 发布:哪里可以购买淘宝小号 编辑:程序博客网 时间:2024/05/11 22:35

 概述:
出于安全考虑,网络的传输中经常对传输数据做加密和编码处理,其中涉及以下几种: 
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解密得到原文。



 

原创粉丝点击