0

AES プログラムを使用しており、暗号化された形式の出力は bytearray です。与えられた一本の紐から

e37c1a5132a9a121d4fbb98ba42a684

ただし、16進配列は

e3 7c 1a 51 32 a9 a1 21 d4 fb b9 8b 0a 42 a6 84

最後の項の 4 番目は 0a ですが、連結された文字列では単なる a として表示されます。どうすればこれを検出できますか?

以下の添付リンクでは、最初に生成されたリンクからバイト配列に移動しようとしていますが、0a に 0 がない場合、文字列がクラッシュします。

http://laurentcharignon.com/blog/?p=37

4

2 に答える 2

2

問題は、ループ内で、関数の結果が16 未満の値hex()の接頭辞の後に先頭のゼロを含まないことです。したがって、代わりに:'0x'hex(10)'0xa'hex(10)[2:]'a'str()

result = ""
for char in ciph:
    result += str((hex(ord(char))))[2:]

hex()次のようなものでの使用を避けることができます。

result = ""
for char in ciph:
    result += '%02x' % ord(char)

同じことを行うもう少し簡潔な方法は、ジェネレーター式を使用することです。

result = ''.join('%02x' % ord(char) for char in ciph)

結果を読みやすくするために、次のわずかに異なる式を使用して、各 2 バイトの 16 進数値の間にスペースを追加できます。

       ' '.join('%02x' % ord(char) for char in ciph)
于 2013-09-22T09:15:53.507 に答える
2

This kind of missing information is not recoverable. You have to fix the place that generates this broken string. Another valid source string for producing your string above would be:

e3 7c 1a 51 32 a9 a1 21 d4 fb b9 8b a4 02 a6 84

Here the 0 is inserted one place later. On a technical level it is not possible to infer which of these source strings was used to produce the above hex representation with zeros dropped.

于 2013-09-22T07:04:44.230 に答える