看看这个吧,希望能对你有所帮助。import java.security.*;import javax.crypto.*;
import javax.crypto.spec.*;import java.util.*;
import java.io.*;
public class DES {
/**
   * DES 加密算法
   * @param data 处理数据
   * @param key  密钥
   * @param mode =0加密  =1解密
   */
  public static String des(String data,String key,int mode)
  {
   if(mode == 0 )
   {
   mode = Cipher.ENCRYPT_MODE;
   }else{
   mode = Cipher.DECRYPT_MODE;
   }
   try{
   Cipher cipher = Cipher.getInstance("DES");
       SecretKeySpec spec = new SecretKeySpec(key.getBytes(),"DES");
       Key deskey = spec;
      cipher.init(mode, deskey);  
       return crypt(new ByteArrayInputStream(data.getBytes()),cipher);
   }catch(Exception e)
{
   e.printStackTrace();
   return "";
}
  
  
  }
  
  private static String crypt(InputStream in,Cipher cipher)throws IOException, GeneralSecurityException
  {
     int blockSize = cipher.getBlockSize();
     int outputSize = cipher.getOutputSize(blockSize);
     byte[] inBytes = new byte[blockSize];
     byte[] outBytes = new byte[outputSize];
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     int inLength = 0;
     boolean more = true;
     while (more)
     {
        inLength = in.read(inBytes);
        if (inLength == blockSize)
        {
           int outLength 
              = cipher.update(inBytes, 0, blockSize, outBytes);
           out.write(outBytes, 0, outLength);
           System.out.println(outLength);
        }
        else more = false;         
     }
     if (inLength > 0)
        outBytes = cipher.doFinal(inBytes, 0, inLength);
     else
        outBytes = cipher.doFinal();
     System.out.println(outBytes.length);
     out.write(outBytes);
     return new String(out.toByteArray());
  }
public static void main(String[] args) {
         DES des = new DES();
         System.out.println(des.des("123","wer",1));
}
}

解决方案 »

  1.   

    楼上的兄弟,你提供的有错啊,
        DES des = new DES();
        String testStr = des.des("中国", "12345678", 0);
        String returnStr = des.des(testStr, "12345678", 1);
        System.out.println(returnStr);
    测试出错。
      

  2.   

    对不起楼主啊,我忘记了我还没调试出来
    楼主看下这个贴子吧 
    http://community.csdn.net/Expert/topic/3925/3925725.xml?temp=7.879275E-02
      

  3.   

    http://www.javaworld.com.tw/jute/post/view?bid=5&id=28789&sty=1&tpg=11&age=0这里也有,希望楼主可以早些解决