java keeloq_Keeloq算法 - java语言实现

KeeLoq算法的核心思想就是用8byte密钥加密4byte明文,从而得到4byte密文或者用8byte密钥解密4byte密文,还原出原4byte明文。KeeLoq算法演算过程需要定义一个数据寄存器,用于存放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个位数

*/

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带进位右移

*/

private static int RRC(int soucre, int c) {

if (c != 0) {

soucre = (soucre >> 1) | 0x80000000;

} else {

soucre = (soucre >> 1) & 0x7fffffff;

}

return soucre;

}

/*

* source带进位左移

*/

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("加密前的数据:" + source);

int cryptData = CRYPT(source, key);

System.out.println("加密后的数据:" + cryptData);

int decryptData = DECRYPT(cryptData, key);

System.out.println("解密后的数据:" + decryptData);

}

}

控制台输出

5c409eee8e25

11381449-b8a0e99392935433.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值