001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import javax.crypto.Cipher;
020: import javax.crypto.SecretKey;
021: import javax.crypto.IllegalBlockSizeException;
022: import javax.crypto.KeyGenerator;
023: import java.io.UnsupportedEncodingException;
024: import java.io.IOException;
025: import java.security.SecureRandom;
026: import java.security.Security;
027:
028: /**
029: * Implemenation of the cryptograph for the DES algorithm.
030: * Inspired from some code found at http://javaalmanac.com/
031: *
032: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
033: */
034: public class DESCryptograph implements Cryptograph {
035: /** encryption cypher */
036: Cipher ecipher;
037: /** decryption cypher */
038: Cipher dcipher;
039:
040: /** Constructor.
041: */
042: public DESCryptograph() {
043:
044: }
045:
046: /**
047: * initialization.
048: * @param random random string
049: */
050: public void init(String random) {
051: try {
052: // this is the only method that gives us reproducibility
053: SecureRandom seed = SecureRandom.getInstance("SHA1PRNG");
054: seed.setSeed(random.getBytes());
055: KeyGenerator keygen = KeyGenerator.getInstance("DES");
056: keygen.init(seed);
057: SecretKey key = keygen.generateKey();
058:
059: ecipher = Cipher.getInstance("DES");
060: dcipher = Cipher.getInstance("DES");
061: ecipher.init(Cipher.ENCRYPT_MODE, key);
062: dcipher.init(Cipher.DECRYPT_MODE, key);
063:
064: } catch (javax.crypto.NoSuchPaddingException e) {
065: e.printStackTrace();
066: } catch (java.security.NoSuchAlgorithmException e) {
067: e.printStackTrace();
068: } catch (java.security.InvalidKeyException e) {
069: e.printStackTrace();
070: }
071: }
072:
073: /**
074: * encrypt a string.
075: * @param str string to encrypt
076: * @return encrypted string
077: */
078: public String encrypt(String str) {
079: try {
080: // Encode the string into bytes using utf-8
081: byte[] utf8 = str.getBytes("UTF8");
082:
083: // Encrypt
084: byte[] enc = ecipher.doFinal(utf8);
085:
086: // Encode bytes to base64 to get a string
087: return new sun.misc.BASE64Encoder().encode(enc);
088: } catch (javax.crypto.BadPaddingException e) {
089: e.printStackTrace();
090: } catch (IllegalBlockSizeException e) {
091: e.printStackTrace();
092: } catch (UnsupportedEncodingException e) {
093: e.printStackTrace();
094: } catch (java.io.IOException e) {
095: e.printStackTrace();
096: }
097: return null;
098: }
099:
100: /**
101: * Decrypt a string.
102: * @param str string to decrypt
103: * @return decrypted string
104: */
105: public String decrypt(String str) {
106: try {
107: // Decode base64 to get bytes
108: byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
109:
110: // Decrypt
111: byte[] utf8 = dcipher.doFinal(dec);
112:
113: // Decode using utf-8
114: return new String(utf8, "UTF8");
115: } catch (javax.crypto.BadPaddingException e) {
116: } catch (IllegalBlockSizeException e) {
117: } catch (UnsupportedEncodingException e) {
118: } catch (java.io.IOException e) {
119: }
120: return null;
121: }
122:
123: static {
124: Security.addProvider(new com.sun.crypto.provider.SunJCE());
125: }
126:
127: /**
128: * test method
129: * @param args not used
130: */
131: public static void main(String args[]) {
132: DESCryptograph crypt = new DESCryptograph();
133: crypt.init("hello there!");
134: while (true) {
135: try {
136: StringBuffer rst = new StringBuffer();
137: int c;
138: while ((c = System.in.read()) != 0x0A) {
139: if (c != 0x0D)
140: rst.append((char) c);
141: }
142: String text = rst.toString();
143: System.out.println("text -> <" + rst);
144: String enc = crypt.encrypt(text);
145: System.out.println("encrypted -> <" + enc + ">");
146: enc = enc.replace('=', '.');
147: enc = enc.replace('/', '_');
148: enc = enc.replace('+', '*');
149: System.out.println("encoded -> <" + enc + ">");
150: String dec = enc;
151: dec = dec.replace('.', '=');
152: dec = dec.replace('_', '/');
153: dec = dec.replace('*', '+');
154: System.out.println("decoded -> <" + dec + ">");
155: System.out.println("decrypted -> <"
156: + crypt.decrypt(dec) + ">");
157: } catch (IOException ioe) {
158: Logger.log(ioe);
159: }
160: }
161: }
162: }
|