gpt4 book ai didi

c - C中Blowfish加密的适配问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:24 28 4
gpt4 key购买 nike

我正在编写一个程序来实现基于 Boneh-Franklin 身份的加密。对于实际的加密方法,我使用从 ( https://voltar.org/) 获得的 Blowfish。我正在尝试使河豚加密/解密代码适应我的程序。我的程序的不同之处在于,我从标准输入读取消息,加密并打印加密,然后解密并打印解密(应该是原始消息)。目前,我阅读输入消息直到“?”字符,然后尝试遵循网站上的代码。但是,解密被打印为不可读的字符。我试图解决这个问题,但我被卡住了。你能帮帮我吗?

//initializations

BF_KEY s_key; //shared key of the blowfish encryption
char plain[1000000]; //the plaintext of the message
char cipher[1000000]; //the ciphertext of the message
char byte; //to hold the byte of the msg
char *token; //points to tokens of the msg
char IV[8]="MY*IV000"; //the initialization vector
int offset = 0; //the offset of encryption
int b_count = 0; //number of bytes in d_buf
char block[8]; //blocks of encryption
char msg[1000000]; //the input msg from the user
int j; //used to read input in a loop with getchar
int i; //for-loop value
int len; //used to calculate lengths different variables
int f; //flag for the setup stage
int q; //used to read input in a loop with getchar
q=0; //reset the index reader
char in; //to read characters
printf("Please enter the message you wish to send:\n");

************ 这是我读取输入消息的代码:***************

//this loop reads the input from the user since C does not 
//provide a safe function to read strings with white spaces

while (in != '?'){
in=getchar();
if(in != '?') //dont include the delim character in the string
msg[q++]=in;
}
msg[q]='\0'; //truncate the string by the null character

************ 然后我使用代码(引用和引用)进行加密 ***************

当然,我将其修改为从 stdin 而非程序 args 读取的消息

for(i=0; i<strlen(msg); i++)    //copy the input message to plain
plain[i] = msg[i];

//set up the shared key of the BF encryption

BF_set_key(&s_key, strlen(ekey_buf), ekey_buf);

while(1){
for(i=0; i<8; i++) //reinitiate the block of 8 characters each time

block[i] = 0;
strncpy(block, plain+offset, 8);
BF_cbc_encrypt(plain+offset, cipher, 8, &s_key, IV, BF_ENCRYPT);
for(i=0; i<strlen(cipher); i++){
printf("%02x", (unsigned char) cipher[i]);
}
if( strlen(plain+offset)>8 ){ //if there is still more characters
offset += 8; //process the next block of 8 characters
} else
break;
}
//the cipher is correctly printed

************ 然后我使用代码(引用和引用)进行解密 ***************

在这里,我排除了它标记密码并创建用于解密的普通 char 数组的部分,我只是传递要解密的密码数组(因为它是加密函数的输出),并存储在普通 char 数组中长度 = strlen(密码)

//set up the shared key of the BF encryption

BF_set_key(&s_key, strlen(dkey_buf), dkey_buf);
BF_cbc_encrypt(cipher, plain, strlen(cipher), &s_key, IV, BF_DECRYPT);

printf("plain after decryption: %s\n", plain);
//HERE IS THE PROBLEM: I get unreadable characters as output of this line

感谢任何帮助。对于给您带来的不便,我们深表歉意,在此先感谢您。

最佳答案

我有预感这是一个脏的 IV,但这只是一个猜测。

for(i=0; i<8; i++) ivec[i] = 'i';
BF_cbc_encrypt(inputz, outputz, strlen(inputz), &key, ivec, BF_ENCRYPT);

// won't decrypt right:
BF_cbc_encrypt(inputz, outputz, strlen(inputz), &key, ivec, BF_DECRYPT);
// without resetting the ivec to all 'i's, the decryption will fail.

// This would work though:

for(i=0; i<8; i++) ivec[i] = 'i';
BF_cbc_encrypt(inputz, outputz, strlen(inputz), &key, ivec, BF_ENCRYPT);

for(i=0; i<8; i++) ivec[i] = 'i';
BF_cbc_encrypt(inputz, outputz, strlen(inputz), &key, ivec, BF_DECRYPT);

我的猜测只有在第一个 block 之后的每个 block 都正确解密时才是正确的。

strlen(inputz) 的另一个大问题是,如果 strlen() 没有恰好落在 8 字节边界上,您的解密最终将失败。我已经相当完整地解决了这个问题 gist on github .

关于c - C中Blowfish加密的适配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/755994/

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