如果google都搜索不到,那也就沒有甚麼辦法了

解决方案 »

  1.   

    JDK里面有个JCE,里面就支持3DES,下面是我头两天写的测试JCE的一个类,你可以参考一下,最好不要直接使用,因为这个类是急急忙忙写出来的,结构没有经过调整。后面的是个测试类,展示了如何使用这个类。import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.security.*;
    import java.security.spec.*;public class DESede {
      public static final int KEY_LEN = 24;  private Cipher cipher = null;  public DESede(String rawKey) throws InvalidKeyException,
          NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        StringBuffer sb = new StringBuffer();
        sb.append(rawKey);
        int rawLen = rawKey.length();
        for (int i = 0; i < KEY_LEN - rawLen; i++) {
          sb.append("\0");
        }    byte[] rawkey = sb.toString().getBytes();    DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        Key key = keyfactory.generateSecret(keyspec);    cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);
      }  public String encrypt(String clearText) throws IllegalStateException,
          IllegalBlockSizeException,
          BadPaddingException {
        return byteArray2HexStr(encrypt(clearText.getBytes()));
      }  public byte[] encrypt(byte[] clearText) throws IllegalStateException,
          IllegalBlockSizeException,
          BadPaddingException {
        return cipher.doFinal(clearText);
      }  private static final char[] hexChars = {
          '0', '1', '2', '3', '4', '5', '6', '7', '8',
          '9', 'a', 'b', 'c', 'd', 'e', 'f'};  static String byteArray2HexStr(byte[] ba) {
        StringBuffer sb = new StringBuffer();    for (int i = 0; i < ba.length; i++) {
          int high = ( (ba[i] & 0xf0) >> 4);
          int low = (ba[i] & 0x0f);      sb.append(hexChars[high]);
          sb.append(hexChars[low]);
        }    return sb.toString();
      }
    }public class TestDESede {
      public static void main(String[] args) throws Exception {
        try {
          DESede desede = new DESede("1111");
          System.out.println(desede.encrypt("1111"));
        } catch (Exception e) {
          e.printStackTrace();
        }
      

  2.   

    //--------------------得到DES加密密钥--------------------------------------------------
      public Key GetSecretKey() throws Exception {
        Key mykey = null;
        try {
          FileInputStream inFile =
              new FileInputStream(FileName);
          ObjectInputStream inObject =
              new ObjectInputStream(inFile);
          Object object = inObject.readObject();
          mykey = (SecretKey) object;
        }
        catch (Exception e) {
        }
        return mykey;
      }//--------------------DES加密--------------------------------------------------
      /**
       *<b>Function:</b><br>
       *  -DES加密-.<br>
       *@param StrTxt 参数说明<br>
       *  StrTxt<br>
       *    Type-- String<br>
       *        Comment:明文<br>
       *@return
       *  Type-- String<br>
       *    返回加密后的密文.
       */  public String encode(String StrTxt) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, GetSecretKey());
        System.out.println("encode(String StrTxt)输入的明文是:" + StrTxt);
        byte[] plaintext = StrTxt.getBytes("UTF8");
    //    System.out.println("StrTxt.getBytes(UTF8)=" + byte2hex(plaintext) + "\n");
        String output1 = new String(plaintext);
        System.out.println("\n\noutput1 text:" + output1);
        //注意把字符串转换成字节需要指定编码方式,一般用此“UTF8”。
        byte[] ciphertext = cipher.doFinal(plaintext); //密文。//解密过程。很简单阿。//    cipher.init(Cipher.DECRYPT_MODE, GetSecretKey());
    //    byte[] decryptedText = cipher.doFinal(ciphertext);
    //    System.out.println("ciphertext=" + byte2hex(ciphertext) + "\n");
        System.out.println("ciphertext.length=" + ciphertext.length);
        String output = ""; //new String(ciphertext);
        char[] cTmp;
        cTmp = new char[ciphertext.length];
        for (int i = 0; i < ciphertext.length; i++) {
          cTmp[i] = (char) (ciphertext[i]);
          output = output + cTmp[i];
        }
        return output;  }
    //--------------------DES解密--------------------------------------------------
      /**
       *<b>Function:</b><br>
       *  -DES解密-.<br>
       *@param StrTxt 参数说明<br>
       *  StrTxt<br>
       *    Type-- String<br>
       *        Comment:密文<br>
       *@return
       *  Type-- String<br>
       *    返回解密后的明文.
       * @exception AllException
       */  public String decode(String StrTxt) throws Exception {
        cipher.init(Cipher.DECRYPT_MODE, mykey);
        String output = "";
    //    System.out.println("decode(String StrTxt)输入的密文为=" + StrTxt);
        char[] cTmp = StrTxt.toCharArray();
        byte[] plaintext1;
        plaintext1 = new byte[cTmp.length];
        for (int i = 0; i < cTmp.length; i++) {
          plaintext1[i] = (byte) (cTmp[i]);
        }
        //此处指定编码方式了哈哈。肯定行的。
        byte[] decryptedText = cipher.doFinal(plaintext1);
    //    System.out.println("decode(String StrTxt)1=" + byte2hex(decryptedText));
        output = new String(decryptedText, "UTF8");
    //    System.out.println("decode(String StrTxt)输出的明文为=" + output);
        return output;
      }
    给你我写的方法,密钥生产的方法不能给你,呵呵,不好意思。
    记得给分
      

  3.   

    对于我的程序补充两句,我的JDK版本是1.4.1,我的程序的密钥和明文是相同的,所以只要参考用法就可以了,更具体的用法可以参考Java的文档。
      

  4.   

    Up多一次 谢谢帮忙
    再放一两天吧,或者会有更多的收获的因为昨天考虑了一下RSA加密和DSA加密
    虽然安全性和3DES差不多,但是不适合在网络上用,因为耗费cpu资源太多,加密解密速度慢所以还是决定用3DES算法
      

  5.   

    看看这个地方 
    有很多加密算法的资料http://www.comms.engg.susx.ac.uk/fft/