java與C#互相使用DES加解密

 

Java代碼
複製代碼
private static String Algorithm = "DESede";
   
    private static final String Default_Key = "A3F2DEI569WBCJSJEOTY45DYQWF68H1Y";
   
    public static String encryptString(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
        return encryptString(Default_Key, value);
    }
   
   
    public static String encryptString(String key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
        byte[] bytesKey = null, bytes = null, bytesCipher = null;
        SecretKey deskey = null;
        if (value == null)
            new IllegalArgumentException("待加密字串不允許爲空");
        //密碼器
        Cipher desCipher = Cipher.getInstance(Algorithm);
        try{
            bytesKey = Base64.decodeBase64(key);
            deskey = new SecretKeySpec(bytesKey, Algorithm);
            bytes = value.getBytes();//待解密的字串
            desCipher.init(Cipher.ENCRYPT_MODE, deskey);//初始化密碼器,用密鑰deskey,進入解密模式 
            bytesCipher = desCipher.doFinal(bytes);
            return Base64.encodeBase64String(bytesCipher).trim();
        }
        finally{
            bytesKey = null;
            bytes = null;
            bytesCipher = null;
        }
     }
   
    public static String decryptString(String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
        return decryptString(Default_Key, value);
    }
   
    public static String decryptString(String key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
        byte[] bytesKey = null, bytes = null, bytesCipher = null;
        SecretKey deskey = null;
        if (value == null)
            new IllegalArgumentException("待解密字串不允許爲空");
        //密碼器
        Cipher desCipher = Cipher.getInstance(Algorithm);
        try{
            bytesKey = Base64.decodeBase64(key);
            deskey = new SecretKeySpec(bytesKey, Algorithm);
            bytes = Base64.decodeBase64(value);//加密後的字串
            desCipher.init(Cipher.DECRYPT_MODE, deskey);//初始化密碼器,用密鑰deskey,進入解密模式 
            bytesCipher = desCipher.doFinal(bytes);
            return (new String(bytesCipher,"UTF-8"));
        }
        finally{
            bytesKey = null;
            bytes = null;
            bytesCipher = null;
        }
     }
     public static void main(String args[]){
         try{
             String strEncode = encryptString("我是誰");
             System.out.println("加密返回:"+strEncode);
             String strOrg = decryptString(strEncode);
             System.out.println("解密返回:"+strOrg);
         }
         catch(Exception e){
             e.printStackTrace();
         }
     } 
複製代碼
C#代碼
複製代碼
#region DESEnCode DES加密
        /// <summary>
        
/// 默認Key
        
/// </summary>
        private const string Default_Key = "A3F2DEI569WBCJSJEOTY45DYQWF68H1Y";
        /// <summary>
        
/// 默認IV 矢量,矢量可以爲空 
        
/// </summary>
        private const string Default_IV = "qcDX+Y6aPLw=";
        /// <summary>
        
/// 加密字符串
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public static string EncryptString(string value)
        {
            return EncryptString(Default_Key, Default_IV, value);
        }
        /// <summary>
        
/// 加密字符串
        
/// </summary>
        
/// <param name="key">Key鍵</param>
        
/// <param name="iv">向量</param>
        
/// <param name="value">要加密的串</param>
        
/// <returns>加密後的字串</returns>
        public static string EncryptString(string key, string iv, string value)
        {
            using (SymmetricAlgorithm desCSP = new TripleDESCryptoServiceProvider())
            {
                desCSP.Key = Convert.FromBase64String(key);
                desCSP.IV = Convert.FromBase64String(iv);    //指定加密的運算模式 
                desCSP.Mode = System.Security.Cryptography.CipherMode.ECB;    //獲取或設置加密算法的填充模式 
                desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ICryptoTransform ctTrans = desCSP.CreateEncryptor(desCSP.Key, desCSP.IV);
                byte[] bytes = Encoding.UTF8.GetBytes(value);
                using (MemoryStream memStream = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(memStream, ctTrans, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(bytes, 0, bytes.Length);
                        csEncrypt.FlushFinalBlock();
                        csEncrypt.Close();
                    }
                    bytes = null//清理內存
                    return Convert.ToBase64String(memStream.ToArray());
                }
            }
        }
        /// <summary>
        
/// 解密字符串
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>
        public static string DecryptString(string value)
        {
            return DecryptString(Default_Key, Default_IV, value);
        }
        /// <summary>
        
/// 解密字符串
        
/// </summary>
        
/// <param name="key">Key鍵</param>
        
/// <param name="iv">向量</param>
        
/// <param name="value">要解密的串</param>
        
/// <returns>解密後的字串</returns>
        public static string DecryptString(string key, string iv, string value)
        {
            using (SymmetricAlgorithm desCSP = new TripleDESCryptoServiceProvider())
            {
                desCSP.Key = Convert.FromBase64String(key);
                desCSP.IV = Convert.FromBase64String(iv);    //指定加密的運算模式 
                desCSP.Mode = System.Security.Cryptography.CipherMode.ECB;    //獲取或設置加密算法的填充模式 
                desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ICryptoTransform ctTrans = desCSP.CreateDecryptor(desCSP.Key, desCSP.IV);
                byte[] bytes = Convert.FromBase64String(value);
                byte[] bytesOut = new byte[10240];
                int iReadLen = 0;
                using (MemoryStream memStream = new MemoryStream(bytes))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(memStream, ctTrans, CryptoStreamMode.Read))
                    {
                        using (MemoryStream outStream = new MemoryStream())
                        {
                            while ((iReadLen = csDecrypt.Read(bytesOut, 0, bytesOut.Length)) > 0)
                            {
                                outStream.Write(bytesOut, 0, iReadLen);
                            }
                            bytes = null;
                            bytesOut = null;//清理內存
                            return System.Text.Encoding.UTF8.GetString(outStream.ToArray());
                        }
                    }
                }
            }
        }
        #endregion
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章