public String encryptString(String myinfo) { 
//添加新安全算法,如果用JCE就要把它添加进去 
Security.addProvider(new com.sun.crypto.provider.SunJCE()); 
String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish 
String ss="";
try { 
//生成密钥 
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); 
SecretKey deskey = keygen.generateKey(); 

//加密 
// System.out.println("加密前的二进串:"+byte2hex(myinfo.getBytes())); 
// System.out.println("加密前的信息:"+myinfo); 
Cipher c1 = Cipher.getInstance(Algorithm); 
c1.init(Cipher.ENCRYPT_MODE,deskey); 

byte[] cipherByte=c1.doFinal(myinfo.getBytes()); 
ss=byte2hex(cipherByte);
// System.out.println("加密后的二进串:"+ss); 
//解密 
c1 = Cipher.getInstance(Algorithm); 
c1.init(Cipher.DECRYPT_MODE,deskey); 
byte[] clearByte=c1.doFinal(cipherByte); 
// System.out.println("解密后的二进串:"+byte2hex(clearByte)); 
// System.out.println("解密后的信息:"+(new String(clearByte))); 


catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();} 
catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();} 
catch (java.lang.Exception e3) {e3.printStackTrace();} 
return ss;

解决方案 »

  1.   

    连通给的key是经过base64加密过的,需要解密,
       private static byte[] TrippleEncrypt(byte[] data,String key )
       {
         try{
           System.out.println("加密前的二进串:"+byte2hex(data));
           System.out.println("加密前的信息:"+new String(data));       byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);//这个地方重要
            DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
            Key enkey = keyfactory.generateSecret(keyspec);
            Cipher c1 = Cipher.getInstance(algorithm);
            //Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            c1.init(Cipher.ENCRYPT_MODE,enkey);
            byte[] cipherByte=c1.doFinal(data);
            return cipherByte;
          }
          catch(Exception e)
          {
            return null;
          }
       }
      

  2.   


    import java.io.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import javax.crypto.spec.DESedeKeySpec;
    import java.security.spec.KeySpec;
    import java.security.spec.AlgorithmParameterSpec;
    import java.security.*;public class DesEdeEncrypter
    {
       private static String Des = "Des";
       private static String Desede = "DESede";
       private static String DesBlowfish = "Blowfish";
       private static String algorithm =Desede;
       public static String byte2hex(byte[] b) //二行制转字符串
          {
           String hs="";
           String stmp="";
           for (int n=0;n<b.length;n++)
            {
             stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
             if (stmp.length()==1) hs=hs+"0"+stmp;
             else hs=hs+stmp;
             if (n<b.length-1)  hs=hs+"";
            }
           return hs.toUpperCase();
          }   private static byte[] TrippleEncrypt(byte[] data,String key )
       {
         try{
           System.out.println("加密前的二进串:"+byte2hex(data));
           System.out.println("加密前的信息:"+new String(data));       //byte[] rawkey = key.getBytes();
           byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);
           DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
           SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
           Key enkey = keyfactory.generateSecret(keyspec);
           Cipher c1 = Cipher.getInstance(algorithm);
           c1.init(Cipher.ENCRYPT_MODE,enkey);
           byte[] cipherByte=c1.doFinal(data);
           System.out.println("加密后的二进串:"+byte2hex(cipherByte));
           return cipherByte;
          }
          catch(Exception e)
          {
            return null;
          }
       }   private static byte[] TrippleDecrypt(byte[] data,String key )
       {     try{
           System.out.println("解密前的二进串:"+byte2hex(data));       //byte[] rawkey = key.getBytes();
           byte[] rawkey = new sun.misc.BASE64Decoder().decodeBuffer(key);
           DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
           SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(algorithm);
           Key enkey = keyfactory.generateSecret(keyspec);
           Cipher c1 = Cipher.getInstance(algorithm);
           c1.init(Cipher.DECRYPT_MODE,enkey);
           byte[] cipherByte=c1.doFinal(data);
           System.out.println("解密后的二进串:"+byte2hex(cipherByte));
           System.out.println("解密后的信息:"+(new String(cipherByte)));
           return  cipherByte;
         }catch(Exception e){
           return null;
         }
       }
      public static void main(String[] args) {
        String info = "9000$13013890874$100201$2004-04-13 10:40:22";
        byte[] x = TrippleEncrypt(info.getBytes(),"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
        //byte[] x = DESEncrypt(info.getBytes(),"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
        System.out.print(new sun.misc.BASE64Encoder().encode(x));
        byte[] y = TrippleDecrypt(x,"cf7FjFMnafiiK40vnuTf9vsWnXpBo6qR");
      }
    }