DESpublic class DES extends Object This code is derived from the above source
JCIFS API
Norbert Hranitzky
and modified again by Michael B. Allen |
Fields Summary |
---|
private int[] | encryptKeys | private int[] | decryptKeys | private int[] | tempInts | private static byte[] | bytebit | private static int[] | bigbyte | private static byte[] | pc1 | private static int[] | totrot | private static byte[] | pc2 | private static int[] | SP1 | private static int[] | SP2 | private static int[] | SP3 | private static int[] | SP4 | private static int[] | SP5 | private static int[] | SP6 | private static int[] | SP7 | private static int[] | SP8 |
Constructors Summary |
---|
public DES()
| public DES(byte[] key)
if( key.length == 7 ) {
byte[] key8 = new byte[8];
makeSMBKey( key, key8 );
setKey( key8 );
} else {
setKey( key );
}
|
Methods Summary |
---|
private void | cookey(int[] raw, int[] KnL)
int raw0, raw1;
int rawi, KnLi;
int i;
for ( i = 0, rawi = 0, KnLi = 0; i < 16; ++i ) {
raw0 = raw[rawi++];
raw1 = raw[rawi++];
KnL[KnLi] = (raw0 & 0x00fc0000) << 6;
KnL[KnLi] |= (raw0 & 0x00000fc0) << 10;
KnL[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
KnL[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
++KnLi;
KnL[KnLi] = (raw0 & 0x0003f000) << 12;
KnL[KnLi] |= (raw0 & 0x0000003f) << 16;
KnL[KnLi] |= (raw1 & 0x0003f000) >>> 4;
KnL[KnLi] |= (raw1 & 0x0000003f);
++KnLi;
}
| public void | decrypt(byte[] cipherText, byte[] clearText)
decrypt( cipherText, 0, clearText, 0 );
| public byte[] | decrypt(byte[] cipherText)decrypts an array where the length must be a multiple of 8
int length = cipherText.length;
if (length % 8 != 0) {
System.out.println("Array must be a multiple of 8");
return null;
}
byte[] clearText = new byte[length];
int count = length / 8;
for (int i=0; i<count; i++)
encrypt(cipherText, i*8, clearText, i*8);
return clearText;
| private void | decrypt(byte[] cipherText, int cipherOff, byte[] clearText, int clearOff)
squashBytesToInts( cipherText, cipherOff, tempInts, 0, 2 );
des( tempInts, tempInts, decryptKeys );
spreadIntsToBytes( tempInts, 0, clearText, clearOff, 2 );
| private void | des(int[] inInts, int[] outInts, int[] keys)
int fval, work, right, leftt;
int round;
int keysi = 0;
leftt = inInts[0];
right = inInts[1];
work = ((leftt >>> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >>> 16) ^ right) & 0x0000ffff;
right ^= work;
leftt ^= (work << 16);
work = ((right >>> 2) ^ leftt) & 0x33333333;
leftt ^= work;
right ^= (work << 2);
work = ((right >>> 8) ^ leftt) & 0x00ff00ff;
leftt ^= work;
right ^= (work << 8);
right = (right << 1) | ((right >>> 31) & 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 1) | ((leftt >>> 31) & 1);
for ( round = 0; round < 8; ++round ) {
work = (right << 28) | (right >>> 4);
work ^= keys[keysi++];
fval = SP7[ work & 0x0000003f ];
fval |= SP5[(work >>> 8) & 0x0000003f ];
fval |= SP3[(work >>> 16) & 0x0000003f ];
fval |= SP1[(work >>> 24) & 0x0000003f ];
work = right ^ keys[keysi++];
fval |= SP8[ work & 0x0000003f ];
fval |= SP6[(work >>> 8) & 0x0000003f ];
fval |= SP4[(work >>> 16) & 0x0000003f ];
fval |= SP2[(work >>> 24) & 0x0000003f ];
leftt ^= fval;
work = (leftt << 28) | (leftt >>> 4);
work ^= keys[keysi++];
fval = SP7[ work & 0x0000003f ];
fval |= SP5[(work >>> 8) & 0x0000003f ];
fval |= SP3[(work >>> 16) & 0x0000003f ];
fval |= SP1[(work >>> 24) & 0x0000003f ];
work = leftt ^ keys[keysi++];
fval |= SP8[ work & 0x0000003f ];
fval |= SP6[(work >>> 8) & 0x0000003f ];
fval |= SP4[(work >>> 16) & 0x0000003f ];
fval |= SP2[(work >>> 24) & 0x0000003f ];
right ^= fval;
}
right = (right << 31) | (right >>> 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 31) | (leftt >>> 1);
work = ((leftt >>> 8) ^ right) & 0x00ff00ff;
right ^= work;
leftt ^= (work << 8);
work = ((leftt >>> 2) ^ right) & 0x33333333;
right ^= work;
leftt ^= (work << 2);
work = ((right >>> 16) ^ leftt) & 0x0000ffff;
leftt ^= work;
right ^= (work << 16);
work = ((right >>> 4) ^ leftt) & 0x0f0f0f0f;
leftt ^= work;
right ^= (work << 4);
outInts[0] = right;
outInts[1] = leftt;
| private void | deskey(byte[] keyBlock, boolean encrypting, int[] KnL)
int i, j, l, m, n;
int[] pc1m = new int[56];
int[] pcr = new int[56];
int[] kn = new int[32];
for ( j = 0; j < 56; ++j ) {
l = pc1[j];
m = l & 07;
pc1m[j] = ( (keyBlock[l >>> 3] & bytebit[m]) != 0 )? 1: 0;
}
for ( i = 0; i < 16; ++i ) {
if ( encrypting )
m = i << 1;
else
m = (15-i) << 1;
n = m+1;
kn[m] = kn[n] = 0;
for ( j = 0; j < 28; ++j ) {
l = j+totrot[i];
if ( l < 28 )
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l-28];
}
for ( j=28; j < 56; ++j ) {
l = j+totrot[i];
if ( l < 56 )
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l-28];
}
for ( j = 0; j < 24; ++j ) {
if ( pcr[pc2[j]] != 0 )
kn[m] |= bigbyte[j];
if ( pcr[pc2[j+24]] != 0 )
kn[n] |= bigbyte[j];
}
}
cookey( kn, KnL );
| public void | encrypt(byte[] clearText, byte[] cipherText)
encrypt( clearText, 0, cipherText, 0 );
| public byte[] | encrypt(byte[] clearText)encrypts an array where the length must be a multiple of 8
int length = clearText.length;
if (length % 8 != 0) {
System.out.println("Array must be a multiple of 8");
return null;
}
byte[] cipherText = new byte[length];
int count = length / 8;
for (int i=0; i<count; i++)
encrypt(clearText, i*8, cipherText, i*8);
return cipherText;
| private void | encrypt(byte[] clearText, int clearOff, byte[] cipherText, int cipherOff)
squashBytesToInts( clearText, clearOff, tempInts, 0, 2 );
des( tempInts, tempInts, encryptKeys );
spreadIntsToBytes( tempInts, 0, cipherText, cipherOff, 2 );
| public static void | makeSMBKey(byte[] key7, byte[] key8)
int i;
key8[0] = (byte) ( ( key7[0] >> 1) & 0xff);
key8[1] = (byte)(( ((key7[0] & 0x01) << 6) | (((key7[1] & 0xff)>>2) & 0xff)) & 0xff );
key8[2] = (byte)(( ((key7[1] & 0x03) << 5) | (((key7[2] & 0xff)>>3) & 0xff)) & 0xff );
key8[3] = (byte)(( ((key7[2] & 0x07) << 4) | (((key7[3] & 0xff)>>4) & 0xff)) & 0xff );
key8[4] = (byte)(( ((key7[3] & 0x0F) << 3) | (((key7[4] & 0xff)>>5) & 0xff)) & 0xff );
key8[5] = (byte)(( ((key7[4] & 0x1F) << 2) | (((key7[5] & 0xff)>>6) & 0xff)) & 0xff );
key8[6] = (byte)(( ((key7[5] & 0x3F) << 1) | (((key7[6] & 0xff)>>7) & 0xff)) & 0xff );
key8[7] = (byte)(key7[6] & 0x7F);
for (i=0;i<8;i++) {
key8[i] = (byte)( key8[i] << 1);
}
| public void | setKey(byte[] key)
// CHECK PAROTY TBD
deskey( key, true, encryptKeys );
deskey( key, false, decryptKeys );
| public static void | spreadIntsToBytes(int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen)
for ( int i = 0; i < intLen; ++i ) {
outBytes[outOff + i * 4 ] = (byte) ( inInts[inOff + i] >>> 24 );
outBytes[outOff + i * 4 + 1] = (byte) ( inInts[inOff + i] >>> 16 );
outBytes[outOff + i * 4 + 2] = (byte) ( inInts[inOff + i] >>> 8 );
outBytes[outOff + i * 4 + 3] = (byte) inInts[inOff + i];
}
| public static void | squashBytesToInts(byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen)
/// Squash bytes down to ints.
for ( int i = 0; i < intLen; ++i )
outInts[outOff + i] =
( ( inBytes[inOff + i * 4 ] & 0xff ) << 24 ) |
( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) |
( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 8 ) |
( inBytes[inOff + i * 4 + 3] & 0xff );
|
|