LFSRC语言与python代码

C语言版本

该版本为大佬所写

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>

void printf_hex(unsigned char *data, unsigned int dataLen, int offset);
unsigned char filter(unsigned char *stat);
int LFSR_256_run(unsigned char stat[32], unsigned char inLastBit, unsigned char *outBit);
int my_KDF(const unsigned char *cdata, int dataLen, int keyLen, unsigned char *retData);

int main() {
   

    /********** STEP 1.2: key exchange and get session key **********/

    printf("[-] the secret random :");
    unsigned char *random = "abcdefghijklmnopqrstuvwxyz12345678"; // len 33
    printf_hex(random, 32, 4);

    printf("[+] Session Key :");
    unsigned char sessionKey[128];
    my_KDF(random, strlen(random), 128, sessionKey);
    printf_hex(sessionKey, 128, 4);

    /********** STEP 2.1: init key stream                  **********/

    printf("[+] init LFSR :");
    unsigned char LFSR1[32] = {
   0};
    unsigned char LFSR2[32] = {
   0};
    unsigned char LFSR3[32] = {
   0};
    unsigned char LFSR4[32] = {
   0};
    unsigned char tmpHashBuf[64];
    unsigned char tmpDataBuf[20];
    unsigned char *date = "20200707";
    unsigned char *stuNum = "19151213595";

    sprintf(tmpDataBuf, "%s%s", stuNum, date);
    SHA512(tmpDataBuf, 20, tmpHashBuf);
    for(int i = 0; i < 32; i++){
   
        LFSR3[i] = tmpHashBuf[i] ^ sessionKey[i];
    }
    for(int i = 0; i < 32; i++){
   
        LFSR4[i] = tmpHashBuf[i+32] ^ sessionKey[i+32];
    }

    sprintf(tmpDataBuf, "%s%s", date, stuNum);
    SHA512(tmpDataBuf, 20, tmpHashBuf);
    for(int i = 0; i < 32; i++){
   
        LFSR1[i] = tmpHashBuf[i] ^ sessionKey[i+64];
    }
    for(int i = 0; i < 32; i++){
   
        LFSR2[i] = tmpHashBuf[i+32] ^ sessionKey[i+96];
    }
    printf("\n\tLFSR1:");
    printf_hex(LFSR1, 32, 4);
    printf("\tLFSR2:");
    printf_hex(LFSR2, 32, 4);
    printf("\tLFSR3:");
    printf_hex(LFSR3, 32, 4);
    printf("\tLFSR4:");
    printf_hex(LFSR4, 32, 4);


    /********** STEP 2.2: ouput key stream and encrypt     **********/

    printf("[+] load big file (~256MB)\n");
    FILE *cFp;
    if( (cFp = fopen("../1.jpg", "rb")) == NULL ){
   
        printf("    Fail to open file!\n");
        return -1;
    }

    FILE *eFp;
    if( (eFp = fopen("../1.jpg.enc", "wb")) == NULL ){
   
        printf("    Fail to open file!\n");
        return -1;
    }

    printf("[+] output key stream :");
    // 初始化256轮
    unsigned char inBit = 0;
    unsigned char outBit;
    for(int i = 0; i < 256; i++){
   
        LFSR_256_run(LFSR1, inBit, &outBit);
        inBit = outBit;
        LFSR_256_run(LFSR2, inBit, &outBit);
        inBit = outBit;
        LFSR_256_run(LFSR3, inBit, &outBit);
        inBit 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值