//==============================================================================
// tribble/security/KeyMaker.java
//------------------------------------------------------------------------------
package tribble.security;
// System imports
import java.lang.Exception;
import java.lang.String;
import java.security.Key;
// Local imports
import tribble.util.HexDump;
/*******************************************************************************
* Methods for generating an encryption/decryption key.
*
* @version $Revision: 1.2 $ $Date: 2003/03/19 04:32:47 $
* @since 2002-04-13
* @author
* David R. Tribble,
* david@tribble.com.
*
* Copyright ©2002 by David R. Tribble, all rights reserved.
*
* Permission is granted to freely use and distribute this source code
* provided that the original copyright and authorship notices remain
* intact.
*/
public class KeyMaker
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/security/KeyMaker.java $Revision: 1.2 $ $Date: 2003/03/19 04:32:47 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constants
/** LFSR bitmask. */
private static final int LFSR_MASK = 0x4010620A;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static methods
/***************************************************************************
* Test driver.
*
* @param args
* Command line arguments. A 256-bit binary key is generated from each
* argument.
*
* @since 1.1, 2002-04-13
*/
public static void main(String[] args)
{
// Make a 128-bit key from each password arg
for (int i = 0; i < args.length; i++)
{
byte[] key;
// Make a 128-bit key from a password arg
System.out.println("\"" + args[i] + "\"");
key = makeKey(args[i], 256/8);
HexDump.hexDump(System.out, key, 0, key.length);
System.out.println();
}
}
/***************************************************************************
* Generate a binary key from a password string.
* The password string is suitably scrambled to produce a binary key exactly
* n bytes long.
*
* @param pwd
* A string containing a password.
* This can be null or empty ("").
*
* @param n
* The length (in bytes) of the resulting binary key.
*
* @return
* An n-byte encryption/decryption key value.
*
* @since 1.1, 2002-04-13
*/
public static byte[] makeKey(String pwd, int n)
{
byte[] key;
int lfsr;
int len;
int i;
int o;
// Check args
if (pwd == null || pwd.length() == 0)
pwd = "\0";
// Set up
len = pwd.length();
key = new byte[n];
lfsr = -1;
// Scramble the bytes of the password
i = 0;
o = 0;
for (int m = (n >= len ? n : len); m >= 0; m--)
{
int ch;
if (i >= len)
i = 0;
// Mix in the next char of the password
ch = pwd.charAt(i++);
if (o >= n)
o = 0;
lfsr += (ch & 0xFF);
lfsr = (lfsr & LFSR_MASK) ^ ((lfsr << 1) | (lfsr >>> 32-1));
key[o++] ^= (byte) lfsr;
ch >>= 8;
if (ch != 0x00)
{
if (o >= n)
o = 0;
lfsr += ch;
lfsr = (lfsr & LFSR_MASK) ^ ((lfsr << 1) | (lfsr >>> 32-1));
key[o++] ^= (byte) lfsr;
}
}
return (key);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constructors
/***************************************************************************
* Constructor.
*
* @since 1.1, 2002-04-13
*/
private KeyMaker()
{
// Do nothing
}
}
// End KeyMaker.java