001: /*
002: * DesEncrypter.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.util;
023:
024: import java.security.NoSuchAlgorithmException;
025: import java.security.SecureRandom;
026: import java.security.spec.KeySpec;
027:
028: import javax.crypto.Cipher;
029: import javax.crypto.SecretKey;
030: import javax.crypto.SecretKeyFactory;
031: import javax.crypto.spec.DESKeySpec;
032:
033: /* ----------------------------------------------------------
034: * CVS NOTE: Changes to the CVS repository prior to the
035: * release of version 3.0.0beta1 has meant a
036: * resetting of CVS revision numbers.
037: * ----------------------------------------------------------
038: */
039:
040: /**
041: *
042: * @author Takis Diakoumis
043: * @version $Revision: 1.4 $
044: * @date $Date: 2006/05/14 06:56:07 $
045: */
046: public class DesEncrypter {
047:
048: private static char[] pwdChars = "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
049: .toCharArray();
050:
051: public DesEncrypter() {
052: }
053:
054: public static String encrypt(String key, String value) {
055: try {
056: SecretKey secretKey = getSecretKey(key);
057:
058: Cipher ecipher = Cipher.getInstance("DES");
059: ecipher.init(Cipher.ENCRYPT_MODE, secretKey);
060: byte[] cleartext = value.getBytes("UTF8");
061: byte[] ciphertext = ecipher.doFinal(cleartext);
062:
063: sun.misc.BASE64Encoder base64encoder = new sun.misc.BASE64Encoder();
064: return base64encoder.encode(ciphertext);
065:
066: } catch (Exception e) {
067: e.printStackTrace();
068: }
069: return null;
070: }
071:
072: public static SecretKey getSecretKey(String key) throws Exception {
073: byte[] keyAsBytes = key.getBytes("UTF8");
074: KeySpec keySpec = new DESKeySpec(keyAsBytes);
075: SecretKeyFactory keyFactory = SecretKeyFactory
076: .getInstance("DES");
077: return keyFactory.generateSecret(keySpec);
078: }
079:
080: public static String decrypt(String key, String value) {
081: try {
082: SecretKey secretKey = getSecretKey(key);
083:
084: Cipher dcipher = Cipher.getInstance("DES");
085: dcipher.init(Cipher.DECRYPT_MODE, secretKey);
086:
087: // Decode base64 to get bytes
088: byte[] dec = new sun.misc.BASE64Decoder()
089: .decodeBuffer(value);
090: // Decrypt
091: byte[] utf8 = dcipher.doFinal(dec);
092:
093: // Decode using utf-8
094: return new String(utf8, "UTF8");
095:
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099: return null;
100: }
101:
102: public static String generateKey(int length) {
103: try {
104: StringBuffer key = new StringBuffer(length);
105: SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
106: byte[] intbytes = new byte[4];
107:
108: for (int i = 0; i < length; i++) {
109: random.nextBytes(intbytes);
110: key.append(pwdChars[Math.abs(getIntFromByte(intbytes)
111: % pwdChars.length)]);
112: }
113:
114: return key.toString();
115: } catch (NoSuchAlgorithmException e) {
116: return null;
117: }
118: }
119:
120: private static int getIntFromByte(byte[] bytes) {
121: int returnNumber = 0;
122: int pos = 0;
123: returnNumber += byteToInt(bytes[pos++]) << 24;
124: returnNumber += byteToInt(bytes[pos++]) << 16;
125: returnNumber += byteToInt(bytes[pos++]) << 8;
126: returnNumber += byteToInt(bytes[pos++]) << 0;
127: return returnNumber;
128: }
129:
130: private static int byteToInt(byte b) {
131: return (int) b & 0xFF;
132: }
133:
134: }
|