0

そのため、cbc_crypt を使用して、さまざまな文字列の暗号化と復号化を試みています。これまでの私のプログラムは次のとおりです。

int main(int argc, char *argv[])
{
  unsigned char key[8] = {0x00, 0x00, 0x00, 0x68, 0x67, 0x01, 0x75, 0x19};
  unsigned char iv[8] = {0xe0, 0x3d, 0x36, 0x0c, 0x55, 0xfc, 0xe4, 0x0f};
  unsigned char ciphertext[96] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x3\
c, 0xa6, 0x05, 0x93, 0x4d, 0x4f, 0x4c, 0x80, 0x19, 0x3f, 0xb5, 0xa8, 0xd6, 0x03\
, 0xec, 0xf9, 0xbc, 0xb0, 0xea, 0x36, 0xe9, 0x8f, 0x3d, 0x94, 0x95, 0x02, 0xa8,\
 0xc0, 0x48, 0x23, 0xba, 0x51, 0xab, 0x65, 0x7f, 0xc2, 0xb8, 0xff, 0xfc, 0x3e, \
0x40, 0xdd, 0xfc, 0xd3, 0xe9, 0x32, 0x43, 0xd2, 0xf2, 0x37, 0x47, 0xab, 0x0d, 0\
x30, 0xba, 0xd0, 0x9e, 0x88, 0x66, 0x2b, 0x22, 0xdf, 0xed, 0x06, 0x76, 0xdb, 0x\
3a, 0xd5, 0x79, 0x7e, 0x22, 0x28, 0xcf, 0x3c, 0x5e, 0xbb, 0xc5, 0x85, 0xa0, 0x3\
f, 0x21, 0xab, 0xa8, 0xbe, 0x74, 0x37, 0xae, 0x59, 0xe9};



  unsigned char short_cipher[8] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x\
3c};

  unsigned char text[8] = {0x12, 0x34, 0x56, 0xab, 0xba, 0xfe, 0xfe, 0xfe};

  printf("Text before is %u \n", text);
  // cbc_crypt(key, text, 8, 1, iv);                                            
  printf("Text after is %u \n", text);

  return;
}

cbc_crypt はコメントアウトされていることに注意してください。これは、cbc_crypt をコメントアウトして実行するとどうなるかです。

[dorset:usabledir] 247) gcc guessdes.c
[dorset:usabledir] 248) ./a.out
Text before is 73623220 
Text after is 73623220 

[dorset:usabledir] 249) ./a.out
Text before is 9cf73030 
Text after is 9cf73030 

[dorset:usabledir] 249) ./a.out
Text before is f46e1bc0 
Text after is f46e1bc0 

[dorset:usabledir] 249) ./a.out
Text before is 674ed540 
Text after is 674ed540 

印刷するたびにテキストが変わる理由がわかりません。暗号化を実行しようとさえしていません。初期化した unsigned char を出力しただけです。どんな助けでも大歓迎です。

編集:どうやら私は %s を使用する必要があります。これを行うと、一貫して印刷されるようになりました。

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

[dorset:usabledir] 256) ./a.out
Text before is 4V???????%< 
Text after is 4V???????%< 

以前にアドレスを印刷していた理由を誰か説明できますか? また、16進数で印刷する方法はありますか?(4Vとすべての疑問符がどこから来ているのか本当にわかりません)。

編集2:わかりましたので、スコットが提案したように、私は今このように印刷しています

  for(i = 0; i < 8; i++){
    printf("%u", text[i]);
  }

次に、テキストを 185286171186254254254 として出力します。

それが元の 16 進コード {0x12, 0x34, 0x56, 0xab, 0xba, 0xfe, 0xfe, 0xfe} とどのように一致するかを理解しようとしています。

編集: %u の代わりに %x を使用する必要があることに気付きました。123456abbafefefe が正しく出力されるようになりました。みんなありがとう。

4

2 に答える 2

1

次のステートメントで配列「テキスト」のアドレスを出力しようとしています

printf("Text before is %u \n", text);

プログラムを実行するたびに、異なるアドレスにロードされるため、毎回異なる値が得られます。

アドレステキストに文字列を出力したい場合は、次を使用できます

printf("Text before is %s \n", text);

ただし、文字列が NULL で終了していないことに注意してください。8 文字の後にジャンク データが出力される可能性があります。

値を符号なし整数として出力したい場合は、使用できます

printf("Text before is %u \n", (unsigned int)*text);
于 2014-01-16T04:40:27.473 に答える