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