Quadcap Embeddable Database

com/quadcap/crypto/KeyFactory.java

Go to the documentation of this file.
00001 package com.quadcap.crypto; 00002 00003 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Random; 00042 00043 /** 00044 * A simple key factory to localize the (hardcoded) choice of ciphers. 00045 * 00046 * @author Stan Bailes 00047 */ 00048 public class KeyFactory { 00049 /** 00050 * Create a symmetric key using the specified random number generator 00051 * and the default symmetric key algorithm 00052 */ 00053 public static SymmetricKey createSymmetricKey(Random r) { 00054 SymmetricKey k = new Tea(); 00055 k.init(r); 00056 return k; 00057 } 00058 00059 /** 00060 * Create a symmetric key seeded with the specified pass phrase 00061 * This fellow creates a Rijndael 128 bit key 00062 */ 00063 public static SymmetricKey createSymmetricKey(String passphrase) { 00064 return createSymmetricKey("aes:128", passphrase); 00065 } 00066 00067 /** 00068 * Make the passphrase into a fixed size byte array with smearing 00069 */ 00070 public static byte[] bytesFromPassphrase(int len, String passphrase) { 00071 byte[] key = new byte[len]; 00072 long seed = 13 * passphrase.length(); 00073 for (int i = 0; i < key.length; i++) { 00074 int c = passphrase.charAt(i % passphrase.length()); 00075 seed += (c << 18) ^ c; 00076 seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); 00077 key[i] = (byte)((seed >> 5) & 0xff); 00078 } 00079 return key; 00080 } 00081 00082 /** 00083 * Create a symmetric key from an algorithm specification and a pass 00084 * phrase. 00085 * 00086 */ 00087 public static SymmetricKey createSymmetricKey(String algo, 00088 String passphrase) { 00089 SymmetricKey k = null; 00090 00091 algo = algo.toLowerCase(); 00092 if (algo.startsWith("aes")) { 00093 int len = 16; 00094 int idx = algo.indexOf(':'); 00095 if (idx > 0) { 00096 len = Integer.parseInt(algo.substring(idx+1)) / 8; 00097 } 00098 Rijndael rk = new Rijndael(); 00099 rk.init(bytesFromPassphrase(len, passphrase)); 00100 k = rk; 00101 } else if (algo.startsWith("tea:256")) { 00102 Tea256 tk = new Tea256(); 00103 tk.init(bytesFromPassphrase(256, passphrase)); 00104 k = tk; 00105 } else if (algo.startsWith("tea:128")) { 00106 Tea tk = new Tea(); 00107 tk.init(bytesFromPassphrase(128, passphrase)); 00108 k = tk; 00109 } 00110 return k; 00111 } 00112 00113 /** 00114 * Create a public/private key pair and return the private portion 00115 */ 00116 public static PrivateKey createPrivateKey(Random r, String alg, 00117 String name) { 00118 RSAPrivateKey k = new RSAPrivateKey(); 00119 k.init(name, 1024, r); 00120 return k; 00121 } 00122 00123 /** 00124 * Restore a symmetric key from its serialized form 00125 */ 00126 public static SymmetricKey readSymmetricKey(String s) { 00127 Tea t = new Tea(); 00128 t.init(s); 00129 return t; 00130 } 00131 00132 /** 00133 * Restore a public key from its serialized form 00134 */ 00135 public static PublicKey readPublicKey(String s) { 00136 RSAPublicKey k = new RSAPublicKey(); 00137 k.init(s); 00138 return k; 00139 } 00140 00141 /** 00142 * Restore a private key from its serialized form 00143 */ 00144 public static PrivateKey readPrivateKey(String s) { 00145 RSAPrivateKey k = new RSAPrivateKey(); 00146 k.init(s); 00147 return k; 00148 } 00149 00150 /** 00151 * Create a digest 00152 */ 00153 public static Digest createDigest(String alg) { 00154 return new SHA1Digest(); 00155 } 00156 00157 public static Random createRandom(String seed) { 00158 Random r = new java.util.Random(); 00159 r.setSeed(System.currentTimeMillis() * 1003 + seed.hashCode()); 00160 return r; 00161 } 00162 00163 }