Java和C#利用DES算法加密和解密数据

今天晚上折腾了很久,一直在想, .NET 的 DES算法 加密的数据 JAVA能解吗,看了下 微软的SDK帮助文档,其实就是一个加密算法,只要知道IV和KEY就能解密数据.
C# SDK API地址:

http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.descryptoserviceprovider%28v=VS.80%29.aspx

.NET加密实现(iv我用一个byte数组,值是1-8)

1
2
3
4
5
6
7
8
9
10
11
12
public static string EncryptDES(string encryptString, string encryptKey)
{
    DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
    byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
    byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
    MemoryStream mStream = new MemoryStream();
    CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, new byte[]{1,2,3,4,5,6,6,7,8}), CryptoStreamMode.Write);
    cStream.Write(inputByteArray, 0, inputByteArray.Length);
    cStream.FlushFinalBlock();
 
    return Convert.ToBase64String(mStream.ToArray());
}

.NET解密实现(解密的key和iv一定要一样,否则解密失败)

1
2
3
4
5
6
7
8
9
10
11
public static string DecryptDES(string decryptString, string decryptKey)
{
    byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
    byte[] inputByteArray = Convert.FromBase64String(decryptString);
    DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
    MemoryStream mStream = new MemoryStream();
    CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, new byte[]{1,2,3,4,5,6,6,7,8}), CryptoStreamMode.Write);
    cStream.Write(inputByteArray, 0, inputByteArray.Length);
    cStream.FlushFinalBlock();
    return Encoding.UTF8.GetString(mStream.ToArray());
}

下面是JAVA版的
JAVA加密:

1
2
3
4
5
6
7
8
9
public static String encryptDES(String encryptString, String encryptKey) throws Exception {
	IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
	SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
	Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
	cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
	byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
 
	return new BASE64Encoder().encode(encryptedData);
}

java解密:

1
2
3
4
5
6
7
8
9
public static String decryptDES(String decryptString, String decryptKey) throws Exception {
	byte[] byteMi = new BASE64Decoder().decodeBuffer(decryptString);
	IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
	SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
	Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
	cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
	byte decryptedData[] = cipher.doFinal(byteMi);
	return new String(decryptedData);
}

另外再贴上两个装换工具
java版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static byte[] convertHexString(String ss)    
    {    
    byte digest[] = new byte[ss.length() / 2];    
    for(int i = 0; i < digest.length; i++)    
    {    
    String byteString = ss.substring(2 * i, 2 * i + 2);    
    int byteValue = Integer.parseInt(byteString, 16);    
    digest[i] = (byte)byteValue;    
    }    
 
    return digest;    
    } 
 
public static String toHexString(byte b[]) {   
        StringBuffer hexString = new StringBuffer();   
        for (int i = 0; i < b.length; i++) {   
            String plainText = Integer.toHexString(0xff & b[i]);   
            if (plainText.length() < 2)   
                plainText = "0" + plainText;   
            hexString.append(plainText);   
        }   
 
        return hexString.toString();   
    }

C#版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 private static string ToHexString(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            string temp = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                int inttemp = (int)bytes[i];
                temp += Convert.ToString((byte)inttemp, 16);
            }
            return temp;
        }
 
     private static string HexToString(string str)
        {
            // byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] buff = new byte[str.Length / 2];
            for (int i = 0; i < buff.Length; i++)
            {
 
                buff[i] = (byte)((int)byte.Parse(str.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            string aa = Encoding.UTF8.GetString(buff);
            return aa;
        }
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>