A-A+

Java和.NET的DES加密

2014年11月10日 技术 暂无评论 阅读 2,633 次

.NET的DES加密算法如下:

  1. /// <summary>  
  2. /// DES加密算法必须使用Base64的Byte对象  
  3. /// </summary>  
  4. /// <param name="data">待加密的字符数据</param>  
  5. /// <param name="key">密匙,长度必须为64位(byte[8]))</param>  
  6. /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  7. /// <returns>加密后的字符</returns>  
  8. private string EnDES(string data, byte[] key, byte[] iv)
  9. {
  10.     DES des = DES.Create();
  11.     //这行代码很重要,需要根据不同的字符串选择不同的转换格式  
  12.     byte[] tmp = Encoding.Unicode.GetBytes(data);
  13.     Byte[] encryptoData;
  14.     ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
  15.     using (MemoryStream memoryStream = new MemoryStream())
  16.     {
  17.         using (var cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  18.         {
  19.             using (StreamWriter writer = new StreamWriter(cs))
  20.             {
  21.                 writer.Write(data);
  22.                 writer.Flush();
  23.             }
  24.         }
  25.         encryptoData = memoryStream.ToArray();
  26.     }
  27.     des.Clear();
  28.     return Convert.ToBase64String(encryptoData);
  29.   }
  30. /// <summary>  
  31. /// DES解密算法  
  32. /// </summary>  
  33. /// <param name="data">待加密的字符数据</param>  
  34. /// <param name="key">密匙,长度必须为64位(byte[8]))</param>  
  35. /// <param name="iv">iv向量,长度必须为64位(byte[8])</param>  
  36. /// <returns>加密后的字符</returns>  
  37. private string DeDes(string data, Byte[] key, Byte[] iv)
  38. {
  39.     string resultData = string.Empty;
  40.     //这行代码很重要  
  41.     Byte[] tmpData = Convert.FromBase64String(data);//转换的格式挺重要  
  42.     DES des = DES.Create();
  43.     ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
  44.     using (var memoryStream = new MemoryStream(tmpData))
  45.     {
  46.         using (var cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
  47.         {
  48.             StreamReader reader = new StreamReader(cs);  
  49.             resultData = reader.ReadLine();
  50.         }
  51.     }  
  52.     return resultData;  
  53. }  

而在Java中,加密代码如下(以JSP为例):

  1. <%@ page import="javax.crypto.spec.DESKeySpec"%>
  2. <%@ page import="javax.crypto.SecretKey"%>
  3. <%@ page import="javax.crypto.Cipher"%>
  4. <%@ page import="javax.crypto.SecretKeyFactory"%>
  5. <%@ page import="javax.crypto.spec.IvParameterSpec"%>
  6. <%@ page import="sun.misc.BASE64Decoder"%>
  7. <%@ page import="sun.misc.BASE64Encoder"%>
  8. <%!
  9. /**
  10.  * 
  11.  * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
  12.  * 
  13.  * 方法: void getKey(String strKey)从strKey的字条生成一个Key
  14.  * 
  15.  * String getEncString(String strMing)对strMing进行加密,返回String密文 String
  16.  * getDesString(String strMi)对strMin进行解密,返回String明文
  17.  * 
  18.  * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
  19.  * byteD)byte[]型的解密
  20.  */
  21. public class DESCrypt {
  22.     SecretKey key;
  23.     IvParameterSpec zeroIv;
  24.     public DESCrypt(){
  25.         getKey("A&&&&%G2");
  26.     }
  27.     /**
  28.      * 根据参数生成KEY
  29.      * 
  30.      * @param strKey
  31.      */
  32.     public  void getKey(String strKey) {
  33.         try {
  34.             zeroIv = new IvParameterSpec(strKey.getBytes("UTF-8"));
  35.             DESKeySpec desKey = new DESKeySpec(strKey.getBytes("UTF-8"));
  36.             //创建一个密匙工厂,然后用它把DESKeySpec转换成
  37.             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  38.             key = keyFactory.generateSecret(desKey);
  39.         } catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     /**
  44.      * 加密String明文输入,String密文输出
  45.      * 
  46.      * @param strMing
  47.      * @return
  48.      */
  49.     public String getEncString(String strMing) {
  50.         byte[] byteMi = null;
  51.         byte[] byteMing = null;
  52.         String strMi = "";
  53.         BASE64Encoder base64en = new BASE64Encoder();
  54.         try {
  55.             byteMing = strMing.getBytes("UTF8");
  56.             byteMi = this.getEncCode(byteMing);
  57.             strMi = base64en.encode(byteMi);
  58.         } catch (Exception e) {
  59.             e.printStackTrace();
  60.         } finally {
  61.             base64en = null;
  62.             byteMing = null;
  63.             byteMi = null;
  64.         }
  65.         return strMi;
  66.     }
  67.     /**
  68.      * 解密 以String密文输入,String明文输出
  69.      * 
  70.      * @param strMi
  71.      * @return
  72.      */
  73.     public String getDesString(String strMi) {
  74.         BASE64Decoder base64De = new BASE64Decoder();
  75.         byte[] byteMing = null;
  76.         byte[] byteMi = null;
  77.         String strMing = "";
  78.         try {
  79.             byteMi = base64De.decodeBuffer(strMi);
  80.             byteMing = this.getDesCode(byteMi);
  81.             strMing = new String(byteMing, "UTF8");
  82.         } catch (Exception e) {
  83.             e.printStackTrace();
  84.         } finally {
  85.             base64De = null;
  86.             byteMing = null;
  87.             byteMi = null;
  88.         }
  89.         return strMing;
  90.     }
  91.     /**
  92.      * 加密以byte[]明文输入,byte[]密文输出
  93.      * 
  94.      * @param byteS
  95.      * @return
  96.      */
  97.     private byte[] getEncCode(byte[] byteS) {
  98.         byte[] byteFina = null;
  99.         Cipher cipher;
  100.         try {
  101.             cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  102.             cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
  103.             byteFina = cipher.doFinal(byteS);
  104.         } catch (Exception e) {
  105.             e.printStackTrace();
  106.         } finally {
  107.             cipher = null;
  108.         }
  109.         return byteFina;
  110.     }
  111.     /**
  112.      * 解密以byte[]密文输入,以byte[]明文输出
  113.      * 
  114.      * @param byteD
  115.      * @return
  116.      */
  117.     private byte[] getDesCode(byte[] byteD) {
  118.         Cipher cipher;
  119.         byte[] byteFina = null;
  120.         try {
  121.             cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  122.             cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
  123.             byteFina = cipher.doFinal(byteD);
  124.         } catch (Exception e) {
  125.             e.printStackTrace();
  126.         } finally {
  127.             cipher = null;
  128.         }
  129.         return byteFina;
  130.     }
  131. }
  132. %>

要想让.Net加密的字符串能够成功被Java解密,或者Java加密的字符串能够被.NET成功解密,其实只要保证Key和Iv相同即可。

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: