📄 des.java
字号:
if (len != BLOCK_SIZE)
{
throw new BadPaddingException("Datasize less than block size.");
}
scrunch(in, inOff, work);
desfunc(work, Kn1);
unscrunch(work, out, outOff);
return BLOCK_SIZE;
}
/**
* the DES engine.
*/
protected synchronized void desfunc(
int[] block,
int[] keys)
{
int fval, work, right, leftt;
int round;
leftt = block[0];
right = block[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)) & 0xffffffff;
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = ((leftt << 1) | ((leftt >>> 31) & 1)) & 0xffffffff;
for (round = 0; round < 8; round++)
{
work = (right << 28) | (right >>> 4);
work ^= keys[round * 4 + 0];
fval = SP7[ work & 0x3f];
fval |= SP5[(work >>> 8) & 0x3f];
fval |= SP3[(work >>> 16) & 0x3f];
fval |= SP1[(work >>> 24) & 0x3f];
work = right ^ keys[round * 4 + 1];
fval |= SP8[ work & 0x3f];
fval |= SP6[(work >>> 8) & 0x3f];
fval |= SP4[(work >>> 16) & 0x3f];
fval |= SP2[(work >>> 24) & 0x3f];
leftt ^= fval;
work = (leftt << 28) | (leftt >>> 4);
work ^= keys[round * 4 + 2];
fval = SP7[ work & 0x3f];
fval |= SP5[(work >>> 8) & 0x3f];
fval |= SP3[(work >>> 16) & 0x3f];
fval |= SP1[(work >>> 24) & 0x3f];
work = leftt ^ keys[round * 4 + 3];
fval |= SP8[ work & 0x3f];
fval |= SP6[(work >>> 8) & 0x3f];
fval |= SP4[(work >>> 16) & 0x3f];
fval |= SP2[(work >>> 24) & 0x3f];
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);
block[0] = right;
block[1] = leftt;
}
protected int encryptBlock(
byte[] in,
int inOff,
int len,
byte[] out,
int outOff)
throws IllegalBlockSizeException
{
if (len != BLOCK_SIZE)
{
throw new IllegalBlockSizeException("Datasize less than block size.");
}
scrunch(in, inOff, work);
desfunc(work, Kn1);
unscrunch(work, out, outOff);
return BLOCK_SIZE;
}
/**
* prepare the key for whatever processing we are planing.
*
* Acknowledgements for this routine go to James Gillogly & Phil Karn.
* (whoever, and wherever they are!).
*/
protected void prepareKeys(
int opMode,
byte[] key,
int[] keys)
{
byte[] pc1m, pcr;
int[] kn;
pc1m = new byte[56];
pcr = new byte[56];
kn = new int[32];
for (int j = 0; j < 56; j++ )
{
int l, m;
l = pc1[j];
m = l & 07;
pc1m[j] = (byte)(((key[l >>> 3] & bytebit[m]) != 0)
? 1 : 0);
}
for (int i = 0; i < 16; i++)
{
int l, m, n;
if (opMode == Cipher.DECRYPT_MODE)
{
m = (15 - i) << 1;
}
else
{
m = i << 1;
}
n = m + 1;
kn[m] = kn[n] = 0;
for (int j = 0; j < 28; j++)
{
l = j + totrot[i];
if ( l < 28 )
{
pcr[j] = pc1m[l];
}
else
{
pcr[j] = pc1m[l - 28];
}
}
for (int j = 28; j < 56; j++)
{
l = j + totrot[i];
if (l < 56 )
{
pcr[j] = pc1m[l];
}
else
{
pcr[j] = pc1m[l - 28];
}
}
for (int j = 0; j < 24; j++)
{
if (pcr[pc2[j]] != 0)
{
kn[m] |= bigbyte[j];
}
if (pcr[pc2[j + 24]] != 0)
{
kn[n] |= bigbyte[j];
}
}
}
/*
* store the processed key
*/
for (int i = 0; i != 16; i++)
{
int i1, i2;
i1 = kn[2 * i];
i2 = kn[2 * i + 1];
keys[i * 2] = (i1 & 0x00fc0000) << 6;
keys[i * 2] |= (i1 & 0x00000fc0) << 10;
keys[i * 2] |= (i2 & 0x00fc0000) >>> 10;
keys[i * 2] |= (i2 & 0x00000fc0) >>> 6;
keys[i * 2 + 1] = (i1 & 0x0003f000) << 12;
keys[i * 2 + 1] |= (i1 & 0x0000003f) << 16;
keys[i * 2 + 1] |= (i2 & 0x0003f000) >>> 4;
keys[i * 2 + 1] |= (i2 & 0x0000003f);
}
}
/**
* pack 8 bytes from outof into 2 ints.
*/
protected void scrunch(
byte[] outof,
int offset,
int[] into)
{
into[0] = (outof[0 + offset] & 0xff) << 24;
into[0] |= (outof[1 + offset] & 0xff) << 16;
into[0] |= (outof[2 + offset] & 0xff) << 8;
into[0] |= (outof[3 + offset] & 0xff);
into[1] = (outof[4 + offset] & 0xff) << 24;
into[1] |= (outof[5 + offset] & 0xff) << 16;
into[1] |= (outof[6 + offset] & 0xff) << 8;
into[1] |= (outof[7 + offset] & 0xff);
}
protected void setKey(Key key)
throws InvalidKeyException
{
if (!((key instanceof DESKey)
|| (key instanceof SecretKeySpec)))
{
throw new InvalidKeyException("not a DES Key");
}
prepareKeys(mode, key.getEncoded(), Kn1);
}
/**
* extract 8 bytes from the 2 ints in outof.
*/
protected void unscrunch(
int[] outof,
byte[] into,
int offset)
{
into[0 + offset] = (byte)((outof[0] >>> 24) & 0xff);
into[1 + offset] = (byte)((outof[0] >>> 16) & 0xff);
into[2 + offset] = (byte)((outof[0] >>> 8) & 0xff);
into[3 + offset] = (byte)( outof[0] & 0xff);
into[4 + offset] = (byte)((outof[1] >>> 24) & 0xff);
into[5 + offset] = (byte)((outof[1] >>> 16) & 0xff);
into[6 + offset] = (byte)((outof[1] >>> 8) & 0xff);
into[7 + offset] = (byte)( outof[1] & 0xff);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -