Java加密和解密

概述:
出于安全考虑,网络的传输中经常对传输数据做加密和编码处理,其中涉及以下几种: 

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. 1、进行MD5加密,得到byte[]  
  2. /** 
  3.   * 进行MD5加密 
  4.   * @param  String 原始的SPKEY 
  5.   * @return  byte[] 指定加密方式为md5后的byte[] 
  6.  */  
  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.   
  23. 2、得到3-DES的密钥匙  
  24. /** 
  25.  * 得到3-DES的密钥匙 
  26.  * 根据根据需要,如密钥匙为24个字节,md5加密出来的是16个字节,因此后面补8个字节的0 
  27.          * @param  String 原始的SPKEY 
  28.  * @return  byte[] 指定加密方式为md5后的byte[] 
  29.  */  
  30.   
  31. private byte[] getEnKey(String spKey)  
  32. {  
  33. byte[] desKey=null;  
  34. try  
  35. {  
  36.   byte[] desKey1 = md5(spKey);  
  37.   desKey = new byte[24];  
  38.   int i = 0;  
  39.   while (i < desKey1.length && i < 24) {  
  40. desKey[i] = desKey1[i];  
  41. i++;  
  42.   }  
  43.   if (i < 24) {           
  44. desKey[i] = 0;  
  45. i++;  
  46.   }  
  47. }  
  48. catch(Exception e){  
  49.   e.printStackTrace();  
  50. }  
  51.   
  52. return desKey;  
  53. }  
  54. 33-DES加密  
  55. /** 
  56.   * 3-DES加密 
  57.   * @param byte[] src 要进行3-DES加密的byte[] 
  58.   * @param   byte[] enKey 3-DES加密密钥 
  59.   * @return  byte[] 3-DES加密后的byte[] 
  60.   */  
  61.    
  62.  public byte[] Encrypt(byte[] src,byte[] enKey)  
  63.  {  
  64.   byte[] encryptedData = null;  
  65.   try  
  66.   {  
  67.   DESedeKeySpec dks = new DESedeKeySpec(enKey);   
  68.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");   
  69.   SecretKey key = keyFactory.generateSecret(dks);   
  70.   Cipher cipher = Cipher.getInstance("DESede");   
  71.   cipher.init(Cipher.ENCRYPT_MODE, key);   
  72.   encryptedData = cipher.doFinal(src);   
  73.   }  
  74.   catch(Exception e)  
  75.   {  
  76.   e.printStackTrace();  
  77.   }  
  78.   return encryptedData;  
  79.  }  
  80. 4、对字符串进行Base64编码  
  81. /** 
  82.   * 对字符串进行Base64编码 
  83.   * @param byte[] src 要进行编码的字符 
  84.   *  
  85.   * @return  String 进行编码后的字符串 
  86.   */  
  87.    
  88.  public String getBase64Encode(byte[] src)  
  89.  {  
  90.   String requestValue="";  
  91.   try{  
  92.     BASE64Encoder base64en = new BASE64Encoder();  
  93.     requestValue=base64en.encode(src);   
  94.     //System.out.println(requestValue);  
  95.   }  
  96.   catch(Exception e){  
  97.     e.printStackTrace();  
  98.   }  
  99.     
  100.   return requestValue;  
  101.  }  
  102.   
  103. 5、根据需要可以去掉字符串的换行符号  
  104. /** 
  105.  * 去掉字符串的换行符号 
  106.  * base64编码3-DES的数据时,得到的字符串有换行符号,根据需要可以去掉 
  107.      */  
  108.   
  109.  private String filter(String str)  
  110.  {  
  111.   String output = null;  
  112.   StringBuffer sb = new StringBuffer();  
  113.   for(int i = 0; i < str.length(); i++)  
  114.   {  
  115.   int asc = str.charAt(i);  
  116.   if(asc != 10 && asc != 13)  
  117.   sb.append(str.subSequence(i, i + 1));  
  118.   }  
  119.   output = new String(sb);  
  120.   return output;  
  121.  }  
  122.   
  123. 6、对字符串进行URLDecoder.encode(strEncoding)编码  
  124. /** 
  125.   * 对字符串进行URLDecoder.encode(strEncoding)编码 
  126.   * @param String src 要进行编码的字符串 
  127.   *  
  128.   * @return  String 进行编码后的字符串 
  129.   */  
  130.    
  131.  public String getURLEncode(String src)  
  132.  {  
  133.   String requestValue="";  
  134.   try{  
  135.     
  136.   requestValue = URLEncoder.encode(src);  
  137.   }  
  138.   catch(Exception e){  
  139.     e.printStackTrace();  
  140.   }  
  141.     
  142.   return requestValue;  
  143.  }  
  144.   
  145. 7、对字符串进行URLDecoder.decode(strEncoding)解码  
  146. /** 
  147.   * 对字符串进行URLDecoder.decode(strEncoding)解码 
  148.   * @param String src 要进行解码的字符串 
  149.   *  
  150.   * @return  String 进行解码后的字符串 
  151.   */  
  152.    
  153.  public String getURLDecoderdecode(String src)  
  154.  {     
  155.   String requestValue="";  
  156.   try{  
  157.     
  158.   requestValue = URLDecoder.decode(src);  
  159.   }  
  160.   catch(Exception e){  
  161.     e.printStackTrace();  
  162.   }  
  163.     
  164.   return requestValue;  
  165.  }  
  166.   
  167. 8、进行3-DES解密(密钥匙等同于加密的密钥匙)  
  168. /** 
  169.   *  
  170.   *进行3-DES解密(密钥匙等同于加密的密钥匙)。  
  171.   * @param byte[]  src 要进行3-DES解密byte[]  
  172.   * @param   String spkey分配的SPKEY 
  173.   * @return  String 3-DES解密后的String 
  174.   */  
  175.  public String deCrypt(byte[] debase64,String spKey)  
  176.  {  
  177.   String strDe = null;  
  178.   Cipher cipher = null;   
  179.   try  
  180.   {  
  181.   cipher=Cipher.getInstance("DESede");  
  182.   byte[] key = getEnKey(spKey);   
  183.   DESedeKeySpec dks = new DESedeKeySpec(key);  
  184.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");  
  185.   SecretKey sKey = keyFactory.generateSecret(dks);  
  186.   cipher.init(Cipher.DECRYPT_MODE, sKey);   
  187.   byte ciphertext[] = cipher.doFinal(debase64);  
  188.   strDe = new String(ciphertext,"UTF-16LE");   
  189.   }  
  190.   catch(Exception ex)  
  191.   {  
  192.    strDe = "";  
  193.    ex.printStackTrace();  
  194.   }  
  195.   return strDe;  
  196.   
  197. 经过以上步骤就可以完成MD5加密,3-DES加密、base64编码传输、base64解码、3-DES解密得到原文。  
  198. 程序全文如下:  
  199. package com.neusoft.test.util.crypt;  
  200.   
  201. import java.io.IOException;  
  202. import java.io.UnsupportedEncodingException;  
  203. import java.net.URLDecoder;  
  204. import java.net.URLEncoder;  
  205. import java.security.MessageDigest;  
  206. import java.text.SimpleDateFormat;  
  207. import java.util.Calendar;  
  208.   
  209.   
  210. import javax.crypto.Cipher;  
  211. import javax.crypto.SecretKey;  
  212. import javax.crypto.SecretKeyFactory;  
  213. import javax.crypto.spec.DESedeKeySpec;  
  214.   
  215. import sun.misc.BASE64Decoder;  
  216. import sun.misc.BASE64Encoder;  
  217.   
  218. /** 
  219.  * <p>Title:加密解密测试</p> 
  220.  * 
  221.  * <p>Description: 加密解密</p> 
  222.  * 
  223.  *<p>Date       : 2005-08-11</p> 
  224.  * 
  225.  * <p>Copyright: Copyright (c) 2005 neusoft</p> 
  226.  * 
  227.  * <p>Company: neusoft</p> 
  228.  * 
  229.  * @author mengk 
  230.  * @version 1.00 
  231.  * 
  232.  * <p>------------------------------------------------------------</p> 
  233.  * <p> 修改历史 </p> 
  234.  * <p>  序号    日期    修改人      修改原因</p> 
  235.  * <p>   1                           </p> 
  236.  */  
  237.   
  238. public class Endecrypt {  
  239.   
  240.   
  241. /** 
  242.  * 进行MD5加密 
  243.  * @param  String 原始的SPKEY 
  244.  * @return  byte[] 指定加密方式为md5后的byte[] 
  245.  */  
  246.   
  247. private byte[] md5(String strSrc)  
  248. {  
  249. byte[] returnByte = null;  
  250. try  
  251. {  
  252. MessageDigest md5 = MessageDigest.getInstance("MD5");   
  253. returnByte = md5.digest(strSrc.getBytes("GBK"));  
  254. }  
  255. catch(Exception e)  
  256. {  
  257. e.printStackTrace();  
  258. }  
  259. return returnByte;  
  260. }    
  261.   
  262.   
  263. /** 
  264.  * 得到3-DES的密钥匙 
  265.  * 根据接口规范,密钥匙为24个字节,md5加密出来的是16个字节,因此后面补8个字节的0 
  266.          * @param  String 原始的SPKEY 
  267.  * @return  byte[] 指定加密方式为md5后的byte[] 
  268.  */  
  269.   
  270. private byte[] getEnKey(String spKey)  
  271. {  
  272. byte[] desKey=null;  
  273. try  
  274. {  
  275.   byte[] desKey1 = md5(spKey);  
  276.   desKey = new byte[24];  
  277.   int i = 0;  
  278.   while (i < desKey1.length && i < 24) {  
  279. desKey[i] = desKey1[i];  
  280. i++;  
  281.   }  
  282.   if (i < 24) {           
  283. desKey[i] = 0;  
  284. i++;  
  285.   }  
  286. }  
  287. catch(Exception e){  
  288.   e.printStackTrace();  
  289. }  
  290.   
  291. return desKey;  
  292. }  
  293.   
  294.   
  295.   
  296. /** 
  297.   * 3-DES加密 
  298.   * @param byte[] src 要进行3-DES加密的byte[] 
  299.   * @param   byte[] enKey 3-DES加密密钥 
  300.   * @return  byte[] 3-DES加密后的byte[] 
  301.   */  
  302.    
  303.  public byte[] Encrypt(byte[] src,byte[] enKey)  
  304.  {  
  305.   byte[] encryptedData = null;  
  306.   try  
  307.   {  
  308.   DESedeKeySpec dks = new DESedeKeySpec(enKey);   
  309.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");   
  310.   SecretKey key = keyFactory.generateSecret(dks);   
  311.   Cipher cipher = Cipher.getInstance("DESede");   
  312.   cipher.init(Cipher.ENCRYPT_MODE, key);   
  313.   encryptedData = cipher.doFinal(src);   
  314.   }  
  315.   catch(Exception e)  
  316.   {  
  317.   e.printStackTrace();  
  318.   }  
  319.   return encryptedData;  
  320.  }  
  321.   
  322.    
  323.   
  324.   
  325.  /** 
  326.   * 对字符串进行Base64编码 
  327.   * @param byte[] src 要进行编码的字符 
  328.   *  
  329.   * @return  String 进行编码后的字符串 
  330.   */  
  331.    
  332.  public String getBase64Encode(byte[] src)  
  333.  {  
  334.   String requestValue="";  
  335.   try{  
  336.     BASE64Encoder base64en = new BASE64Encoder();  
  337.     requestValue=base64en.encode(src);   
  338.     //System.out.println(requestValue);  
  339.   }  
  340.   catch(Exception e){  
  341.     e.printStackTrace();  
  342.   }  
  343.     
  344.   return requestValue;  
  345.  }  
  346.    
  347.    
  348.   
  349. /** 
  350.  * 去掉字符串的换行符号 
  351.  * base64编码3-DES的数据时,得到的字符串有换行符号 
  352.  * ,一定要去掉,否则uni-wise平台解析票根不会成功,  
  353.  * 提示“sp验证失败”。在开发的过程中,因为这个问题让我束手无策, 
  354.  * 一个朋友告诉我可以问联通要一段加密后 的文字,然后去和自己生成的字符串比较, 
  355.  * 这是个不错的调试方法。我最后比较发现我生成的字符串唯一不同的 是多了换行。 
  356.  * 我用c#语言也写了票根请求程序,没有发现这个问题。  
  357.  *  
  358.      */  
  359.   
  360.  private String filter(String str)  
  361.  {  
  362.   String output = null;  
  363.   StringBuffer sb = new StringBuffer();  
  364.   for(int i = 0; i < str.length(); i++)  
  365.   {  
  366.   int asc = str.charAt(i);  
  367.   if(asc != 10 && asc != 13)  
  368.   sb.append(str.subSequence(i, i + 1));  
  369.   }  
  370.   output = new String(sb);  
  371.   return output;  
  372.  }  
  373.   
  374.    
  375.    
  376.    
  377.    
  378.    
  379.    
  380.    
  381.  /** 
  382.   * 对字符串进行URLDecoder.encode(strEncoding)编码 
  383.   * @param String src 要进行编码的字符串 
  384.   *  
  385.   * @return  String 进行编码后的字符串 
  386.   */  
  387.    
  388.  public String getURLEncode(String src)  
  389.  {  
  390.   String requestValue="";  
  391.   try{  
  392.     
  393.   requestValue = URLEncoder.encode(src);  
  394.   }  
  395.   catch(Exception e){  
  396.     e.printStackTrace();  
  397.   }  
  398.     
  399.   return requestValue;  
  400.  }  
  401.    
  402.    
  403.    
  404.    
  405.    
  406.  /** 
  407.   * 3-DES加密 
  408.   * @param String src 要进行3-DES加密的String 
  409.   * @param   String spkey分配的SPKEY 
  410.   * @return  String 3-DES加密后的String 
  411.   */  
  412.    
  413.  public String get3DESEncrypt(String src,String spkey)  
  414.  {  
  415.   String requestValue="";  
  416.   try{  
  417.      
  418.     
  419.     //得到3-DES的密钥匙  
  420.     byte[] enKey = getEnKey(spkey);  
  421.     //要进行3-DES加密的内容在进行/"UTF-16LE/"取字节  
  422.     byte[] src2 = src.getBytes("UTF-16LE");  
  423.     //进行3-DES加密后的内容的字节  
  424.     byte[] encryptedData = Encrypt(src2,enKey);   
  425.       
  426.      
  427.     //进行3-DES加密后的内容进行BASE64编码  
  428.     String base64String = getBase64Encode(encryptedData);  
  429.  //BASE64编码去除换行符后  
  430.     String base64Encrypt = filter(base64String);        
  431.       
  432.     //对BASE64编码中的HTML控制码进行转义的过程  
  433.     requestValue=getURLEncode(base64Encrypt);   
  434.     //System.out.println(requestValue);  
  435.   }  
  436.   catch(Exception e){  
  437.     e.printStackTrace();  
  438.   }  
  439.     
  440.   return requestValue;  
  441.  }  
  442.   
  443.    
  444.    
  445.  /** 
  446.   * 对字符串进行URLDecoder.decode(strEncoding)解码 
  447.   * @param String src 要进行解码的字符串 
  448.   *  
  449.   * @return  String 进行解码后的字符串 
  450.   */  
  451.    
  452.  public String getURLDecoderdecode(String src)  
  453.  {     
  454.   String requestValue="";  
  455.   try{  
  456.     
  457.   requestValue = URLDecoder.decode(src);  
  458.   }  
  459.   catch(Exception e){  
  460.     e.printStackTrace();  
  461.   }  
  462.     
  463.   return requestValue;  
  464.  }  
  465.    
  466.    
  467.    
  468.  /** 
  469.   *  
  470.   *进行3-DES解密(密钥匙等同于加密的密钥匙)。  
  471.   * @param byte[]  src 要进行3-DES解密byte[]  
  472.   * @param   String spkey分配的SPKEY 
  473.   * @return  String 3-DES解密后的String 
  474.   */  
  475.  public String deCrypt(byte[] debase64,String spKey)  
  476.  {  
  477.   String strDe = null;  
  478.   Cipher cipher = null;   
  479.   try  
  480.   {  
  481.   cipher=Cipher.getInstance("DESede");  
  482.   byte[] key = getEnKey(spKey);   
  483.   DESedeKeySpec dks = new DESedeKeySpec(key);  
  484.   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");  
  485.   SecretKey sKey = keyFactory.generateSecret(dks);  
  486.   cipher.init(Cipher.DECRYPT_MODE, sKey);   
  487.   byte ciphertext[] = cipher.doFinal(debase64);  
  488.   strDe = new String(ciphertext,"UTF-16LE");   
  489.   }  
  490.   catch(Exception ex)  
  491.   {  
  492.    strDe = "";  
  493.    ex.printStackTrace();  
  494.   }  
  495.   return strDe;  
  496.  }  
  497.   
  498.   
  499.    
  500.  /** 
  501.   * 3-DES解密 
  502.   * @param String src 要进行3-DES解密的String 
  503.   * @param   String spkey分配的SPKEY 
  504.   * @return  String 3-DES加密后的String 
  505.   */  
  506.    
  507.  public String get3DESDecrypt(String src,String spkey)  
  508.  {  
  509.   String requestValue="";  
  510.   try{  
  511.      
  512.     
  513.     //得到3-DES的密钥匙  
  514.      
  515.           //URLDecoder.decodeTML控制码进行转义的过程  
  516.     String URLValue=getURLDecoderdecode(src);   
  517.       
  518.     //进行3-DES加密后的内容进行BASE64编码  
  519.        
  520.       BASE64Decoder base64Decode = new BASE64Decoder();  
  521. byte[] base64DValue = base64Decode.decodeBuffer(URLValue);   
  522.       
  523.     //要进行3-DES加密的内容在进行/"UTF-16LE/"取字节  
  524.   
  525. requestValue = deCrypt(base64DValue,spkey);  
  526.   }  
  527.   catch(Exception e){  
  528.     e.printStackTrace();  
  529.   }  
  530.   return requestValue;  
  531.  }  
  532.   
  533.    
  534.    
  535. public static void main(String[] args) {  
  536. Endecrypt test = new Endecrypt();  
  537. String oldString = "毒素发";  
  538.   
  539. String SPKEY = "1234";  
  540. System.out.println("1。分配的SPKEY为:  "+SPKEY);  
  541. System.out.println("2。的内容为:  "+oldString);  
  542. String  reValue = test.get3DESEncrypt(oldString,SPKEY);  
  543. reValue = reValue.trim().intern();  
  544. System.out.println("进行3-DES加密后的内容: "+reValue);  
  545. String reValue2 = test.get3DESDecrypt(reValue,SPKEY);  
  546. System.out.println("进行3-DES解密后的内容: "+reValue2);  
  547. }  
  548. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值