c#加密,java解密(3DES加密)

c#代码 

Java代码  收藏代码
  1. using System;  
  2. using System.Security;  
  3. using System.Security.Cryptography;  
  4. using System.IO;  
  5. using System.Text;  
  6. using System.Threading;namespace WebApplication2  
  7. {  
  8.  /// <summary>  
  9.  /// DES3 的摘要说明。  
  10.  /// </summary>  
  11.  public class DES3  
  12.  {  
  13.   public DES3()  
  14.   {  
  15.   }   
  16.    //密钥  
  17.    private const string sKey = "A3F2569DESJEIWBCJOTY45DYQWF68H1Y";   //矢量,矢量可以为空  
  18.    private const string sIV = "qcDY6X+aPLw=";   //构造一个对称算法  
  19.    private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();  
  20.    #region public string EncryptString(string Value)   /// 加密字符串  
  21.    /// 输入的字符串  
  22.    /// 加密后的字符串  
  23.    public string EncryptString(string Value)   {  
  24.     ICryptoTransform ct;  
  25.     MemoryStream ms;  
  26.     CryptoStream cs;  
  27.     byte[] byt;  
  28.     mCSP.Key = Convert.FromBase64String(sKey);  
  29.     mCSP.IV = Convert.FromBase64String(sIV);    //指定加密的运算模式  
  30.     mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;    //获取或设置加密算法的填充模式  
  31.     mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;  
  32.     ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);  
  33.     byt = Encoding.UTF8.GetBytes(Value);  
  34.     ms = new MemoryStream();  
  35.     cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);  
  36.     cs.Write(byt, 0, byt.Length);  
  37.     cs.FlushFinalBlock();  
  38.     cs.Close();  
  39.     return Convert.ToBase64String(ms.ToArray());  
  40.    }   #endregion  
  41.   } }  


  java代码 

Java代码  收藏代码
  1. package des;import javax.crypto.Cipher;  
  2. import javax.crypto.NoSuchPaddingException;  
  3. import javax.crypto.SecretKey;  
  4. import java.security.NoSuchAlgorithmException;  
  5. import sun.misc.*;  
  6. import java.io.IOException;  
  7. import java.io.UnsupportedEncodingException;  
  8. import javax.crypto.BadPaddingException;  
  9. import javax.crypto.IllegalBlockSizeException;  
  10. import javax.crypto.spec.SecretKeySpec;  
  11. import java.security.*;  
  12. import javax.crypto.SecretKeyFactory;  
  13. import java.security.spec.*;  
  14. import javax.crypto.spec.DESedeKeySpec;  
  15. /** 
  16. 解密 
  17.  */  
  18. public class DES {  
  19.     private static String Algorithm = "DESede";//加密算法的名称  
  20.     private static Cipher c;//密码器  
  21.     private static byte[] cipherByte;  
  22.     private static SecretKey deskey;//密钥  
  23.     private static String keyString = "A3F2569DESJEIWBCJOTY45DYQWF68H1Y";//获得密钥的参数     //对base64编码的string解码成byte数组  
  24.      public byte[] deBase64(String parm) throws IOException {  
  25.         BASE64Decoder dec=new BASE64Decoder();  
  26.         byte[] dnParm = dec.decodeBuffer(parm);  
  27.         System.out.println(dnParm.length);  
  28.         System.out.println(dnParm);  
  29.         return dnParm;  
  30.      }  
  31.     //把密钥参数转为byte数组  
  32.      public byte[] dBase64(String parm) throws IOException {  
  33.         BASE64Decoder dec=new BASE64Decoder();  
  34.         byte[] dnParm = dec.decodeBuffer(parm);  
  35.         return dnParm;  
  36.      }    /** 
  37.      * 对 Byte 数组进行解密 
  38.      * @param buff 要解密的数据 
  39.      * @return 返回加密后的 String 
  40.      */  
  41.      public static String createDecryptor(byte[] buff) throws  
  42.       NoSuchPaddingException, NoSuchAlgorithmException,  
  43.       UnsupportedEncodingException {  
  44.         try {  
  45.            c.init(Cipher.DECRYPT_MODE, deskey);//初始化密码器,用密钥deskey,进入解密模式  
  46.            cipherByte = c.doFinal(buff);  
  47.         }  
  48.         catch(java.security.InvalidKeyException ex){  
  49.             ex.printStackTrace();  
  50.         }  
  51.         catch(javax.crypto.BadPaddingException ex){  
  52.             ex.printStackTrace();  
  53.         }  
  54.         catch(javax.crypto.IllegalBlockSizeException ex){  
  55.             ex.printStackTrace();  
  56.         }  
  57.         return (new String(cipherByte,"UTF-8"));  
  58.      }  
  59.      public void getKey(String key) throws IOException, InvalidKeyException,  
  60.       InvalidKeySpecException {  
  61.       byte[] dKey = dBase64(key);  
  62.         try {          deskey=new javax.crypto.spec.SecretKeySpec(dKey,Algorithm);  
  63.           c = Cipher.getInstance(Algorithm);  
  64.         }  
  65.         catch (NoSuchPaddingException ex) {  
  66.         }  
  67.         catch (NoSuchAlgorithmException ex) {  
  68.         }  
  69.      }  
  70.      public static void main(String args[]) throws IOException,  
  71.       NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,  
  72.       InvalidKeyException, IOException {  
  73.        DES des = new DES();  
  74.        des.getKey(keyString);  
  75.        byte[] dBy = des.deBase64("1ZVasdJJco1qccDnnfQfb8QeaARxhkR6");  
  76.        String dStr = des.createDecryptor(dBy);  
  77.        System.out.println("解:"+dStr);  
  78.      }  
  79. }  


这个可以加解密,不限制加密字符长度

原文地址:https://www.cnblogs.com/changbaishan/p/4259448.html