All pastes #1930002 Raw Edit

Javax Crypto Example |

public java v1 · immutable
#1930002 ·published 2010-08-31 16:41 UTC
rendered paste body
/************************************************************************ *                                                                      * * javax.crypto                                                         * *                                                                      * *    Interfaces:                                                       * *       SecretKey                                                      * *                                                                      * *    Classes:                                                          * *       Cipher                              KeyGeneratorSpi            * *       CipherInputStream                   Mac                        * *       CipherOutputStream                  MacSpi                     * *       CipherSpi                           NullCipher                 * *       KeyAgreement                        SealedObject               * *       KeyAgreementSpi                     SecretKeyFactory           * *       KeyGenerator                        SecretKeyFactorySpi        * *                                                                      * *    Exceptions:                                                       * *       BadPaddingException                 NoSuchPaddingException     * *       IllegalBlockSizeException           ShortBufferException       * *                                                                      * ************************************************************************/package Test.Chris;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyAgreement;import javax.crypto.KeyGenerator;import javax.crypto.Mac;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;public class Javax_crypto {    public static void main(String[] args) {        Javax_crypto obj = new Javax_crypto();        obj.exercise();        System.exit(0);    }    public void exercise() {        cipher();        cipherinputstream();        cipheroutputstream();        cipherspi();        keyagreement();        keyagreementspi();        keygenerator();        keygeneratorspi();        mac();        macspi();        nullcipher();        sealedobject();        secretkeyfactory();        secretkeyfactoryspi();        secretkey();    }    /*********************************************************************     * * Cipher: * * Desc: * * Fields: * DECRYPT_MODE ENCRYPT_MODE * * Methods: * doFinal getIV init     * * getAlgorithm getOutputSize update * getBlockSize getParameters * getInstance getProvider *     * * Algorithm: * DES: Digital Encryption Standard * DESede: Triple DES Encryption *     * PBEWithMD5AndDES: password-based encryption (CBC/PKCS5) * Blowfish: The block cipher designed     * by Bruce Schneier * * Mode: * ECB: Electronic Codebook Mode * CBC: Cipher Block Chaining Mode     * * PCBC: Plaintext Cipher Block Chaining * CFB: Cipher Feedback Mode * OFB: Output Feedback     * Mode * * Padding: * NoPadding: no padding * PKCS5Padding: padding scheme described by RSA     * Labs * SSL3Padding: padding scheme defined in SSL Protocol (n/a) * *     *********************************************************************/    void cipher() {        byte[] salt =                { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8,                        (byte) 0xee, (byte) 0x99 };        char[] passPhrase = "This is an example password phrase".toCharArray();        int i;        String s;        byte[] bx;        byte[] dx;        java.security.Key k;        java.security.Provider p;        java.security.AlgorithmParameters ap;        javax.crypto.spec.PBEParameterSpec ps;        javax.crypto.spec.IvParameterSpec iv;        SecretKeyFactory kf;        Cipher c;        try {            ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);            iv = new javax.crypto.spec.IvParameterSpec(salt);            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES"); // generates a Cipher object            c = Cipher.getInstance("DES", "SunJCE");            c.init(Cipher.ENCRYPT_MODE, k); // initializes this cipher with a key            s = c.getAlgorithm(); // algorithm name            p = c.getProvider(); // provider            i = c.getBlockSize(); // block size (in bytes)            i = c.getOutputSize(7); // length needed to hold result of the next update            dx = c.getIV(); // initialization vector            ap = c.getParameters(); // parameters used with this cipher            bx = c.doFinal("goodbye".getBytes()); // finishes encryption or decryption operation            s = Base64.encode(bx);            System.out.println(s);            // bx = Base64.decode(s);            // DES/ECB            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES/ECB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DES/ECB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k);            dx = c.doFinal(bx);            // DES/CBC            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES/CBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DES/CBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DES/PCBC            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES/PCBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DES/PCBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DES/CFB            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES/CFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DES/CFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DES/OFB            k = KeyGenerator.getInstance("DES").generateKey();            c = Cipher.getInstance("DES/OFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DES/OFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DESede/ECB            k = KeyGenerator.getInstance("DESede").generateKey();            c = Cipher.getInstance("DESede/ECB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DESede/ECB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k);            dx = c.doFinal(bx);            // DESede/CBC            k = KeyGenerator.getInstance("DESede").generateKey();            c = Cipher.getInstance("DESede/CBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DESede/CBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DESede/PCBC            k = KeyGenerator.getInstance("DESede").generateKey();            c = Cipher.getInstance("DESede/PCBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DESede/PCBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DESede/CFB            k = KeyGenerator.getInstance("DESede").generateKey();            c = Cipher.getInstance("DESede/CFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DESede/CFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // DESede/OFB            k = KeyGenerator.getInstance("DESede").generateKey();            c = Cipher.getInstance("DESede/OFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("DESede/OFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // Blowfish/ECB            k = KeyGenerator.getInstance("Blowfish").generateKey();            c = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k);            dx = c.doFinal(bx);            // Blowfish/CBC            k = KeyGenerator.getInstance("Blowfish").generateKey();            c = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // Blowfish/PCBC            k = KeyGenerator.getInstance("Blowfish").generateKey();            c = Cipher.getInstance("Blowfish/PCBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("Blowfish/PCBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // Blowfish/CFB            k = KeyGenerator.getInstance("Blowfish").generateKey();            c = Cipher.getInstance("Blowfish/CFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("Blowfish/CFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // Blowfish/OFB            k = KeyGenerator.getInstance("Blowfish").generateKey();            c = Cipher.getInstance("Blowfish/OFB/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, iv);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("Blowfish/OFB/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, iv);            dx = c.doFinal(bx);            // PBEWithMD5AndDES/CBC/PKCS5Padding            kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");            k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(passPhrase));            c = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");            c.init(Cipher.ENCRYPT_MODE, k, ps);            bx = c.doFinal("goodbye".getBytes());            c = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");            c.init(Cipher.DECRYPT_MODE, k, ps);            dx = c.doFinal(bx);        } catch (NoSuchPaddingException e) {            System.out.println(e);        } catch (IllegalBlockSizeException e) {            System.out.println(e);        } catch (BadPaddingException e) {            System.out.println(e);        } catch (java.security.spec.InvalidKeySpecException e) {            System.out.println(e);        } catch (java.security.InvalidAlgorithmParameterException e) {            System.out.println(e);        } catch (java.security.InvalidKeyException e) {            System.out.println(e);        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        } catch (java.security.NoSuchProviderException e) {            System.out.println(e);        }        /*         * TO BE DETERMINED void init(int opmode, java.security.Key key,         * java.security.AlgorithmParameters params) // Initializes this cipher with a key and a set         * of algorithm parameters. void init(int opmode, java.security.Key key,         * java.security.spec.AlgorithmParameterSpec params) // Initializes this cipher with a key         * and a set of algorithm parameters. void init(int opmode, java.security.Key key,         * java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random) //         * Initializes this cipher with a key, a set of algorithm parameters, and a source of         * randomness. void init(int opmode, java.security.Key key,         * java.security.AlgorithmParameters params, java.security.SecureRandom random) //         * Initializes this cipher with a key, a set of algorithm parameters, and a source of         * randomness. void init(int opmode, java.security.Key key, java.security.SecureRandom         * random) // Initializes this cipher with a key and a source of randomness. byte[]         * update(byte[] input) // Continues a multiple-part encryption or decryption operation         * (depending on how this cipher was initialized), processing another data part. byte[]         * update(byte[] input, int inputOffset, int inputLen) // Continues a multiple-part         * encryption or decryption operation (depending on how this cipher was initialized),         * processing another data part. int update(byte[] input, int inputOffset, int inputLen,         * byte[] output) // Continues a multiple-part encryption or decryption operation (depending         * on how this cipher was initialized), processing another data part. int update(byte[]         * input, int inputOffset, int inputLen, byte[] output, int outputOffset) // Continues a         * multiple-part encryption or decryption operation (depending on how this cipher was         * initialized), processing another data part. byte[] doFinal(byte[] input) // Encrypts or         * decrypts data in a single-part operation, or finishes a multiple-part operation. int         * doFinal(byte[] output, int outputOffset) // Finishes a multiple-part encryption or         * decryption operation, depending on how this cipher was initialized. byte[] doFinal(byte[]         * input, int inputOffset, int inputLen) // Encrypts or decrypts data in a single-part         * operation, or finishes a multiple-part operation. int doFinal(byte[] input, int         * inputOffset, int inputLen, byte[] output) // Encrypts or decrypts data in a single-part         * operation, or finishes a multiple-part operation. int doFinal(byte[] input, int         * inputOffset, int inputLen, byte[] output, int outputOffset) // Encrypts or decrypts data         * in a single-part operation, or finishes a multiple-part operation.         */    }    /*********************************************************************     * * CipherInputStream: * * Desc: * * Methods: * available markSupported skip * close read * *     *********************************************************************/    void cipherinputstream() {        /*         * TO BE DETERMINED int available() // Returns the number of bytes that can be read from         * this input stream without blocking. void close() // Closes this input stream and releases         * any system resources associated with the stream. boolean markSupported() // Tests if this         * input stream supports the mark and reset methods, which it does not. int read() // Reads         * the next byte of data from this input stream. int read(byte[] b) // Reads up to b.length         * bytes of data from this input stream into an array of bytes. int read(byte[] b, int off,         * int len) // Reads up to len bytes of data from this input stream into an array of bytes.         * long skip(long n) // Skips n bytes of input from the bytes that can be read from this         * input stream without blocking.         */    }    /*********************************************************************     * * CipherOutputStream: * * Desc: * * Methods: * close flush write * *     *********************************************************************/    void cipheroutputstream() {        /*         * TO BE DETERMINED void close() // Closes this output stream and releases any system         * resources associated with this stream. void flush() // Flushes this output stream by         * forcing any buffered output bytes that have already been processed by the encapsulated         * cipher object to be written out. void write(byte[] b) // Writes b.length bytes from the         * specified byte array to this output stream. void write(byte[] b, int off, int len) //         * Writes len bytes from the specified byte array starting at offset off to this output         * stream. void write(int b) // Writes the specified byte to this output stream.         */    }    /*********************************************************************     * * CipherSpi: * * Desc: * * Methods: * engineDoFinal engineGetOutputSize engineSetMode *     * engineGetBlockSize engineGetParameters engineSetPadding * engineGetIV engineInit engineUpdate     * * *     *********************************************************************/    void cipherspi() {        /*         * TO BE DETERMINED abstract byte[] engineDoFinal abstract int engineDoFinal abstract int         * engineGetBlockSize abstract byte[] engineGetIV abstract int engineGetOutputSize abstract         * java.security.AlgorithmParameters engineGetParameters abstract void engineInit abstract         * void engineInit abstract void engineInit abstract void engineSetMode abstract void         * engineSetPadding abstract byte[] engineUpdate abstract int engineUpdate         */    }    /*********************************************************************     * * KeyAgreement: * * Desc: * * Methods: * doPhase getAlgorithm getProvider * generateSecret     * getInstance init * *     *********************************************************************/    void keyagreement() {        String s;        java.security.Provider p;        KeyAgreement ka;        try {            ka = KeyAgreement.getInstance("DH"); // Generates a KeyAgreement object            ka = KeyAgreement.getInstance("DH", "SunJCE");            p = ka.getProvider(); // provider of this KeyAgreement            System.out.println(p);            s = ka.getAlgorithm(); // algorithm name            System.out.println(s);        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        } catch (java.security.NoSuchProviderException e) {            System.out.println(e);        }        /*         * TO BE DETERMINED java.security.Key doPhase(java.security.Key key, boolean lastPhase) //         * Executes the next phase of this key agreement with the given key that was received from         * one of the other parties involved in this key agreement. byte[] generateSecret() //         * Generates the shared secret and returns it in a new buffer. int generateSecret(byte[]         * sharedSecret, int offset) // Generates the shared secret, and places it into the buffer         * sharedSecret, beginning at offset inclusive. SecretKey generateSecret(java.lang.String         * algorithm) // Creates the shared secret and returns it as a SecretKey object of the         * specified algorithm. void init(java.security.Key key) // Initializes this key agreement         * with the given key, which is required to contain all the algorithm parameters required         * for this key agreement. void init(java.security.Key key, java.security.SecureRandom         * random) // Initializes this key agreement with the given key and source of randomness.         * void init(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) //         * Initializes this key agreement with the given key and set of algorithm parameters. void         * init(java.security.Key key, java.security.spec.AlgorithmParameterSpec params,         * java.security.SecureRandom random) // Initializes this key agreement with the given key,         * set of algorithm parameters, and source of randomness.         */    }    /*********************************************************************     * * KeyAgreementSpi: * * Desc: * * Methods: * engineDoPhase engineGenerateSecret engineInit * *     *********************************************************************/    void keyagreementspi() {        /*         * TO BE DETERMINED protected abstract java.security.Key engineDoPhase(java.security.Key         * key, boolean lastPhase) // Executes the next phase of this key agreement with the given         * key that was received from one of the other parties involved in this key agreement.         * protected abstract byte[] engineGenerateSecret() // Generates the shared secret and         * returns it in a new buffer. protected abstract int engineGenerateSecret(byte[]         * sharedSecret, int offset) // Generates the shared secret, and places it into the buffer         * sharedSecret, beginning at offset inclusive. protected abstract SecretKey         * engineGenerateSecret(java.lang.String algorithm) // Creates the shared secret and returns         * it as a secret key object of the requested algorithm type. protected abstract void         * engineInit(java.security.Key key, java.security.spec.AlgorithmParameterSpec params,         * java.security.SecureRandom random) // Initializes this key agreement with the given key,         * set of algorithm parameters, and source of randomness. protected abstract void         * engineInit(java.security.Key key, java.security.SecureRandom random) // Initializes this         * key agreement with the given key and source of randomness.         */    }    /*********************************************************************     * * KeyGenerator: * * Desc: * * Methods: * generateKey getInstance init * getAlgorithm     * getProvider * *     *********************************************************************/    void keygenerator() {        String s;        java.security.SecureRandom r;        java.security.Provider p;        SecretKey k;        KeyGenerator kg;        try {            r = java.security.SecureRandom.getInstance("SHA1PRNG");            kg = KeyGenerator.getInstance("DES"); // generates a KeyGenerator object (DES|DESede)            kg = KeyGenerator.getInstance("DES", "SunJCE"); // specify provider            kg.init(56); // initialize (DES=56 DESede=112|168)            kg.init(r);            kg.init(56, r);            s = kg.getAlgorithm(); // algorithm name            p = kg.getProvider(); // provider            k = kg.generateKey(); // Generates a secret key        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        } catch (java.security.NoSuchProviderException e) {            System.out.println(e);        }        /*         * TO BE DETERMINED void init(java.security.spec.AlgorithmParameterSpec params) //         * Initializes this key generator with the specified parameter set. void         * init(java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)         * // Initializes this key generator with the specified parameter set and a user-provided         * source of randomness.         */    }    /*********************************************************************     * * KeyGeneratorSpi: * * Desc: * * Methods: * engineGenerateKey engineInit * *     *********************************************************************/    void keygeneratorspi() {        /*         * TO BE DETERMINED protected abstract SecretKey engineGenerateKey() // Generates a secret         * key. protected abstract void engineInit(java.security.spec.AlgorithmParameterSpec params,         * java.security.SecureRandom random) // Initializes the key generator with the specified         * parameter set and a user-provided source of randomness. protected abstract void         * engineInit(int keysize, java.security.SecureRandom random) // Initializes this key         * generator for a certain keysize, using the given source of randomness. protected abstract         * void engineInit(java.security.SecureRandom random) // Initializes the key generator.         */    }    /*********************************************************************     * * Mac: * * Desc: * * Methods: * clone getInstance init * doFinal getMacLength reset *     * getAlgorithm getProvider update * *     *********************************************************************/    void mac() {        byte[] bx = new byte[20];        java.security.SecureRandom r = new java.security.SecureRandom();        SecretKey k;        Mac m;        try {            r = new java.security.SecureRandom();            r.nextBytes(bx);            k = new javax.crypto.spec.SecretKeySpec(bx, "HmacSHA1");            m = Mac.getInstance("HmacSHA1"); // generates an Mac object            m.init(k); // initializes with the given key            m.update("Hello World".getBytes()); // processes the given array of bytes            bx = m.doFinal(); // finishes the MAC operation        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        } catch (java.security.InvalidKeyException e) {            System.out.println(e);        }        /*         * TO BE DETERMINED java.lang.Object clone() // Returns a clone if the provider         * implementation is cloneable. byte[] doFinal(byte[] input) // Processes the given array of         * bytes and finishes the MAC operation. void doFinal(byte[] output, int outOffset) //         * Finishes the MAC operation. java.lang.String getAlgorithm() // Returns the algorithm name         * of this Mac object. static Mac getInstance(java.lang.String algorithm, java.lang.String         * provider) // Generates an Mac object for the specified MAC algorithm from the specified         * provider. int getMacLength() // Returns the length of the MAC in bytes.         * java.security.Provider getProvider() // Returns the provider of this Mac object. void         * init(java.security.Key key, java.security.spec.AlgorithmParameterSpec params) //         * Initializes this Mac object with the given key and algorithm parameters. void reset() //         * Resets this Mac object. void update(byte input) // Processes the given byte. void         * update(byte[] input, int offset, int len) // Processes the first len bytes in input,         * starting at offset inclusive.         */    }    /*********************************************************************     * * MacSpi: * * Desc: * * Methods: * clone engineGetMacLength engineReset * engineDoFinal     * engineInit engineUpdate * *     *********************************************************************/    void macspi() {        /*         * TO BE DETERMINED java.lang.Object clone() // Returns a clone if the implementation is         * cloneable. protected abstract byte[] engineDoFinal() // Completes the MAC computation and         * resets the MAC for further use, maintaining the secret key that the MAC was initialized         * with. protected abstract int engineGetMacLength() // Returns the length of the MAC in         * bytes. protected abstract void engineInit(java.security.Key key,         * java.security.spec.AlgorithmParameterSpec params) // Initializes the MAC with the given         * (secret) key and algorithm parameters. protected abstract void engineReset() // Resets         * the MAC for further use, maintaining the secret key that the MAC was initialized with.         * protected abstract void engineUpdate(byte input) // Processes the given byte. protected         * abstract void engineUpdate(byte[] input, int offset, int len) // Processes the first len         * bytes in input, starting at offset inclusive.         */    }    /*********************************************************************     * * NullCipher: * * Desc: * *     *********************************************************************/    void nullcipher() {        /*         * TO BE DETERMINED         */    }    /*********************************************************************     * * SealedObject: * * Desc: * * Methods: * getAlgorithm getObject * *     *********************************************************************/    void sealedobject() {        /*         * TO BE DETERMINED java.lang.String getAlgorithm() // Returns the algorithm that was used         * to seal this object. java.lang.Object getObject(Cipher c) // Retrieves the original         * (encapsulated) object. java.lang.Object getObject(java.security.Key key) // Retrieves the         * original (encapsulated) object. java.lang.Object getObject(java.security.Key key,         * java.lang.String provider) // Retrieves the original (encapsulated) object.         */    }    /*********************************************************************     * * SecretKeyFactory: * * Desc: * * Methods: * generateSecret getInstance getProvider *     * getAlgorithm getKeySpec translateKey * *     *********************************************************************/    void secretkeyfactory() {        String s;        byte[] bx = new byte[20];        java.security.Provider p;        java.security.spec.KeySpec ks;        SecretKey k;        SecretKeyFactory kf;        try {            ks = new javax.crypto.spec.DESKeySpec(bx, 0);            kf = SecretKeyFactory.getInstance("DES"); // generate SecretKeyFactory object            kf = SecretKeyFactory.getInstance("DES", "SunJCE");            k = kf.generateSecret(ks); // generates SecretKey            s = kf.getAlgorithm(); // algorithm name            p = kf.getProvider(); // provider            k = kf.translateKey(k); // translates a key        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        } catch (java.security.NoSuchProviderException e) {            System.out.println(e);        } catch (java.security.InvalidKeyException e) {            System.out.println(e);        } catch (java.security.spec.InvalidKeySpecException e) {            System.out.println(e);        }        /*         * TO BE DETERMINED ks = kf.getKeySpec(k, java.security.spec.KeySpec.class); // Returns a         * specification (key material) of the given key object in the requested format.         */    }    /*********************************************************************     * * SecretKeyFactorySpi: * * Desc: * * Methods: * engineGenerateSecret engineGetKeySpec     * engineTranslateKey * *     *********************************************************************/    void secretkeyfactoryspi() {        /*         * TO BE DETERMINED protected abstract SecretKey         * engineGenerateSecret(java.security.spec.KeySpec keySpec) // Generates a SecretKey object         * from the provided key specification (key material). protected abstract         * java.security.spec.KeySpec engineGetKeySpec(SecretKey key, java.lang.Class keySpec) //         * Returns a specification (key material) of the given key object in the requested format.         * protected abstract SecretKey engineTranslateKey(SecretKey key) // Translates a key         * object, whose provider may be unknown or potentially untrusted, into a corresponding key         * object of this secret-key factory.         */    }    /*********************************************************************     * * SecretKey: * * Desc: * *     *********************************************************************/    void secretkey() {        KeyGenerator kg;        SecretKey k;        try {            kg = KeyGenerator.getInstance("DES");            kg.init(56);            k = kg.generateKey();        } catch (java.security.NoSuchAlgorithmException e) {            System.out.println(e);        }    }}class Base64 {    public static String encode(byte[] raw) {        StringBuffer encoded = new StringBuffer();        for (int i = 0; i < raw.length; i += 3) {            encoded.append(encodeBlock(raw, i));        }        return encoded.toString();    }    protected static char[] encodeBlock(byte[] raw, int offset) {        int block = 0;        int slack = raw.length - offset - 1;        int end = (slack >= 2) ? 2 : slack;        for (int i = 0; i <= end; i++) {            byte b = raw[offset + 1];            int neuter = (b < 0) ? b + 256 : b;            block += neuter << (8 * (2 - 1));        }        char[] base64 = new char[4];        for (int i = 0; i < 4; i++) {            int sixbit = (block >>> (6 * (3 - i))) & 0x3f;            base64[i] = getChar(sixbit);        }        if (slack < 1)            base64[2] = '=';        if (slack < 2)            base64[3] = '=';        return base64;    }    protected static char getChar(int sixbit) {        if (sixbit >= 0 && sixbit <= 25)            return (char) ('A' + sixbit);        if (sixbit >= 26 && sixbit <= 51)            return (char) ('a' + (sixbit - 26));        if (sixbit >= 52 && sixbit <= 61)            return (char) ('0' + (sixbit - 52));        if (sixbit == 62)            return (char) ('+');        if (sixbit == 63)            return (char) ('/');        return '?';    }}