JAVA上加密算法的实现用例
http://www-900.ibm.com/developerWorks/java/l-security/index.shtml

解决方案 »

  1.   

    package encryption;/**
     * Title:
     * Description:
     * Copyright:    Copyright (c) 2001
     * Company:
     * @author
     * @version 1.0
     */
    import java.io.*;
    import java.math.BigInteger;
    import java.security.*;
    import java.security.spec.*;
    import java.security.interfaces.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import javax.crypto.interfaces.*;public class En1 {    public En1() {        KeyGenerator keygen;
            SecretKey desKey;
            Cipher desCipher;        try{
                //DES,DESede,PBEWithMD5AndDES,Blowfish
                //Generating a Key
    //            keygen = KeyGenerator.getInstance("DES");
    //            keygen = KeyGenerator.getInstance("DESede");
                keygen = KeyGenerator.getInstance("Blowfish");            desKey = keygen.generateKey();
                System.out.println(
                    desKey.getEncoded().toString()
                );            // Create the cipher
    //            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    //            desCipher = Cipher.getInstance("DESede");
                desCipher = Cipher.getInstance("Blowfish");
                // Our cleartext
                byte[] cleartext = "This is just an example:邢战军".getBytes();
                StringBuffer StrBuf = new StringBuffer();
                for(int i=0,j=cleartext.length;i<j;i++)
                {
                    StrBuf.append(String.valueOf((char)cleartext[i]));
                }
                System.out.println(" ");
                System.out.println(StrBuf.toString());            //Initialize the cipher for encryption
                desCipher.init(Cipher.ENCRYPT_MODE,desKey);            // Encrypt the cleartext
                StrBuf = new StringBuffer();
                byte[] ciphertext = desCipher.doFinal(cleartext);
    //            System.out.println(ciphertext.toString());
                for(int i=0,j=ciphertext.length;i<j;i++)
                {
                    StrBuf.append(String.valueOf((char)ciphertext[i]));
                }
                System.out.println(" ");
                System.out.println("Encrypt the cleartext");
                System.out.println(StrBuf.toString());            SecretKeySpec seckey = new javax.crypto.spec.SecretKeySpec(
                    desKey.getEncoded(),"Blowfish");
                System.out.println(
                    seckey.getEncoded()
                );            seckey = new javax.crypto.spec.SecretKeySpec(
                    seckey.getEncoded(),"Blowfish");
                System.out.println(
                    seckey.getEncoded()
                );            // Initialize the same cipher for decryption
    //            desCipher.init(Cipher.DECRYPT_MODE, desKey);
                desCipher.init(Cipher.DECRYPT_MODE, seckey);            // Decrypt the ciphertext
                StrBuf = new StringBuffer();
                byte[] cleartext1 = desCipher.doFinal(ciphertext);
    //            System.out.println(cleartext1.toString());
                for(int i=0,j=cleartext1.length;i<j;i++)
                {
                    StrBuf.append(String.valueOf((char)cleartext1[i]));
                }
                System.out.println(" ");
                System.out.println("Decrypt the ciphertext");
                System.out.println(StrBuf.toString());
            }
            catch(java.security.InvalidKeyException e2)
            {
                e2.printStackTrace();
            }
            catch(java.security.NoSuchAlgorithmException e)
            {
                e.printStackTrace();
            }
            catch(javax.crypto.NoSuchPaddingException e1)
            {
                e1.printStackTrace();
            }
            catch(javax.crypto.BadPaddingException e3)
            {
                e3.printStackTrace();
            }
            catch(javax.crypto.IllegalBlockSizeException e4)
            {
                e4.printStackTrace();
            }    }    public static void main(String[] args) {
            En1 en11 = new En1();
        }
    }
      

  2.   

    import javax.crypto.*;
    在哪个JAR里?