* @return 长度为8的字节数组
*/
private byte[] Encrypt(int[] timeData, int flag, int[][] keyarray) {
int i;
byte[] encrypt = new byte[8];
int flags = flag;
int[] M = new int[64];
int[] MIP_1 = new int[64];
// 特别注意:xxx[IP[i]-1]等类似变换
for (i = 0; i < 64; i++) {
M[i] = timeData[IP[i] - 1]; // 明文IP变换
}
if (flags == 1) { // 加密
for (i = 0; i < 16; i++) {
LoopF(M, i, flags, keyarray);// S盒处理
}
} else if (flags == 0) { // 解密
for (i = 15; i > -1; i--) {
LoopF(M, i, flags, keyarray);// S盒处理
}
}
for (i = 0; i < 64; i++) {
MIP_1[i] = M[IP_1[i] - 1]; // 进行IP-1运算
}
// 将(int[64]二进制数据字节数组,经过IP、S盒、IP-1处理后,得到的新的)int[64]二进制数据字节数组转换成byte[8]的字节数组
GetEncryptResultOfByteArray(MIP_1, encrypt);
// 返回加密数据
return encrypt;
}
/**
* 转换8个字节长度的数据字节数组为二进制数组 (一个字节转换为8个二进制)
*
* @param intdata
* 8个字节的数据字节数组
* @return 长度为64的二进制数组
*/
private int[] ReadDataToBirnaryIntArray(byte[] intdata) {
int i;
int j;
// 将数据转换为二进制数,存储到数组
int[] IntDa = new int[8];
for (i = 0; i < 8; i++) {
IntDa[i] = intdata[i];// intdata[i]为byte,范围是-128~127
if (IntDa[i] < 0) {// 故:IntDa[i]范围是-128~127
IntDa[i] += 256;// IntDa[i]永远不会超过256
IntDa[i] %= 256;// 所以该处不需要取模,取模后结果还是自己
}
}
int[] IntVa = new int[64];
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
IntVa[((i * 8) + 7) - j] = IntDa[i] % 2;
IntDa[i] = IntDa[i] / 2;
}
}
return IntVa;
}
/**
* int[64]二进制数据字节数组转换成byte[8]的字节数组
*
* @param data
* int[64]二进制数据字节数组
* @param value
* byte[8] byte[8]的字节数组
private void GetEncryptResultOfByteArray(int[] data, byte[] value) {
int i;
int j;
// 将存储64位二进制数据的数组中的数据转换为八个整数(byte)
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
value[i] += (byte) (data[(i << 3) + j] << (7 - j));
}
}
for (i = 0; i < 8; i++) {
value[i] %= 256;
if (value[i] > 128) {
value[i] -= 255;
}
}
}
/**
* 左移
*
* @param k
* @param offset
*/
private void LeftBitMove(int[] k, int offset) {
int i;
// 循环移位操作函数
int[] c0 = new int[28];
int[] d0 = new int[28];
int[] c1 = new int[28];
int[] d1 = new int[28];
for (i = 0; i < 28; i++) {
c0[i] = k[i];
d0[i] = k[i + 28];
}
if (offset == 1) {
for (i = 0; i < 27; i++) { // 循环左移一位
c1[i] = c0[i + 1];
d1[i] = d0[i + 1];
}
c1[27] = c0[0];
d1[27] = d0[0];
} else if (offset == 2) {
for (i = 0; i < 26; i++) { // 循环左移两位
c1[i] = c0[i + 2];
d1[i] = d0[i + 2];
}
c1[26] = c0[0];
d1[26] = d0[0];
c1[27] = c0[1];
d1[27] = d0[1];
}
for (i = 0; i < 28; i++) {
k[i] = c1[i];
k[i + 28] = d1[i];
}
}
/**
* S盒处理
*
* @param M
* @param times
* @param flag
* @param keyarray
*/
private