// Download:http://www.codefans.net
// Source File Name: Blowfish.java
import java.security.MessageDigest;
import java.util.Random;
public class Blowfish
{
private static class BlowfishCBC extends BlowfishECB
{
public long getCBCIV()
{
return m_lCBCIV;
}
public void getCBCIV(byte dest[])
{
Blowfish.longToByteArray(m_lCBCIV, dest, 0);
}
public void setCBCIV(long lNewCBCIV)
{
m_lCBCIV = lNewCBCIV;
}
public void setCBCIV(byte newCBCIV[])
{
m_lCBCIV = Blowfish.byteArrayToLong(newCBCIV, 0);
}
public void cleanUp()
{
m_lCBCIV = 0L;
super.cleanUp();
}
private long encryptBlockCBC(long lPlainblock)
{
lPlainblock ^= m_lCBCIV;
lPlainblock = super.encryptBlock(lPlainblock);
return m_lCBCIV = lPlainblock;
}
private long decryptBlockCBC(long lCipherblock)
{
long lTemp = lCipherblock;
lCipherblock = super.decryptBlock(lCipherblock);
lCipherblock ^= m_lCBCIV;
m_lCBCIV = lTemp;
return lCipherblock;
}
public void encrypt(byte inbuffer[], byte outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI += 8)
{
long lTemp = Blowfish.byteArrayToLong(inbuffer, nI);
lTemp = encryptBlockCBC(lTemp);
Blowfish.longToByteArray(lTemp, outbuffer, nI);
}
}
public void encrypt(byte buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI += 8)
{
long lTemp = Blowfish.byteArrayToLong(buffer, nI);
lTemp = encryptBlockCBC(lTemp);
Blowfish.longToByteArray(lTemp, buffer, nI);
}
}
public void encrypt(int inbuffer[], int outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI += 2)
{
long lTemp = Blowfish.intArrayToLong(inbuffer, nI);
lTemp = encryptBlockCBC(lTemp);
Blowfish.longToIntArray(lTemp, outbuffer, nI);
}
}
public void encrypt(int buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI += 2)
{
long lTemp = Blowfish.intArrayToLong(buffer, nI);
lTemp = encryptBlockCBC(lTemp);
Blowfish.longToIntArray(lTemp, buffer, nI);
}
}
public void encrypt(long inbuffer[], long outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI++)
outbuffer[nI] = encryptBlockCBC(inbuffer[nI]);
}
public void encrypt(long buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI++)
buffer[nI] = encryptBlockCBC(buffer[nI]);
}
public void decrypt(byte inbuffer[], byte outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI += 8)
{
long lTemp = Blowfish.byteArrayToLong(inbuffer, nI);
lTemp = decryptBlockCBC(lTemp);
Blowfish.longToByteArray(lTemp, outbuffer, nI);
}
}
public void decrypt(byte buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI += 8)
{
long lTemp = Blowfish.byteArrayToLong(buffer, nI);
lTemp = decryptBlockCBC(lTemp);
Blowfish.longToByteArray(lTemp, buffer, nI);
}
}
public void decrypt(int inbuffer[], int outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI += 2)
{
long lTemp = Blowfish.intArrayToLong(inbuffer, nI);
lTemp = decryptBlockCBC(lTemp);
Blowfish.longToIntArray(lTemp, outbuffer, nI);
}
}
public void decrypt(int buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI += 2)
{
long lTemp = Blowfish.intArrayToLong(buffer, nI);
lTemp = decryptBlockCBC(lTemp);
Blowfish.longToIntArray(lTemp, buffer, nI);
}
}
public void decrypt(long inbuffer[], long outbuffer[])
{
int nLen = inbuffer.length;
for(int nI = 0; nI < nLen; nI++)
outbuffer[nI] = decryptBlockCBC(inbuffer[nI]);
}
public void decrypt(long buffer[])
{
int nLen = buffer.length;
for(int nI = 0; nI < nLen; nI++)
buffer[nI] = decryptBlockCBC(buffer[nI]);
}
long m_lCBCIV;
public BlowfishCBC(byte bfkey[])
{
super(bfkey);
setCBCIV(0L);
}
public BlowfishCBC(byte bfkey[], long lInitCBCIV)
{
super(bfkey);
setCBCIV(lInitCBCIV);
}
public BlowfishCBC(byte bfkey[], byte initCBCIV[])
{
super(bfkey);
setCBCIV(initCBCIV);
}
}
private static class BlowfishECB
{
public void cleanUp()
{
for(int nI = 0; nI < 18; nI++)
m_pbox[nI] = 0;
for(int nI = 0; nI < 256; nI++)
m_sbox1[nI] = m_sbox2[nI] = m_sbox3[nI] = m_sbox4[nI] = 0;
}
public static boolean selfTest()
{
byte testKey1[] = {
28, 88, 127, 28, 19, -110, 79, -17
};
int tv_p1[] = {
0x30553228, 0x6d6f295a
};
int tv_c1[] = {
0x55cb3774, 0xd13ef201
};
int tv_t1[] = new int[2];
String sTestKey2 = "Who is John Galt?";
byte testKey2[] = sTestKey2.getBytes();
int tv_p2[] = {
0xfedcba98, 0x76543210
};
int tv_c2[] = {
0xcc91732b, 0x8022f684
};
int tv_t2[] = new int[2];
BlowfishECB testbf1 = new BlowfishECB(testKey1);
testbf1.encrypt(tv_p1, tv_t1);
if(tv_t1[0] != tv_c1[0] || tv_t1[1] != tv_c1[1])
return false;
testbf1.decrypt(tv_t1);
if(tv_t1[0] != tv_p1[0] || tv_t1[1] != tv_p1[1])
return false;
BlowfishECB testbf2 = new BlowfishECB(testKey2);
testbf2.encrypt(tv_p2, tv_t2);
if(tv_t2[0] != tv_c2[0] || tv_t2[1] != tv_c2[1])
return false;
testbf2.decrypt(tv_t2);
return tv_t2[0] == tv_p2[0] && tv_t2[1] == tv_p2[1];
}
protected long encryptBlock(long lPlainBlock)
{
int nHi = Blowfish.longHi32(lPlainBlock);
int nLo = Blowfish.longLo32(lPlainBlock);
int sbox1[] = m_sbox1;
int sbox2[] = m_sbox2;
int sbox3[] = m_sbox3;
int sbox4[] = m_sbox4;
int pbox[] = m_pbox;
nHi ^= pbox[0];
nLo ^= (sbox1[nHi >>> 24] + sbox2[nHi >>> 16 & 0xff] ^ sbox3[nHi >>> 8 & 0xff]) + sbox4[nHi & 0xff] ^ pbox[1];
nHi ^= (sbox1[nLo >>> 24] + sbox2[nLo >>> 16 & 0xff] ^ sbox3[nLo >>> 8 & 0xff]) + sbox4[nLo & 0xff] ^ pbox[2];
nLo ^= (sbox1[nHi >>> 24] + sbox2[nHi >>>