public class IDEA {
private byte[] Encrypt(byte[] bytekey, byte[] inputBytes, boolean flag) {//每一轮加密函数
byte[] encryptCode = new byte[8];
int[] key = get_subkey(flag, bytekey);// 分解子密钥
encrypt(key, inputBytes, encryptCode);// 进行加密操作
return encryptCode;// 返回加密数据
}
private int bytesToInt(byte[] inBytes, int startPos) {//二进制数组转换为字节
return ((inBytes[startPos] << 8) & 0xff00) +
(inBytes[startPos + 1] & 0xff);
}
private void intToBytes(int inputInt, byte[] outBytes, int startPos) {//字节转换为二进制数组
outBytes[startPos] = (byte) (inputInt >>> 8);
outBytes[startPos + 1] = (byte) inputInt;
}
private int x_multiply_y(int x, int y) {//乘法运算
if (x == 0) {
x = 0x10001 - y;
} else if (y == 0) {
x = 0x10001 - x;
} else {
int tmp = x * y;
y = tmp & 0xffff;
x = tmp >>> 16;
x = (y - x) + ((y < x) ? 1 : 0);
}
return x & 0xffff;
}
private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) {//对称算法 ,加解密用一个函数操作
int k = 0;
int a = bytesToInt(inbytes, 0);//将64位明文分为四个子块
int b = bytesToInt(inbytes, 2);
int c = bytesToInt(inbytes, 4);
int d = bytesToInt(inbytes, 6);
for (int i = 0; i < 8; i++) { //八轮循环开始
a = x_multiply_y(a, key[k++]); //步骤(1)
b += key[k++]; //(2)
b &= 0xffff;
c += key[k++]; //(3)
c &= 0xffff;
d = x_multiply_y(d, key[k++]); //(4)
int tmp1 = b;
int tmp2 = c;
c ^= a; //(5)
b ^= d;
IDEA加密算法源码(java版) 来自淘豆网m.daumloan.com转载请标明出处.