当前位置: 代码迷 >> J2SE >>         catch(javax.crypto.IllegalBlockSizeException
  详细解决方案

        catch(javax.crypto.IllegalBlockSizeException

热度:102   发布时间:2016-04-24 16:28:32.0
c#加密,java解密
如题,现在代码已经可以用了,就是有个问题,即用C#加密后,JAVA解密,部分解密过后的数据会显示为乱码,小弟以为是字符集转换的问题,找了半天毛病都没找到,请各位高手帮个忙

c#代码

using   System;
using   System.Security;
using   System.Security.Cryptography;
using   System.IO;
using   System.Text;
using   System.Threading;
namespace   WebApplication2
{
  ///   <summary>
  ///   DES3   的摘要说明。
  ///   </summary>
  public   class   DES3
  {
    public   DES3()
    {
    }  
      //密钥
      private   const   string   sKey   =   "A3F2569DESJEIWBCJOTY45DYQWF68H1Y ";
      //矢量,矢量可以为空
      private   const   string   sIV   =   "qcDY6X+aPLw= ";
      //构造一个对称算法
      private   SymmetricAlgorithm   mCSP   =   new   TripleDESCryptoServiceProvider();
      #region   public   string   EncryptString(string   Value)
      ///   加密字符串
      ///   输入的字符串
      ///   加密后的字符串
      public   string   EncryptString(string   Value)
      {
        ICryptoTransform   ct;
        MemoryStream   ms;
        CryptoStream   cs;
        byte[]   byt;
        mCSP.Key   =   Convert.FromBase64String(sKey);
        mCSP.IV   =   Convert.FromBase64String(sIV);
        //指定加密的运算模式
        mCSP.Mode   =   System.Security.Cryptography.CipherMode.ECB;
        //获取或设置加密算法的填充模式
        mCSP.Padding   =   System.Security.Cryptography.PaddingMode.PKCS7;
        ct   =   mCSP.CreateEncryptor(mCSP.Key,   mCSP.IV);
        byt   =   Encoding.UTF8.GetBytes(Value);
        ms   =   new   MemoryStream();
        cs   =   new   CryptoStream(ms,   ct,   CryptoStreamMode.Write);
        cs.Write(byt,   0,   byt.Length);
        cs.FlushFinalBlock();
        cs.Close();
        return   Convert.ToBase64String(ms.ToArray());
      }
      #endregion
    }
  }
 
 
java代码
 
package   des;
import   javax.crypto.Cipher;
import   javax.crypto.NoSuchPaddingException;
import   javax.crypto.SecretKey;
import   java.security.NoSuchAlgorithmException;
import   sun.misc.*;
import   java.io.IOException;
import   java.io.UnsupportedEncodingException;
import   javax.crypto.BadPaddingException;
import   javax.crypto.IllegalBlockSizeException;
import   javax.crypto.spec.SecretKeySpec;
import   java.security.*;
import   javax.crypto.SecretKeyFactory;
import   java.security.spec.*;
import   javax.crypto.spec.DESedeKeySpec;
/**
解密
  */
public   class   DES   {
        private   static   String   Algorithm   =   "DESede ";//加密算法的名称
  相关解决方案