首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

catch(javax.crypto.IllegalBlockSizeException

2011-12-26 
c#加密,java解密如题,现在代码已经可以用了,就是有个问题,即用C#加密后,JAVA解密,部分解密过后的数据会显

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 ";//加密算法的名称


        private   static   Cipher   c;//密码器
        private   static   byte[]   cipherByte;
        private   static   SecretKey   deskey;//密钥
        private   static   String   keyString   =   "A3F2569DESJEIWBCJOTY45DYQWF68H1Y ";//获得密钥的参数
          //对base64编码的string解码成byte数组
          public   byte[]   deBase64(String   parm)   throws   IOException   {
                BASE64Decoder   dec=new   BASE64Decoder();
                byte[]   dnParm   =   dec.decodeBuffer(parm);
                System.out.println(dnParm.length);
                System.out.println(dnParm);
                return   dnParm;
          }
        //把密钥参数转为byte数组
          public   byte[]   dBase64(String   parm)   throws   IOException   {
                BASE64Decoder   dec=new   BASE64Decoder();
                byte[]   dnParm   =   dec.decodeBuffer(parm);
                return   dnParm;
          }
        /**
          *   对   Byte   数组进行解密
          *   @param   buff   要解密的数据
          *   @return   返回加密后的   String
          */
          public   static   String   createDecryptor(byte[]   buff)   throws
            NoSuchPaddingException,   NoSuchAlgorithmException,
            UnsupportedEncodingException   {
                try   {
                      c.init(Cipher.DECRYPT_MODE,   deskey);//初始化密码器,用密钥deskey,进入解密模式
                      cipherByte   =   c.doFinal(buff);
                }
                catch(java.security.InvalidKeyException   ex){
                        ex.printStackTrace();
                }
                catch(javax.crypto.BadPaddingException   ex){
                        ex.printStackTrace();
                }
                catch(javax.crypto.IllegalBlockSizeException   ex){
                        ex.printStackTrace();
                }
                return   (new   String(cipherByte, "UTF-8 "));


          }
          public   void   getKey(String   key)   throws   IOException,   InvalidKeyException,
            InvalidKeySpecException   {
            byte[]   dKey   =   dBase64(key);
                try   {
                    deskey=new   javax.crypto.spec.SecretKeySpec(dKey,Algorithm);
                    c   =   Cipher.getInstance(Algorithm);
                }
                catch   (NoSuchPaddingException   ex)   {
                }
                catch   (NoSuchAlgorithmException   ex)   {
                }
          }
          public   static   void   main(String   args[])   throws   IOException,
            NoSuchAlgorithmException,   NoSuchPaddingException,   InvalidKeySpecException,
            InvalidKeyException,   IOException   {
              DES   des   =   new   DES();
              des.getKey(keyString);
              byte[]   dBy   =   des.deBase64( "1ZVasdJJco1qccDnnfQfb8QeaARxhkR6 ");
              String   dStr   =   des.createDecryptor(dBy);
              System.out.println( "解: "+dStr);
          }
}

[解决办法]
学习学习
[解决办法]
不错!

热点排行