/**
* $RCSfile$
* $Revision: 3657 $
* $Date: 2002-09-09 08:31:31 -0700 (Mon, 09 Sep 2002) $
*
* Adapted from Markus Hahn's Blowfish package so that all functionality is
* in a single source file. Please visit the following URL for his excellent
* package: http://www.hotpixel.net/software.html
*
* Copyright (c) 1997-2002 Markus Hahn <markus_hahn@gmx.net>
*
* Released under the Apache 2.0 license.
*/
package com.wc.tools;
import java.security.MessageDigest;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class that provides easy Blowfish encryption.<p>
*
* @author Markus Hahn <markus_hahn@gmx.net>
* @author Gaston Dombiak
*/
public class Blowfish {
private static final Logger Log = LoggerFactory.getLogger(Blowfish.class);
private BlowfishCBC m_bfish;
private static Random m_rndGen = new Random();
/**
* Creates a new Blowfish object using the specified key (oversized
* password will be cut).
*
* @param password the password (treated as a real unicode array)
*/
public Blowfish(String password) {
// hash down the password to a 160bit key
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA1");
digest.update(password.getBytes());
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
// setup the encryptor (use a dummy IV)
m_bfish = new BlowfishCBC(digest.digest(), 0);
digest.reset();
}
/**
* Encrypts a string (treated in UNICODE) using the
* standard Java random generator, which isn't that
* great for creating IVs
*
* @param sPlainText string to encrypt
* @return encrypted string in binhex format
*/
public String encryptString(String sPlainText) {
// get the IV
long lCBCIV;
synchronized (m_rndGen)
{
lCBCIV = m_rndGen.nextLong();
}
// map the call;
return encStr(sPlainText, lCBCIV);
}
// Internal routine for string encryption
private String encStr(String sPlainText,
long lNewCBCIV)
{
// allocate the buffer (align to the next 8 byte border plus padding)
int nStrLen = sPlainText.length();
byte[] buf = new byte [((nStrLen << 1) & 0xfffffff8) + 8];
// copy all bytes of the string into the buffer (use network byte order)
int nI;
int nPos = 0;
for (nI = 0; nI < nStrLen; nI++)
{
char cActChar = sPlainText.charAt(nI);
buf[nPos++] = (byte) ((cActChar >> 8) & 0x0ff);
buf[nPos++] = (byte) (cActChar & 0x0ff) ;
}
// pad the rest with the PKCS5 scheme
byte bPadVal = (byte)(buf.length - (nStrLen << 1));
while (nPos < buf.length)
{
buf[nPos++] = bPadVal;
}
synchronized (m_bfish) {
// create the encryptor
m_bfish.setCBCIV(lNewCBCIV);
// encrypt the buffer
m_bfish.encrypt(buf);
}
// return the binhex string
byte[] newCBCIV = new byte[BlowfishCBC.BLOCKSIZE];
longToByteArray(lNewCBCIV,
newCBCIV,
0);
return bytesToBinHex(newCBCIV, 0, BlowfishCBC.BLOCKSIZE) +
bytesToBinHex(buf, 0, buf.length);
}
/**
* decrypts a hexbin string (handling is case sensitive)
* @param sCipherText hexbin string to decrypt
* @return decrypted string (null equals an error)
*/
public String decryptString(String sCipherText)
{
// get the number of estimated bytes in the string (cut off broken blocks)
int nLen = (sCipherText.length() >> 1) & ~7;
// does the given stuff make sense (at least the CBC IV)?
if (nLen < BlowfishECB.BLOCKSIZE)
return null;
// get the CBC IV
byte[] cbciv = new byte[BlowfishCBC.BLOCKSIZE];
int nNumOfBytes = binHexToBytes(sCipherText,
cbciv,
0,
0,
BlowfishCBC.BLOCKSIZE);
if (nNumOfBytes < BlowfishCBC.BLOCKSIZE)
return null;
// something left to decrypt?
nLen -= BlowfishCBC.BLOCKSIZE;
if (nLen == 0)
{
return "";
}
// get all data bytes now
byte[] buf = new byte[nLen];
nNumOfBytes = binHexToBytes(sCipherText,
buf,
BlowfishCBC.BLOCKSIZE * 2,
0,
nLen);
// we cannot accept broken binhex sequences due to padding
// and decryption
if (nNumOfBytes < nLen)
{
return null;
}
synchronized (m_bfish) {
// (got it)
m_bfish.setCBCIV(cbciv);
// decrypt the buffer
m_bfish.decrypt(buf);
}
// get the last padding byte
int nPadByte = (int)buf[buf.length - 1] & 0x0ff;
// ( try to get all information if the padding doesn't seem to be correct)
if ((nPadByte > 8) || (nPadByte < 0))
{
nPadByte = 0;
}
// calculate the real size of this message
nNumOfBytes -= nPadByte;
if (nNumOfBytes < 0)
{
return "";
}
// success
return byteArrayToUNCString(buf, 0, nNumOfBytes);
}
/**
* destroys (clears) the encryption engine,
* after that the instance is not valid anymore
*/
public void destroy()
{
m_bfish.cleanUp();
}
/**
* implementation of the Blowfish encryption algorithm in ECB mode
* @author Markus Hahn <markus_hahn@gmx.net>
* @version Feburary 14, 2001
*/
private static class BlowfishECB
{
/** maximum possible key length */
public final static int MAXKEYLENGTH = 56;
/** block size of this cipher (in bytes) */
public final static int BLOCKSIZE = 8;
// size of the single boxes
final static int PBOX_ENTRIES = 18;
final static int SBOX_ENTRIES = 256;
// the boxes
int[] m_pbox;
int[] m_sbox1;
int[] m_sbox2;
int[] m_sbox3;
int[] m_sbox4;
/**
* default constructor
* @param bfkey key material, up to MAXKEYLENGTH bytes
*/
public BlowfishECB(byte[] bfkey)
{
// create the boxes
int nI;
m_pbox = new int[PBOX_ENTRIES];
for (nI = 0; nI < PBOX_ENTRIES; nI++)
{
m_pbox[nI] = pbox_init[nI];
}
m_sbox1 = new int[SBOX_ENTRIES];
m_sbox2 = new int[SBOX_ENTRIES];
m_sbox3 = new int[SBOX_ENTRIES];
m_sbox4 = new int[SBOX_ENTRIES];
for (nI = 0; nI < SBOX_ENTRIES; nI++)
{
m_sbox1[nI] = sbox_init_1[nI];
m_sbox2[nI] = sbox_init_2[nI];
m_sbox3[nI] = sbox_init_3[nI];
m_sbox4[nI] = sbox_init_4[nI];
}
// xor the key over the p-boxes
int nLen = bfkey.length;
if (nLen == 0) return; // such a setup is also valid (zero key "encryption" is possible)
int nKeyPos = 0;
int nBuild = 0;
int nJ;
for (nI = 0; nI < PBOX_ENTRIES; nI++)
{
for (nJ = 0; nJ < 4; nJ++)
{
nBuild = (nBuild << 8) | (((int) bfkey[nKeyPos]) & 0x0ff);
if (++nKeyPos == nLen)
{