码迷,mamicode.com
首页 > 编程语言 > 详细

C# Java DES加密解密

时间:2014-11-24 14:54:24      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

转自http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html

最近被DES加解密弄得超级郁闷,我用C#的方法加密得到的密文老是跟客户给的Java的加密密文不同,找了很多资料都没有得到解决。直到看到上面的博文,才解决掉这个问题。这里mark一下,以免忘记!

 

 

先来个C#版的:

 1 public class DESHelper
 2    {
 3  
 4        /// <summary>
 5        /// DES加密算法
 6        /// </summary>
 7        /// <param name="encryptString">要加密的字符串</param>
 8        /// <param name="sKey">加密码Key</param>
 9        /// <returns>正确返回加密后的结果,错误返回源字符串</returns>
10        public static string ToDESEncrypt(string encryptString, string sKey)
11        {
12            try
13            {
14  
15                byte[] keyBytes = Encoding.UTF8.GetBytes(sKey);
16                byte[] keyIV = keyBytes;
17                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
18  
19                DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
20  
21                // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
22                desProvider.Mode = CipherMode.ECB;
23                MemoryStream memStream = new MemoryStream();
24                CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
25  
26                crypStream.Write(inputByteArray, 0, inputByteArray.Length);
27                crypStream.FlushFinalBlock();
28                return Convert.ToBase64String(memStream.ToArray());
29  
30            }
31            catch
32            {
33                return encryptString;
34            }
35        }
36  
37  
38        /// <summary>
39        /// DES解密算法
40        /// </summary>
41        /// <param name="decryptString">要解密的字符串</param>
42        /// <param name="sKey">加密Key</param>
43        /// <returns>正确返回加密后的结果,错误返回源字符串</returns>
44        public static string ToDESDecrypt(string decryptString, string sKey)
45        {
46            byte[] keyBytes = Encoding.UTF8.GetBytes(sKey);
47            byte[] keyIV = keyBytes;
48            byte[] inputByteArray = Convert.FromBase64String(decryptString);
49  
50            DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
51  
52            // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
53            desProvider.Mode = CipherMode.ECB;
54            MemoryStream memStream = new MemoryStream();
55            CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
56  
57            crypStream.Write(inputByteArray, 0, inputByteArray.Length);
58            crypStream.FlushFinalBlock();
59            return Encoding.Default.GetString(memStream.ToArray());
60  
61        }
62    }

再来个Java版的

 1 public class DESHelper {
 2  
 3     private byte[] desKey;
 4  
 5     public DES(String desKey) {
 6         this.desKey = desKey.getBytes();
 7     }
 8  
 9     public byte[] desEncrypt(byte[] plainText) throws Exception {
10         SecureRandom sr = new SecureRandom();
11         byte rawKeyData[] = desKey;
12         DESKeySpec dks = new DESKeySpec(rawKeyData);
13         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
14         SecretKey key = keyFactory.generateSecret(dks);
15         Cipher cipher = Cipher.getInstance("DES");
16         cipher.init(Cipher.ENCRYPT_MODE, key, sr);
17         byte data[] = plainText;
18         byte encryptedData[] = cipher.doFinal(data);
19         return encryptedData;
20     }
21  
22     public byte[] desDecrypt(byte[] encryptText) throws Exception {
23         SecureRandom sr = new SecureRandom();
24         byte rawKeyData[] = desKey;
25         DESKeySpec dks = new DESKeySpec(rawKeyData);
26         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
27         SecretKey key = keyFactory.generateSecret(dks);
28         Cipher cipher = Cipher.getInstance("DES");
29         cipher.init(Cipher.DECRYPT_MODE, key, sr);
30         byte encryptedData[] = encryptText;
31         byte decryptedData[] = cipher.doFinal(encryptedData);
32         return decryptedData;
33     }
34  
35     public String encrypt(String input) throws Exception {
36         return base64Encode(desEncrypt(input.getBytes()));
37     }
38  
39     public String decrypt(String input) throws Exception {
40         byte[] result = base64Decode(input);
41         return new String(desDecrypt(result));
42     }
43  
44     public static String base64Encode(byte[] s) {
45         if (s == null)
46             return null;
47         BASE64Encoder b = new sun.misc.BASE64Encoder();
48         return b.encode(s);
49     }
50  
51     public static byte[] base64Decode(String s) throws IOException {
52         if (s == null)
53             return null;
54         BASE64Decoder decoder = new BASE64Decoder();
55         byte[] b = decoder.decodeBuffer(s);
56         return b;
57     }
58      
59 }

 

C# Java DES加密解密

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/aoping/p/4118503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!