转自http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html
最近被DES加解密弄得超级郁闷,我用C#的方法加密得到的密文老是跟客户给的Java的加密密文不同,找了很多资料都没有得到解决。直到看到上面的博文,才解决掉这个问题。这里mark一下,以免忘记!
先来个C#版的:
public class DESHelper
{ /// <summary>
/// DES加密算法
/// </summary>
/// <param name="encryptString">要加密的字符串</param>
/// <param name="sKey">加密码Key</param>
/// <returns>正确返回加密后的结果,错误返回源字符串</returns>
public static string ToDESEncrypt(string encryptString, string sKey)
{
try
{ byte[] keyBytes = Encoding.UTF8.GetBytes(sKey);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
desProvider.Mode = CipherMode.ECB;
MemoryStream memStream = new MemoryStream();
CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); crypStream.Write(inputByteArray, , inputByteArray.Length);
crypStream.FlushFinalBlock();
return Convert.ToBase64String(memStream.ToArray()); }
catch
{
return encryptString;
}
} /// <summary>
/// DES解密算法
/// </summary>
/// <param name="decryptString">要解密的字符串</param>
/// <param name="sKey">加密Key</param>
/// <returns>正确返回加密后的结果,错误返回源字符串</returns>
public static string ToDESDecrypt(string decryptString, string sKey)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(sKey);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); // java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
desProvider.Mode = CipherMode.ECB;
MemoryStream memStream = new MemoryStream();
CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); crypStream.Write(inputByteArray, , inputByteArray.Length);
crypStream.FlushFinalBlock();
return Encoding.Default.GetString(memStream.ToArray()); }
}
再来个Java版的
public class DESHelper { private byte[] desKey; public DES(String desKey) {
this.desKey = desKey.getBytes();
} public byte[] desEncrypt(byte[] plainText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
byte data[] = plainText;
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
} public byte[] desDecrypt(byte[] encryptText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
} public String encrypt(String input) throws Exception {
return base64Encode(desEncrypt(input.getBytes()));
} public String decrypt(String input) throws Exception {
byte[] result = base64Decode(input);
return new String(desDecrypt(result));
} public static String base64Encode(byte[] s) {
if (s == null)
return null;
BASE64Encoder b = new sun.misc.BASE64Encoder();
return b.encode(s);
} public static byte[] base64Decode(String s) throws IOException {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(s);
return b;
} }