博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

C# Java DES加密解密

Posted on 2014-06-18 17:27  木头人Ricky  阅读(353)  评论(0编辑  收藏  举报

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, 0, 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, 0, 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;
    }
     
}