001: package org.dbbrowser;
002:
003: import java.security.InvalidAlgorithmParameterException;
004: import java.security.InvalidKeyException;
005: import java.security.NoSuchAlgorithmException;
006: import java.security.spec.InvalidKeySpecException;
007: import javax.crypto.BadPaddingException;
008: import javax.crypto.Cipher;
009: import javax.crypto.IllegalBlockSizeException;
010: import javax.crypto.KeyGenerator;
011: import javax.crypto.NoSuchPaddingException;
012: import javax.crypto.SecretKey;
013: import javax.crypto.SecretKeyFactory;
014: import javax.crypto.spec.PBEKeySpec;
015: import javax.crypto.spec.PBEParameterSpec;
016:
017: import org.apache.axis.encoding.Base64;
018:
019: public class TestEncryption {
020: public static void main(String[] args) {
021: (new TestEncryption()).runWithDES();
022: //(new TestEncryption()).runWithPBE();
023: }
024:
025: public void runWithPBE() {
026: System.out.println("Starting TestEncryption.runWithPBE...");
027: try {
028: //Use a password based encryption
029: PBEKeySpec keySpec = new PBEKeySpec("MasterPassword"
030: .toCharArray());
031:
032: //Get a secret key using the key spec and the secret key factory
033: //Generate a key for DES encryption and MD5 checksum using the key spec
034: SecretKeyFactory keyFactory = SecretKeyFactory
035: .getInstance("PBEWithMD5AndDES");
036: SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
037:
038: Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Symmetric DES encryption
039: //Init using the salt and iteration count
040: byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
041: (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
042: PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
043: salt, 20);
044: c.init(Cipher.ENCRYPT_MODE, pbeSecretKey, pbeParameterSpec);
045:
046: byte[] cleartext = "aPassword".getBytes();
047:
048: // Encrypt the cleartext
049: byte[] ciphertext = c.doFinal(cleartext);
050:
051: //Print the encrypted text
052: System.out.println("Encrypted text: "
053: + new String(ciphertext));
054:
055: //-----------------------------------------------------------------
056: //Start decryption
057: //Convert the byte[] to string using base64 encoding
058: String base64EncodedString = Base64.encode(ciphertext);
059: System.out.println("base64EncodedString: "
060: + base64EncodedString);
061:
062: //Decode the base64 encoded string
063: byte[] base64DecodedByteArray = Base64
064: .decode(base64EncodedString);
065: System.out.println("Base64 decoded string: "
066: + new String(base64DecodedByteArray));
067:
068: // Initialize the same cipher for decryption
069: c.init(Cipher.DECRYPT_MODE, pbeSecretKey, pbeParameterSpec);
070:
071: // Decrypt the ciphertext
072: byte[] recreatedClearText = c
073: .doFinal(base64DecodedByteArray);
074:
075: System.out.println(new String(cleartext) + " = "
076: + new String(recreatedClearText));
077: } catch (NoSuchAlgorithmException exc) {
078: System.out.println(exc.getMessage());
079: } catch (NoSuchPaddingException exc) {
080: System.out.println(exc.getMessage());
081: } catch (InvalidKeyException exc) {
082: System.out.println(exc.getMessage());
083: } catch (BadPaddingException exc) {
084: System.out.println(exc.getMessage());
085: } catch (IllegalBlockSizeException exc) {
086: System.out.println(exc.getMessage());
087: } catch (InvalidKeySpecException exc) {
088: System.out.println(exc.getMessage());
089: } catch (InvalidAlgorithmParameterException exc) {
090: System.out.println(exc.getMessage());
091: }
092: }
093:
094: public void runWithDES() {
095: System.out.println("Starting TestEncryption.runWithDES...");
096: try {
097: KeyGenerator keygen = KeyGenerator.getInstance("DES");
098: SecretKey desKey = keygen.generateKey();
099:
100: Cipher c = Cipher.getInstance("DES"); //Symmetric encryption
101: c.init(Cipher.ENCRYPT_MODE, desKey);
102:
103: byte[] cleartext = "portal".getBytes();
104:
105: // Encrypt the cleartext
106: byte[] ciphertext = c.doFinal(cleartext);
107:
108: // Initialize the same cipher for decryption
109: c.init(Cipher.DECRYPT_MODE, desKey);
110:
111: // Decrypt the ciphertext
112: byte[] cleartext1 = c.doFinal(ciphertext);
113:
114: System.out
115: .println("Cypher text: " + new String(ciphertext));
116: System.out.println(new String(cleartext) + " = "
117: + new String(cleartext1));
118: } catch (NoSuchAlgorithmException exc) {
119: System.out.println(exc.getMessage());
120: } catch (NoSuchPaddingException exc) {
121: System.out.println(exc.getMessage());
122: } catch (InvalidKeyException exc) {
123: System.out.println(exc.getMessage());
124: } catch (BadPaddingException exc) {
125: System.out.println(exc.getMessage());
126: } catch (IllegalBlockSizeException exc) {
127: System.out.println(exc.getMessage());
128: }
129: }
130:
131: }
|