1

Boneh-Franklin IdentityBasedEncryptionを実装するプログラムを書いています。実際の暗号化方法は、( 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から読み取ったメッセージとして変更しました

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

どんな助けでも大歓迎です。ご不便をおかけして申し訳ございませんが、よろしくお願いいたします。

4

4 に答える 4

1

エンコーディングを確認してください。

于 2009-04-16T12:56:38.180 に答える
1

null終了する必要があります。plainつまり、次のようなものplain[ strlen(dkey_buf) ] = 0が必要です。

于 2009-04-16T12:59:59.170 に答える
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)のもう1つの大きな問題は、strlen()が8バイトの境界に正確に当てはまらない場合、復号化が最終的に失敗することです。githubの要点として、この問題にかなり完全に対処しました。

于 2009-04-16T13:37:59.030 に答える
0

IBEアルゴリズムを活用するプログラムの使用/配布に特許の制限はありませんか?

于 2009-04-17T13:40:00.910 に答える