gpt4 book ai didi

c - IDEA/CFB模式解密和损坏的 block

转载 作者:行者123 更新时间:2023-11-30 16:37:12 25 4
gpt4 key购买 nike

我正在尝试了解 OpenSSL 库。我用 C 编写了一个程序,它应该解密使用 OpenSSL 1.1.0g 加密的 IDEA/CFB 模式。这是到目前为止的代码:

#include <fcntl.h>                     /* O_RDONLY */
#include <stdio.h> /* printf */
#include <string.h> /* memcpy */
#include <unistd.h> /* read */
#include <openssl/idea.h> /* BF_* */
#include <openssl/evp.h>
#include <openssl/ripemd.h>

#define BUFFER_SIZE 861

unsigned char key[16];
unsigned char iv[8];

int read_file(char *file, unsigned char *buffer);

int main(void)
{

unsigned char buffer[BUFFER_SIZE];
unsigned char cipher_buffer[BUFFER_SIZE];
unsigned char plain[BUFFER_SIZE];
unsigned char iv_buffer[BUFFER_SIZE];
unsigned char hash_buffer[BUFFER_SIZE];
unsigned char final[BUFFER_SIZE];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER *ciph;
char *cipher = "./cipher.bin";
char *key1 = "./key.bin";

//read key
int blah = read_file(key1,buffer);
printf("\nKeylength: %d\n", EVP_CIPHER_key_length(EVP_idea_cfb64()));
printf("IV Length: %d\n",EVP_CIPHER_iv_length(EVP_idea_cfb64()));
printf("\nKEY:");
for(int i = 0;i<16;i++){
key[i] = buffer[i];
printf("%d ", buffer[i]);
}

printf("\nIV : ");
for(int i = 16;i<24;i++){
iv[i] = buffer[i];
printf("%d ", iv[i]);
}

//read cipher
int cipherLen = read_file(cipher,cipher_buffer);
unsigned char cipher_text[cipherLen];
for(int i = 0; i<861;i++){
cipher_text[i] = cipher_buffer[i];
}

int outlen = 0;
unsigned char lastl[BUFFER_SIZE];
int last;
EVP_CIPHER_CTX_init(ctx);
EVP_CipherInit_ex(ctx,EVP_idea_cfb(),NULL,key,iv,0);
EVP_CipherUpdate(ctx,plain,&last,cipher_buffer,BUFFER_SIZE);
EVP_DecryptFinal_ex(ctx,plain,&last);

printf("%s",plain);

return 0;
}

int read_file(char *file, unsigned char *buffer){
int fp = open(file,O_RDONLY);
//printf("fd:%d File: %s\n",fp,file);
if(fp == -1){
perror("File not found");
}
int f_size = lseek (fp , 0 , SEEK_END);
lseek (fp , 0 ,SEEK_SET);
printf(" f_size:%d\n",f_size);
read(fp,buffer,BUFFER_SIZE);

//printf("lol:%d\n", f_size);
return f_size;
}

我遇到的问题是,当我打印结果时,一切正常,但文本的第一个 block 已损坏(前 8 个字节),我认为这与 CFB 模式有关,但我无法换行我的头脑围绕着这个。 :(

这是输出: Output

这是两个文件(cipher.bin 和 key.bin):

cipher.bin

key.bin

最佳答案

我这边犯了一个大错误

我从文件中读取了 key 并弄乱了索引...

IV vector 已损坏

int j=0;
for(int i = 16;i<24;i++){
iv[j] = buffer[i];
printf("0x%02x,", iv[i]);
j++;
}

解决了这个问题

关于c - IDEA/CFB模式解密和损坏的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48079986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com