001: // crypt.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: //
007: // $LastChangedDate: 2008-01-12 00:24:24 +0000 (Sa, 12 Jan 2008) $
008: // $LastChangedRevision: 4324 $
009: // $LastChangedBy: borg-0300 $
010: //
011: // This program is free software; you can redistribute it and/or modify
012: // it under the terms of the GNU General Public License as published by
013: // the Free Software Foundation; either version 2 of the License, or
014: // (at your option) any later version.
015: //
016: // This program is distributed in the hope that it will be useful,
017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: // GNU General Public License for more details.
020: //
021: // You should have received a copy of the GNU General Public License
022: // along with this program; if not, write to the Free Software
023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: //
025: // Using this software in any meaning (reading, learning, copying, compiling,
026: // running) means that you agree that the Author(s) is (are) not responsible
027: // for cost, loss of data or any harm that may be caused directly or indirectly
028: // by usage of this softare or this documentation. The usage of this software
029: // is on your own risk. The installation and usage (starting/running) of this
030: // software may allow other people or application to access your computer and
031: // any attached devices and is highly dependent on the configuration of the
032: // software which must be done by the user of the software; the author(s) is
033: // (are) also not responsible for proper configuration and usage of the
034: // software, even if provoked by documentation provided together with
035: // the software.
036: //
037: // Any changes to this file according to the GPL as documented in the file
038: // gpl.txt aside this file in the shipment you received can be done to the
039: // lines that follows this copyright notice here, but changes must not be
040: // done inside the copyright notive above. A re-distribution must contain
041: // the intact and unchanged copyright notice.
042: // Contributions and changes to the program code must be marked as such.
043:
044: package de.anomic.tools;
045:
046: import java.text.SimpleDateFormat;
047: import java.util.Locale;
048: import java.util.Random;
049:
050: import de.anomic.kelondro.kelondroBase64Order;
051:
052: public class crypt {
053:
054: // --------------------------------------------------------
055: // Section: random salt generation
056: // --------------------------------------------------------
057:
058: private static long saltcounter = 0;
059: private static Random saltrandom = new Random(System
060: .currentTimeMillis());
061:
062: public static String randomSalt() {
063: // generate robust 48-bit random number
064: final long salt = (saltrandom.nextLong() & 0XffffffffffffL)
065: + (System.currentTimeMillis() & 0XffffffffffffL)
066: + ((1001 * saltcounter) & 0XffffffffffffL);
067: saltcounter++;
068: // we generate 48-bit salt values, that are represented as 8-character
069: // b64-encoded strings
070: return kelondroBase64Order.standardCoder.encodeLong(
071: salt & 0XffffffffffffL, 8);
072: }
073:
074: // --------------------------------------------------------
075: // Section: PBE + PublicKey based on passwords encryption
076: // --------------------------------------------------------
077:
078: public static final String vDATE = "20030925";
079: public static final String copyright = "[ 'crypt' v" + vDATE
080: + " by Michael Christen / www.anomic.de ]";
081: public static final String magicString = "crypt|anomic.de|0"; // magic identifier inside every '.crypt' - file
082: public static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
083: "yyyyMMddHHmmssSSS", Locale.ENGLISH);
084:
085: String cryptMethod; // one of ["TripleDES", "Blowfish", "DESede", "DES"]
086: private static final String defaultMethod = "PBEWithMD5AndDES"; //"DES";
087:
088: public crypt(String pbe) {
089: // this is possible, but not recommended
090: this (pbe, (pbe + "XXXXXXXX").substring(0, 8));
091: }
092:
093: public crypt(String pbe, String salt) {
094: this (pbe, salt, defaultMethod);
095: }
096:
097: private crypt(String pbe, String salt, String method) {
098: // a Password-Based Encryption. The SecretKey is created on the fly
099: if (salt.length() > 8)
100: salt = salt.substring(0, 8);
101: if (salt.length() < 8)
102: salt = (salt + "XXXXXXXX").substring(0, 8);
103:
104: // Create a cipher and initialize it for encrypting end decrypting
105: cryptMethod = method;
106: }
107:
108: // --------------------------------------------------------
109: // Section: simple Codings
110: // --------------------------------------------------------
111:
112: public static String simpleEncode(String content) {
113: return simpleEncode(content, null, 'b');
114: }
115:
116: public static String simpleEncode(String content, String key) {
117: return simpleEncode(content, key, 'b');
118: }
119:
120: public static String simpleEncode(String content, String key,
121: char method) {
122: if (key == null) {
123: key = "NULL";
124: }
125: switch (method) {
126: case 'b':
127: return "b|"
128: + kelondroBase64Order.enhancedCoder
129: .encodeString(content);
130: case 'z':
131: return "z|"
132: + kelondroBase64Order.enhancedCoder.encode(gzip
133: .gzipString(content));
134: case 'p':
135: return "p|" + content;
136: default:
137: return null;
138: }
139: }
140:
141: public static String simpleDecode(String encoded, String key) {
142: if (encoded == null || encoded.length() < 3) {
143: return null;
144: }
145: if (encoded.charAt(1) != '|') {
146: return encoded;
147: } // not encoded
148: switch (encoded.charAt(0)) {
149: case 'b':
150: return kelondroBase64Order.enhancedCoder.decodeString(
151: encoded.substring(2),
152: "de.anomic.tools.crypt.simpleDecode()");
153: case 'z':
154: return gzip.gunzipString(kelondroBase64Order.enhancedCoder
155: .decode(encoded.substring(2),
156: "de.anomic.tools.crypt.simpleDecode()"));
157: case 'p':
158: return encoded.substring(2);
159: default:
160: return null;
161: }
162: }
163:
164: }
|