KeeLoq算法的核心思想就是用8byte密鑰加密4byte明文净嘀,從而得到4byte密文或者用8byte密鑰解密4byte密文,還原出原4byte明文。KeeLoq算法演算過程需要定義一個數(shù)據(jù)寄存器,用于存放4byte明文y31y0或者4byte密文y31y0,和一個密鑰寄存器小槐,用于存放8byte密鑰k63~k0。其加密特點是運算速度快,加密性高凿跳,線性等件豌。
public class Crypt {
private static int NLF[][][][][] = new int[2][2][2][2][2];
static {
NLF[0][0][0][0][0]=0;
NLF[0][0][0][0][1]=1;
NLF[0][0][0][1][0]=1;
NLF[0][0][0][1][1]=1;
NLF[0][0][1][0][0]=0;
NLF[0][0][1][0][1]=1;
NLF[0][0][1][1][0]=0;
NLF[0][0][1][1][1]=0;
NLF[0][1][0][0][0]=0;
NLF[0][1][0][0][1]=0;
NLF[0][1][0][1][0]=1;
NLF[0][1][0][1][1]=0;
NLF[0][1][1][0][0]=1;
NLF[0][1][1][0][1]=1;
NLF[0][1][1][1][0]=1;
NLF[0][1][1][1][1]=0;
NLF[1][0][0][0][0]=0;
NLF[1][0][0][0][1]=0;
NLF[1][0][0][1][0]=1;
NLF[1][0][0][1][1]=1;
NLF[1][0][1][0][0]=1;
NLF[1][0][1][0][1]=0;
NLF[1][0][1][1][0]=1;
NLF[1][0][1][1][1]=0;
NLF[1][1][0][0][0]=0;
NLF[1][1][0][0][1]=1;
NLF[1][1][0][1][0]=0;
NLF[1][1][0][1][1]=1;
NLF[1][1][1][0][0]=1;
NLF[1][1][1][0][1]=1;
NLF[1][1][1][1][0]=0;
NLF[1][1][1][1][1]=0;
}
/*
* 獲取source第n個位數(shù)
*/
private static int getBit(long source, int n) {
long temp0 = ((long) 1 << n);
long temp1 = source & temp0;
if (temp1 != 0) {
return 1;
}
return 0;
}
/*
* source帶進(jìn)位右移
*/
private static int RRC(int soucre, int c) {
if (c != 0) {
soucre = (soucre >> 1) | 0x80000000;
} else {
soucre = (soucre >> 1) & 0x7fffffff;
}
return soucre;
}
/*
* source帶進(jìn)位左移
*/
private static int RLC(int source, int c) {
if (c != 0) {
source = (source << 1) | 1;
} else {
source = (source << 1) & 0xFFFFFFFE;
}
return source;
}
/**
* 加密
*/
public static int CRYPT(int source, long key) {
int c;
for (int i = 0; i < 528; i++) {
int nlf = NLF[getBit(source, 31)][getBit(source, 26)][getBit(source, 20)][getBit(source, 9)][getBit(source,
1)];
int y16 = getBit(source, 16);
int y0 = getBit(source, 0);
int k = getBit(key, i % 64);
int result = nlf ^ y16 ^ y0 ^ k;
if (result != 0) {
c = 1;
} else {
c = 0;
}
source = RRC(source, c);
}
return source;
}
/**
* 解密
*/
public static int DECRYPT(int source, long key) {
int c;
for (int i = 528; i > 0; i--) {
int nlf = NLF[getBit(source, 30)][getBit(source, 25)][getBit(source, 19)][getBit(source, 8)][getBit(source,
0)];
int y15 = getBit(source, 15);
int y31 = getBit(source, 31);
int k = getBit(key, (i - 1) % 64);
int result = nlf ^ y15 ^ y31 ^ k;
if (result != 0) {
c = 1;
} else {
c = 0;
}
source = RLC(source, c);
}
return source;
}
public static void main(String[] args) {
long key=0xefcdab2143658709L;
int source = 1520149488;
System.out.println("加密前的數(shù)據(jù):" + source);
int cryptData = CRYPT(source, key);
System.out.println("加密后的數(shù)據(jù):" + cryptData);
int decryptData = DECRYPT(cryptData, key);
System.out.println("解密后的數(shù)據(jù):" + decryptData);
}
}
控制臺輸出
11381449-b8a0e99392935433.png