DES加密服务器端总是返回签名无效,求助

xpbtmusic 发布于 2012/04/12 16:59
阅读 568
收藏 0

服务器那端用的是perl解密的。总是返回签名无效,向高手求助。

package perl.des;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class PerlDES {

 Cipher ecipher;
 Cipher dcipher;
 byte[] buf = new byte[1024];

 public PerlDES() {
  String strKey = "12345678";
  byte[] keyBytes = null;
  try {
   keyBytes = strKey.getBytes("UTF-8");
  } catch (java.io.UnsupportedEncodingException ex) {
   ex.printStackTrace();
  }
  byte[] iv = "01234567".getBytes();
  IvParameterSpec ivSpec = new IvParameterSpec(iv);
  try {
   ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
   dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  }
  try {
   DESKeySpec dks = new DESKeySpec(keyBytes);
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   SecretKey securekey = keyFactory.generateSecret(dks);
   ecipher.init(Cipher.ENCRYPT_MODE, securekey, ivSpec);
   dcipher.init(Cipher.DECRYPT_MODE, securekey, ivSpec);
  } catch (InvalidKeyException e1) {
   e1.printStackTrace();
  } catch (InvalidAlgorithmParameterException e1) {
   e1.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (InvalidKeySpecException e) {
   e.printStackTrace();
  }
 }

 public void encrypt(InputStream in, OutputStream out) {
  try {
   out = new CipherOutputStream(out, ecipher);
   int numRead = 0;
   while ((numRead = in.read(buf)) >= 0) {
    out.write(buf, 0, numRead);
   }
   out.close();
  } catch (java.io.IOException e) {
   e.printStackTrace();
  }
 }

 public byte[] encrptyStr(byte[] bIn) {
  try {
   return ecipher.doFinal(bIn);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return new byte[] {};
 }

 public void decrypt(InputStream in, OutputStream out) {
  try {
   out = new CipherOutputStream(out, dcipher);
   int numRead = 0;
   while ((numRead = in.read(buf)) >= 0) {
    out.write(buf, 0, numRead);
   }
   out.close();
  } catch (java.io.IOException e) {
   e.printStackTrace();
  }
 }

 public byte[] decrptyStr(byte[] bIn) {
  try {
   return dcipher.doFinal(bIn);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return new byte[] {};
 }

 public static String byte2hex(byte[] b) {
  String hs = "";
  String stmp = "";
  for (int n = 0; n < b.length; n++) {
   stmp = (Integer.toHexString(b[n] & 0XFF));
   System.out.println(n + ":" + stmp + "==" + "byte2hex");
   if (stmp.length() == 1)
    hs = hs + "0" + stmp;
   else
    hs = hs + stmp;
  }
  return hs;
 }

 public static byte[] hex2byte(byte[] b) {
  if ((b.length % 2) != 0)
   throw new IllegalArgumentException("Length is not even");
  byte[] b2 = new byte[b.length / 2];
  for (int n = 0; n < b.length; n += 2) {
   String item = new String(b, n, 2);
   System.out.println(item + "00000000");
   b2[n / 2] = (byte) Integer.parseInt(item, 16);
  }
  return b2;
 }

 public static String byte2String(byte[] b) {
  String hs = "";
  String stmp = "";
  for (int n = 0; n < b.length; n++) {
   stmp = (Integer.toHexString(b[n] & 0XFF));
   System.out.println(Integer.parseInt(stmp, 16) + "====");
   hs += Character.valueOf((char) Integer.parseInt(stmp, 16))
     .toString();
  }
  return hs;
 }

 public static void main(String args[]) throws Exception {
  // if (args.length == 0) {
  // System.err.println("Need a string!");
  // System.exit(1);
  // }
  String ss = "MQ==";
  String sss = "d1407e2836e3451f";
  PerlDES encrypter = new PerlDES();
  byte[] in = hex2byte(sss.getBytes("gb2312"));
  byte[] out = encrypter.decrptyStr(in);
  System.out.println(new String(out));
  // //d1407e2836e3451f

  byte[] inin = ss.getBytes("gb2312");
  for (int jj = 0; jj < inin.length; jj++) {
   System.out.println(jj + ":" + inin[jj] + "before");
   if (jj == inin.length - 1) {
    System.out.println("\n");
   }
  }
  byte[] outout = encrypter.encrptyStr(inin);
  for (int kk = 0; kk < outout.length; kk++) {
   System.out.println(kk + ":" + outout[kk] + "after");
  }
  System.out.println(new String(outout));
  String str = byte2hex(outout);
  System.out.println(str);
 }
}
加载中
返回顶部
顶部