现在的位置: 首页 > 综合 > 正文

[转]DES加密 java与.net可以相互加密解密两种方法

2013年06月29日 ⁄ 综合 ⁄ 共 3591字 ⁄ 字号 评论关闭

通过这个方法可以实现java和C#相互加密与解密 并能保持解密出来一致


方法一:通过.NET的key和VI来生成对应于java的key

java:

   

Java代码  收藏代码
  1. <span style="color: #888888;">import java.security.Key;  
  2. import java.security.spec.AlgorithmParameterSpec;  
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKeyFactory;  
  6. import javax.crypto.spec.DESKeySpec;  
  7. import javax.crypto.spec.IvParameterSpec;  
  8.   
  9. import sun.misc.BASE64Decoder;  
  10. import sun.misc.BASE64Encoder;  
  11.   
  12. public class CryptoTools {  
  13.   
  14.     private static final byte[] DESkey = { (byte0x15, (byte0xE7,  
  15.             (byte0xA1, (byte0x22, (byte0x96, (byte0x8B, (byte0x24,  
  16.             (byte0xFA };// 设置密钥,略去  
  17.   
  18.     private static final byte[] DESIV = { (byte0xCE, (byte0x35, (byte0x5,  
  19.             (byte0xD, (byte0x98, (byte0x91, (byte0x8, (byte0xA };// 设置向量,略去  
  20.   
  21.     static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现  
  22.     private static Key key = null;  
  23.   
  24.     public CryptoTools() throws Exception {  
  25.         DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数  
  26.         iv = new IvParameterSpec(DESIV);// 设置向量  
  27.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂  
  28.         key = keyFactory.generateSecret(keySpec);// 得到密钥对象  
  29.   
  30.     }  
  31.   
  32.     public String encode(String data) throws Exception {  
  33.         Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher  
  34.         enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量  
  35.         byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));  
  36.         BASE64Encoder base64Encoder = new BASE64Encoder();  
  37.         return base64Encoder.encode(pasByte);  
  38.     }  
  39.   
  40.     public String decode(String data) throws Exception {  
  41.         Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
  42.         deCipher.init(Cipher.DECRYPT_MODE, key, iv);  
  43.         BASE64Decoder base64Decoder = new BASE64Decoder();  
  44.         byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));  
  45.         return new String(pasByte, "UTF-8");  
  46.     }  
  47.   
  48.     public static void main(String[] args) throws Exception {  
  49.   
  50.         CryptoTools tools = new CryptoTools();  
  51.         System.out.println("加密:" + tools.encode("天下"));  
  52.         System.out.println("解密:" + tools.decode(tools.encode("天下")));  
  53.     }  
  54.   
  55. }</span>  

 



×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××




方法二:通过java的key来生成对应于.NET的key和VI

C#:

C#代码  收藏代码
  1. <span style="color: #888888;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9.     
  10. using System.Configuration;     
  11. using System.Web;     
  12. using System.Security.Cryptography;     
  13. using System.IO;     
  14.     
  15.   
  16. namespace DES  
  17. {  
  18.     public partial class Form1 : Form  
  19.     {  
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         private void button1_Click(object sender, EventArgs e)  
  26.         {  
  27.             string jiami = textBox1.Text;  
  28.            textBox2.Text= DESEnCode(jiami, "11111111");  
  29.         }  
  30.   
  31.           public static string DES_Key = "11111111";    
  32.   
  33.     #region DESEnCode DES加密     
  34.     public static string DESEnCode(string pToEncrypt, string sKey)     
  35.     {  
  36.        // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);     
  37.         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  38.         byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);     
  39.     
  40.         //建立加密对象的密钥和偏移量      
  41.         //原文使用ASCIIEnc

抱歉!评论已关闭.