打印
[资料干货]

KEELOQ算法—C语言

[复制链接]
553|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
无名灬李|  楼主 | 2020-7-7 11:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

源码一:KeeLoq.c (32位处理器)
[cpp] view plain copy
u32 SERX=0xefcdab2143658709;
u32 key=0xefcdab2143658709;
word HOPE=01234567;
word  hope[3];
word c=0;
int NLF[2][2][2][2][2];
int getBit(u32 source,int n) {
u32 temp0=((long long int) 1<<n);
u32 temp1=source&temp0;
if ( temp1 != 0) {
return 1;
}
return 0;
}
void RrcHOPE(){
if(c!=0){
HOPE=(HOPE>>1)|0x80000000;
}else{
HOPE=(HOPE>>1)&0x7fffffff;
}
}
void RlcHOPE(){
if(c!=0){
HOPE=(HOPE<<1)|1;
}else{
HOPE=(HOPE<<1)&0xFFFFFFFE;
}
}
void CRYPT() {
//key=SERX;
for (int i = 0; i < 528; i++) {
int nlf=NLF[getBit(HOPE, 31)][getBit(HOPE, 26)][getBit(HOPE, 20)][getBit(HOPE, 9)][getBit(HOPE, 1)];
int y16=getBit(HOPE, 16);
int y0=getBit(HOPE, 0);
int k=getBit(key, i%64);
int result=nlf^y16^y0^k;
if (result!=0) {
c=1;
}else {
c=0;
}
RrcHOPE();
}
}
void DECRYPT() {
key=SERX;
for (int i = 528; i >0; i--) {
int nlf=NLF[getBit(HOPE, 30)][getBit(HOPE, 25)][getBit(HOPE, 19)][getBit(HOPE, 8)][getBit(HOPE, 0)];
int y15=getBit(HOPE, 15);
int y31=getBit(HOPE, 31);
int k=getBit(key, (i-1)%64);
int result=nlf^y15^y31^k;
if (result!=0) {
c=1;
}else {
c=0;
}
//printf("step %d : %x %x %x %x %x %x\n",i,HOPE,nlf,y15,y31,k,result);
RlcHOPE();
}
}
int main(){
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;
while(1){
scanf("%x",&HOPE);
CRYPT();
printf("%x\n",HOPE);
DECRYPT();
printf("%x\n",HOPE);
}
return 0;
}
源码二:KeeLoq.h+KeeLoq.c(8位微机)
[cpp] view plain copy
unsigned char SERX[]={0XEF,0XCD,0XAB,0X21,0X43,0X65,0X87,0X09};
unsigned char key[]={0X09,0X87,0X65,0X43,0X21,0XAB,0XCD,0XEF};
unsigned char NLF[2][2][2][2][2];
unsigned char getBit(unsigned char source[],int n);
unsigned char RRC(unsigned char source[],char c,char n);
unsigned char RLC(unsigned char source[],char c,char n);
unsigned char CRYPT(unsigned char source);
unsigned char DECRYPT(unsigned char source);
void init();
[cpp] view plain copy
include<stdio.h>
include"KeeLoq.h"
/*****
@param source
@param n
@return source的第n个位数
***/
unsigned char getBit(unsigned char source[],int n)
{
unsigned char temp0=(unsigned char)1<<(n%8);
unsigned char temp1=source[n/8]&temp0;
if(temp1!=0)
{
return 1;
}
return 0;
}
/***
@param source[]
@param c 进位标志位
@param n 数组长度
@return source数组带进位右移
****/
unsigned char * RRC(unsigned char source[],char c,char n)
{
int i=0;
unsigned char temp;
for(i=n-1;i>=0;i--)
{
temp=source;
if(c!=0){
source=(source>>1)|0x80;
}else{
source=(source>>1)&0x7f;
}
if(temp&0x01!=0){
c=1;
}else{
c=0;
}
}
return source;
}
/*****
@param source[]
@param c 进位标志位
@param n 数组长度
@return source数组带进位左移
**/
unsigned char * RLC(unsigned char source[],char c,char n)
{
int i=0;
unsigned char temp;
for(i=0;i<n;i++)
{
temp=source;
if(c!=0){
source=(source<<1)|0x01;
}else{
source=(source<<1)&0xfe;
}
if((temp&0x80)!=0){
c=1;
}else{
c=0;
}
}
return source;
}
/*****
@param source
@return source明文经过KeeLoq加密后的密文
**/
unsigned char CRYPT(unsigned char source)
{
int i=0;
unsigned char c=0;
unsigned char nlf,y16,y0,k,result;
init();
for (i = 0; i < 528; i++)
{
nlf=NLF[getBit(source, 31)][getBit(source, 26)][getBit(source, 20)][getBit(source, 9)][getBit(source, 1)];
y16=getBit(source, 16);
y0=getBit(source, 0);
k=getBit(key, i%64);
result=nlf^y16^y0^k;
if (result!=0) {
c=1;
}else {
c=0;
}
source=RRC(source,c,4);
}
return source;
}
/*****
@param source
@return source密文经过KeeLoq解密后的明文
**/
unsigned char DECRYPT(unsigned char source)
{
int i=0;
unsigned char c=0;
unsigned char nlf,y15,y31,k,result;
init();
for (i = 528; i >0; i--)
{
nlf=NLF[getBit(source, 30)][getBit(source, 25)][getBit(source, 19)][getBit(source, 8)][getBit(source, 0)];
y15=getBit(source, 15);
y31=getBit(source, 31);
k=getBit(key, (i-1)%64);
result=nlf^y15^y31^k;
if (result!=0) {
c=1;
}else {
c=0;
}
source=RLC(source,c,4);
}
return source;
}
/*****
初始化非线性逻辑函数的值
**/
void init()
{
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;
}
int main()
{
unsigned char source[4];
unsigned char *p;
scanf("%c %c %c %c",&source[3],&source[2],&source[1],&source[0]);
p=source;
printf("%x %x %x %x\n",p[3],p[2],p[1],p[0]);
p=CRYPT(p);
printf("%x %x %x %x\n",p[3],p[2],p[1],p[0]);
p=DECRYPT(p);
printf("%x %x %x %x",p[3],p[2],p[1],p[0]);
return 0;
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

7

帖子

0

粉丝