📄 rijndael.java
字号:
System.arraycopy(a, 0, t, 0, BC);
}
for(int i = 0; i < BC; i++)
{
int tt = Ke[ROUNDS][i];
result[j++] = (byte)(S[t[i] >>> 24 & 0xff] ^ tt >>> 24);
result[j++] = (byte)(S[t[(i + s1) % BC] >>> 16 & 0xff] ^ tt >>> 16);
result[j++] = (byte)(S[t[(i + s2) % BC] >>> 8 & 0xff] ^ tt >>> 8);
result[j++] = (byte)(S[t[(i + s3) % BC] & 0xff] ^ tt);
}
return result;
}
public static byte[] blockDecrypt(byte in[], int inOffset, Object sessionKey, int blockSize)
{
if(blockSize == 16)
return blockDecrypt(in, inOffset, sessionKey);
Object sKey[] = (Object[])sessionKey;
int Kd[][] = (int[][])sKey[1];
int BC = blockSize / 4;
int ROUNDS = Kd.length - 1;
int SC = BC != 4 ? ((int) (BC != 6 ? 2 : 1)) : 0;
int s1 = shifts[SC][1][1];
int s2 = shifts[SC][2][1];
int s3 = shifts[SC][3][1];
int a[] = new int[BC];
int t[] = new int[BC];
byte result[] = new byte[blockSize];
int j = 0;
for(int i = 0; i < BC; i++)
t[i] = ((in[inOffset++] & 0xff) << 24 | (in[inOffset++] & 0xff) << 16 | (in[inOffset++] & 0xff) << 8 | in[inOffset++] & 0xff) ^ Kd[0][i];
for(int r = 1; r < ROUNDS; r++)
{
for(int i = 0; i < BC; i++)
a[i] = T5[t[i] >>> 24 & 0xff] ^ T6[t[(i + s1) % BC] >>> 16 & 0xff] ^ T7[t[(i + s2) % BC] >>> 8 & 0xff] ^ T8[t[(i + s3) % BC] & 0xff] ^ Kd[r][i];
System.arraycopy(a, 0, t, 0, BC);
}
for(int i = 0; i < BC; i++)
{
int tt = Kd[ROUNDS][i];
result[j++] = (byte)(Si[t[i] >>> 24 & 0xff] ^ tt >>> 24);
result[j++] = (byte)(Si[t[(i + s1) % BC] >>> 16 & 0xff] ^ tt >>> 16);
result[j++] = (byte)(Si[t[(i + s2) % BC] >>> 8 & 0xff] ^ tt >>> 8);
result[j++] = (byte)(Si[t[(i + s3) % BC] & 0xff] ^ tt);
}
return result;
}
public static int getRounds(int keySize, int blockSize)
{
switch(keySize)
{
case 16: // '\020'
return blockSize != 16 ? blockSize != 24 ? 14 : 12 : 10;
case 24: // '\030'
return blockSize == 32 ? 14 : 12;
}
return 14;
}
public static final String ident = "$Id: Rijndael.java,v 0.0 2000/12/26 $";
private Object rijndaelKey;
static final int BLOCK_SIZE = 16;
static final int alog[];
static final int log[];
static final byte S[];
static final byte Si[];
static final int T1[];
static final int T2[];
static final int T3[];
static final int T4[];
static final int T5[];
static final int T6[];
static final int T7[];
static final int T8[];
static final int U1[];
static final int U2[];
static final int U3[];
static final int U4[];
static final byte rcon[];
static final int shifts[][][] = {
{
{
0, 0
}, {
1, 3
}, {
2, 2
}, {
3, 1
}
}, {
{
0, 0
}, {
1, 5
}, {
2, 4
}, {
3, 3
}
}, {
{
0, 0
}, {
1, 7
}, {
3, 5
}, {
4, 4
}
}
};
private static final char HEX_DIGITS[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'
};
static
{
alog = new int[256];
log = new int[256];
S = new byte[256];
Si = new byte[256];
T1 = new int[256];
T2 = new int[256];
T3 = new int[256];
T4 = new int[256];
T5 = new int[256];
T6 = new int[256];
T7 = new int[256];
T8 = new int[256];
U1 = new int[256];
U2 = new int[256];
U3 = new int[256];
U4 = new int[256];
rcon = new byte[30];
long time = System.currentTimeMillis();
int ROOT = 283;
int j = 0;
alog[0] = 1;
for(int i = 1; i < 256; i++)
{
j = alog[i - 1] << 1 ^ alog[i - 1];
if((j & 0x100) != 0)
j ^= ROOT;
alog[i] = j;
}
for(int i = 1; i < 255; i++)
log[alog[i]] = i;
byte A[][] = {
{
1, 1, 1, 1, 1, 0, 0, 0
}, {
0, 1, 1, 1, 1, 1, 0, 0
}, {
0, 0, 1, 1, 1, 1, 1, 0
}, {
0, 0, 0, 1, 1, 1, 1, 1
}, {
1, 0, 0, 0, 1, 1, 1, 1
}, {
1, 1, 0, 0, 0, 1, 1, 1
}, {
1, 1, 1, 0, 0, 0, 1, 1
}, {
1, 1, 1, 1, 0, 0, 0, 1
}
};
byte B[] = {
0, 1, 1, 0, 0, 0, 1, 1
};
byte box[][] = new byte[256][8];
box[1][7] = 1;
for(int i = 2; i < 256; i++)
{
j = alog[255 - log[i]];
for(int t = 0; t < 8; t++)
box[i][t] = (byte)(j >>> 7 - t & 0x1);
}
byte cox[][] = new byte[256][8];
for(int i = 0; i < 256; i++)
{
for(int t = 0; t < 8; t++)
{
cox[i][t] = B[t];
for(j = 0; j < 8; j++)
cox[i][t] ^= A[t][j] * box[i][j];
}
}
for(int i = 0; i < 256; i++)
{
S[i] = (byte)(cox[i][0] << 7);
for(int t = 1; t < 8; t++)
S[i] ^= cox[i][t] << 7 - t;
Si[S[i] & 0xff] = (byte)i;
}
byte G[][] = {
{
2, 1, 1, 3
}, {
3, 2, 1, 1
}, {
1, 3, 2, 1
}, {
1, 1, 3, 2
}
};
byte AA[][] = new byte[4][8];
for(int i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
AA[i][j] = G[i][j];
AA[i][i + 4] = 1;
}
byte iG[][] = new byte[4][4];
for(int i = 0; i < 4; i++)
{
byte pivot = AA[i][i];
if(pivot == 0)
{
int t;
for(t = i + 1; AA[t][i] == 0 && t < 4; t++);
if(t == 4)
throw new RuntimeException("G matrix is not invertible");
for(j = 0; j < 8; j++)
{
byte tmp = AA[i][j];
AA[i][j] = AA[t][j];
AA[t][j] = tmp;
}
pivot = AA[i][i];
}
for(j = 0; j < 8; j++)
if(AA[i][j] != 0)
AA[i][j] = (byte)alog[((255 + log[AA[i][j] & 0xff]) - log[pivot & 0xff]) % 255];
for(int t = 0; t < 4; t++)
{
if(i == t)
continue;
for(j = i + 1; j < 8; j++)
AA[t][j] ^= mul(AA[i][j], AA[t][i]);
AA[t][i] = 0;
}
}
for(int i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
iG[i][j] = AA[i][j + 4];
for(int t = 0; t < 256; t++)
{
int s = S[t];
T1[t] = mul4(s, G[0]);
T2[t] = mul4(s, G[1]);
T3[t] = mul4(s, G[2]);
T4[t] = mul4(s, G[3]);
s = Si[t];
T5[t] = mul4(s, iG[0]);
T6[t] = mul4(s, iG[1]);
T7[t] = mul4(s, iG[2]);
T8[t] = mul4(s, iG[3]);
U1[t] = mul4(t, iG[0]);
U2[t] = mul4(t, iG[1]);
U3[t] = mul4(t, iG[2]);
U4[t] = mul4(t, iG[3]);
}
rcon[0] = 1;
int r = 1;
for(int t = 1; t < 30;)
rcon[t++] = (byte)(r = mul(2, r));
time = System.currentTimeMillis() - time;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -