我正在编写一个实现Boneh-Franklin Identity Based Encryption的程序。对于实际的加密方法,我使用的是来自(https://voltar.org/)的Blowfish。我正在尝试将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
************然后我使用代码(引用和引用)进行解密***************
在这里,我排除了它对密码进行了标记的部分并创建了用于解密的普通字符数组,我只是传递了要解密的密码数组(因为它是从加密函数输出的),并存储在普通字符数组中with length = strlen(cipher)
//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
感谢任何帮助。很抱歉给您带来不便,并提前多多感谢。
答案 0 :(得分:1)
检查您的编码。
答案 1 :(得分:1)
您需要取消终止plain
,即需要plain[ strlen(dkey_buf) ] = 0
之类的内容。
答案 2 :(得分:1)
我有预感这是一个肮脏的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);
我的猜测只有在第一次正确解密后才能正确解析。
strlen(inputz)的另一个大问题是,如果strlen()没有完全落在8字节边界上,那么解密最终会失败。我已将此问题完全解决为gist on github。
答案 3 :(得分:0)
使用/分发利用IBE算法的程序是否有专利限制?