惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
T
Tenable Blog
P
Proofpoint News Feed
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
P
Privacy & Cybersecurity Law Blog
美团技术团队
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
量子位
AI
AI
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
Know Your Adversary
Know Your Adversary
IT之家
IT之家
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LangChain Blog
I
Intezer
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
月光博客
月光博客
Recorded Future
Recorded Future
O
OpenAI News
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
S
Security @ Cisco Blogs
Recent Announcements
Recent Announcements
P
Privacy International News Feed
NISL@THU
NISL@THU
MongoDB | Blog
MongoDB | Blog
W
WeLiveSecurity
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
Cyberwarzone
Cyberwarzone
H
Hacker News: Front Page

博客园 - IT爱好者

C# 自定义打印 C# 打印,不显示打印进度对话框 如何解决迅雷插件导致IE10崩溃的问题 ASP.NET MVC 使用Jquery Uploadify 在非IE浏览器下Http Error的解决方案 c#操作access,update语句不执行的解决办法 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用 服务器“**”上的MSDTC不可用的解决办法 通过Package Manager Console 向VS2010安装 EFCodeFirst 删除SVN遗留的无用文件 解决"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本" 修改IIS默认的localhost名称 IIS服务器应用程序不可用的解决办法 使用XML与远程服务器进行交互 小米手机指令大全 获取执行SQL语句的返回结果 ASP.NET中如何向页面写入JavaScript脚本内容 Oracle自增列创建方法 ASP.net中用JSON序列化对象 android Listview 拖动时背景为黑色问题
Java与.NET DES加密解密互转
IT爱好者 · 2011-08-27 · via 博客园 - IT爱好者

项目需要在两个系统间采用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 

.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 * 22), 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