Java与.NET DES加密解密互转
项目需要在两个系统间采用DES加密,一个系统为Java开发的,另外一个.NET开发的
在网上找了很多写法但加密出的数据两个系统都无法匹配,
在做了小修改以后终于可以用了,已经测试过
上代码:
Java代码:
1 import javax.crypto.Cipher;
2 import javax.crypto.SecretKey;
3 import javax.crypto.SecretKeyFactory;
4 import javax.crypto.spec.DESKeySpec;
5 import javax.crypto.spec.IvParameterSpec;
6
7
8 public class Des {
9 private byte[] desKey;
10
11
12 //解密数据
13 public static String decrypt(String message,String key) throws Exception {
14
15 byte[] bytesrc =convertHexString(message);
16 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
17 DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
18 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
19 SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
20 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
21
22 cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
23
24 byte[] retByte = cipher.doFinal(bytesrc);
25 return new String(retByte);
26 }
27
28 public static byte[] encrypt(String message, String key)
29 throws Exception {
30 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
31
32 DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
33
34 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
35 SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
36 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
37 cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
38
39 return cipher.doFinal(message.getBytes("UTF-8"));
40 }
41
42 public static byte[] convertHexString(String ss)
43 {
44 byte digest[] = new byte[ss.length() / 2];
45 for(int i = 0; i < digest.length; i++)
46 {
47 String byteString = ss.substring(2 * i, 2 * i + 2);
48 int byteValue = Integer.parseInt(byteString, 16);
49 digest[i] = (byte)byteValue;
50 }
51
52 return digest;
53 }
54
55
56 public static void main(String[] args) throws Exception {
57 String key = "12345678";
58 String value="test1234 ";
59 String jiami=java.net.URLEncoder.encode(value, "utf-8").toLowerCase();
60
61 System.out.println("加密数据:"+jiami);
62 String a=toHexString(encrypt(jiami, key)).toUpperCase();
63
64
65 System.out.println("加密后的数据为:"+a);
66 String b=java.net.URLDecoder.decode(decrypt(a,key), "utf-8") ;
67 System.out.println("解密后的数据:"+b);
68
69 }
70
71
72 public static String toHexString(byte b[]) {
73 StringBuffer hexString = new StringBuffer();
74 for (int i = 0; i < b.length; i++) {
75 String plainText = Integer.toHexString(0xff & b[i]);
76 if (plainText.length() < 2)
77 plainText = "0" + plainText;
78 hexString.append(plainText);
79 }
80
81 return hexString.toString();
82 }
83
84 }
3 import javax.crypto.SecretKeyFactory;
4 import javax.crypto.spec.DESKeySpec;
5 import javax.crypto.spec.IvParameterSpec;
6
7
8 public class Des {
9 private byte[] desKey;
10
11
12 //解密数据
13 public static String decrypt(String message,String key) throws Exception {
14
15 byte[] bytesrc =convertHexString(message);
16 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
17 DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
18 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
19 SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
20 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
21
22 cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
23
24 byte[] retByte = cipher.doFinal(bytesrc);
25 return new String(retByte);
26 }
27
28 public static byte[] encrypt(String message, String key)
29 throws Exception {
30 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
31
32 DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
33
34 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
35 SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
36 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
37 cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
38
39 return cipher.doFinal(message.getBytes("UTF-8"));
40 }
41
42 public static byte[] convertHexString(String ss)
43 {
44 byte digest[] = new byte[ss.length() / 2];
45 for(int i = 0; i < digest.length; i++)
46 {
47 String byteString = ss.substring(2 * i, 2 * i + 2);
48 int byteValue = Integer.parseInt(byteString, 16);
49 digest[i] = (byte)byteValue;
50 }
51
52 return digest;
53 }
54
55
56 public static void main(String[] args) throws Exception {
57 String key = "12345678";
58 String value="test1234 ";
59 String jiami=java.net.URLEncoder.encode(value, "utf-8").toLowerCase();
60
61 System.out.println("加密数据:"+jiami);
62 String a=toHexString(encrypt(jiami, key)).toUpperCase();
63
64
65 System.out.println("加密后的数据为:"+a);
66 String b=java.net.URLDecoder.decode(decrypt(a,key), "utf-8") ;
67 System.out.println("解密后的数据:"+b);
68
69 }
70
71
72 public static String toHexString(byte b[]) {
73 StringBuffer hexString = new StringBuffer();
74 for (int i = 0; i < b.length; i++) {
75 String plainText = Integer.toHexString(0xff & b[i]);
76 if (plainText.length() < 2)
77 plainText = "0" + plainText;
78 hexString.append(plainText);
79 }
80
81 return hexString.toString();
82 }
83
84 }
.NET代码
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Security.Cryptography;
12 using System.IO;
13 using System.Text;
14 public class TestDes{
15 //cookies加密密钥
16 public static string DES_Key = "12345678";
17
18 #region DESEnCode DES加密
19 public static string DESEnCode(string pToEncrypt, string sKey)
20 {
21 pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
22 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
23 byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
24
25
26 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
27 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
28 MemoryStream ms = new MemoryStream();
29 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
30
31 cs.Write(inputByteArray, 0, inputByteArray.Length);
32 cs.FlushFinalBlock();
33
34 StringBuilder ret = new StringBuilder();
35 foreach (byte b in ms.ToArray())
36 {
37 ret.AppendFormat("{0:X2}", b);
38 }
39 ret.ToString();
40 return ret.ToString();
41 }
42 #endregion
43
44 #region DESDeCode DES解密
45 public static string DESDeCode(string pToDecrypt, string sKey)
46 {
47 // HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
48 // HttpContext.Current.Response.End();
49 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
50
51 byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
52 for (int x = 0; x < pToDecrypt.Length / 2; x++)
53 {
54 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
55 inputByteArray[x] = (byte)i;
56 }
57
58 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
59 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
60 MemoryStream ms = new MemoryStream();
61 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
62 cs.Write(inputByteArray, 0, inputByteArray.Length);
63 cs.FlushFinalBlock();
64
65 StringBuilder ret = new StringBuilder();
66
67 return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
68 }
69 #endregion
70
71 public TestDes()
72 {
73 //
74 // TODO: 在此处添加构造函数逻辑
75 //
76 }
77 }
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using System.Security.Cryptography;
12 using System.IO;
13 using System.Text;
14 public class TestDes{
15 //cookies加密密钥
16 public static string DES_Key = "12345678";
17
18 #region DESEnCode DES加密
19 public static string DESEnCode(string pToEncrypt, string sKey)
20 {
21 pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
22 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
23 byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
24
25
26 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
27 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
28 MemoryStream ms = new MemoryStream();
29 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
30
31 cs.Write(inputByteArray, 0, inputByteArray.Length);
32 cs.FlushFinalBlock();
33
34 StringBuilder ret = new StringBuilder();
35 foreach (byte b in ms.ToArray())
36 {
37 ret.AppendFormat("{0:X2}", b);
38 }
39 ret.ToString();
40 return ret.ToString();
41 }
42 #endregion
43
44 #region DESDeCode DES解密
45 public static string DESDeCode(string pToDecrypt, string sKey)
46 {
47 // HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
48 // HttpContext.Current.Response.End();
49 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
50
51 byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
52 for (int x = 0; x < pToDecrypt.Length / 2; x++)
53 {
54 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
55 inputByteArray[x] = (byte)i;
56 }
57
58 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
59 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
60 MemoryStream ms = new MemoryStream();
61 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
62 cs.Write(inputByteArray, 0, inputByteArray.Length);
63 cs.FlushFinalBlock();
64
65 StringBuilder ret = new StringBuilder();
66
67 return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
68 }
69 #endregion
70
71 public TestDes()
72 {
73 //
74 // TODO: 在此处添加构造函数逻辑
75 //
76 }
77 }