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);

    }
}

控制台输出


Keeloq算法java_第1张图片
11381449-b8a0e99392935433.png

附:
Keeloq算法c
Keeloq算法js

你可能感兴趣的:(Keeloq算法java)