방법 CTR 모드에서 추구 스트림의 일부를 해독?
Crypto++ Pipeline을 사용하면 Discard
or Skip
on a Source
does not work as expected이기 때문에 조금 어색하다. 현재 구현중인 데이터를 "nothing"에 입력해야합니다. 스택 오버플로에 대해서도 Skip'ing on a Source does not work as expected을 참조하십시오.
다음은 AES/CTR을 사용하고 스트림에서 검색하는 예입니다. 그것은 "두 부분"추구를 수행해야합니다. 먼저 Source
에서 cipher
이라는 바이트를 삭제합니다. 둘째, 카운터를 동기화하기 위해 enc
이라는 암호화 개체의 키 스트림을 찾습니다. 탐색이 수행되면 암호 텍스트의 나머지 부분은 PumpAll()
을 호출하여 암호 해독됩니다.이 암호는 파이프 라인을 통해 나머지 데이터를 펌프합니다. 여기에, 일반적인 패턴을 볼 수있는 범위 [5,10]
를 사용하여 데이터 세트에 대한 수정이다으니
$ ./test.exe
0: Now is the time for all good men to come to the aide of their country
1: ow is the time for all good men to come to the aide of their country
2: w is the time for all good men to come to the aide of their country
3: is the time for all good men to come to the aide of their country
4: is the time for all good men to come to the aide of their country
5: s the time for all good men to come to the aide of their country
6: the time for all good men to come to the aide of their country
7: the time for all good men to come to the aide of their country
8: he time for all good men to come to the aide of their country
9: e time for all good men to come to the aide of their country
10: time for all good men to come to the aide of their country
11: time for all good men to come to the aide of their country
12: ime for all good men to come to the aide of their country
13: me for all good men to come to the aide of their country
14: e for all good men to come to the aide of their country
15: for all good men to come to the aide of their country
16: for all good men to come to the aide of their country
17: or all good men to come to the aide of their country
18: r all good men to come to the aide of their country
19: all good men to come to the aide of their country
20: all good men to come to the aide of their country
21: ll good men to come to the aide of their country
22: l good men to come to the aide of their country
23: good men to come to the aide of their country
24: good men to come to the aide of their country
25: ood men to come to the aide of their country
26: od men to come to the aide of their country
27: d men to come to the aide of their country
28: men to come to the aide of their country
29: men to come to the aide of their country
30: en to come to the aide of their country
31: n to come to the aide of their country
32: to come to the aide of their country
33: to come to the aide of their country
34: o come to the aide of their country
35: come to the aide of their country
36: come to the aide of their country
37: ome to the aide of their country
38: me to the aide of their country
39: e to the aide of their country
40: to the aide of their country
41: to the aide of their country
42: o the aide of their country
43: the aide of their country
44: the aide of their country
45: he aide of their country
46: e aide of their country
47: aide of their country
48: aide of their country
49: ide of their country
50: de of their country
51: e of their country
52: of their country
53: of their country
54: f their country
55: their country
56: their country
57: heir country
58: eir country
59: ir country
60: r country
61: country
62: country
63: ountry
64: untry
65: ntry
66: try
67: ry
68: y
: 여기
#include "modes.h"
#include "aes.h"
using namespace CryptoPP;
int main(int argc, char* argv[])
{
string plain = "Now is the time for all good men to come to the aide of their country";
byte key[AES::DEFAULT_KEYLENGTH] = {0};
byte nonce[AES::BLOCKSIZE] = {0};
CTR_Mode<AES>::Encryption enc;
enc.SetKeyWithIV(key, sizeof(key), nonce, sizeof(nonce));
string cipher;
StringSource ss1(plain, true, new StreamTransformationFilter(enc, new StringSink(cipher)));
for(size_t i=0; i<cipher.size(); i++)
{
CTR_Mode<AES>::Decryption dec;
dec.SetKeyWithIV(key, sizeof(key), nonce, sizeof(nonce));
StringSource ss2(cipher, false);
ss2.Pump(i);
dec.Seek(i);
string recover;
StreamTransformationFilter stf(dec, new StringSink(recover));
// Attach the decryption filter after seeking
ss2.Attach(new Redirector(stf));
ss2.PumpAll();
cout << i << ": " << recover << endl;
}
return 0;
}
는 결과이다.
아니요은 stf.MessageEnd()
으로 전화해야합니다. 복구 된 텍스트가 XOR이 수행되는 즉시 준비되기 때문입니다. 다른 모드에서는 MessageEnd()
으로 전화해야 할 수도 있습니다. Crypto ++ wiki의 Init-Update-Final도 참조하십시오.
$ ./test.exe
's the '
을 그리고 여기에 좀 더 :
StringSource ss2(cipher, false);
ss2.Pump(5);
dec.Seek(5);
string recover;
StreamTransformationFilter stf(dec, new StringSink(recover));
// Attach the decryption filter after seeking
ss2.Attach(new Redirector(stf));
ss2.Pump(10 - 5 + 1);
cout << "'" << recover << "'" << endl;
이 생산
StringSource ss2(cipher, false);
ss2.Pump(5);
dec.Seek(5);
string recover;
StreamTransformationFilter stf(dec, new StringSink(recover));
// Attach the decryption filter after seeking
ss2.Attach(new Redirector(stf));
ss2.Pump(10 - 5 + 1);
cout << "'" << recover << "'" << endl;
ss2.Pump(1);
cout << "'" << recover << "'" << endl;
ss2.Pump(1);
cout << "'" << recover << "'" << endl;
이 생성됩니다
$ ./test.exe
's the '
's the t'
's the ti'
이전에 나는 라고 말했고 "Crypto++ Pipeline을 사용하는 것은 약간 어색하다"입니다.여기에 우리가 원하는 모든하지만 우리는 순간에 할 수 없습니다
StringSource ss(cipher, false, new StreamTransformationFilter(dec, new StringSink(x)));
ss.Skip(5); // Discard bytes and synchronize stream
ss.Pump(5); // Process bytes [5,10]
cout << x << endl;
롭의 코멘트 "당신은 전체 16 바이트 블록을 해제해야합니다 ..."에 대해서 - 당신이 작업하는 경우 CBC 모드와 같은 다른 모드를 사용하면 이전 일반 텍스트 또는 암호 텍스트를 처리해야합니다. 블록을 조작해야합니다. CBC 모드와 연쇄 속성은이를 요구합니다.
그러나 CTR은 조금 다르게 설계되었습니다. 탐색 가능하도록 설계되어있어 스트림에서 뛰어 넘을 수 있습니다. 이 점에서 OFB 모드와 비슷합니다. (CTR 모드와 OFB 모드는 키 스트림을 생성하는 방식이 다르지만 일반 텍스트 또는 암호 텍스트로 키 스트림을 XOR합니다).
+10 여기에는 의미가 없습니다. 블록 모드에 상관없이 전체 16 바이트 블록의 암호를 해독해야합니다. CTR로 가능하지만 다소 복잡하고 Crypto ++가 직접 지원하는지 기억할 수 없으므로이를 달성하는 데 필요한 중요한 코드가있을 수 있습니다.) 작은 오프셋의 경우 이점이 없습니다. 그냥 첫 번째 N 바이트를 버리십시오. –
@jww 이것은 여전히 16 바이트를 해독해야합니다. AES는 항상 16 바이트를 암호화/해독합니다. 따라서 바이트 10의 암호를 해독하는 경우 바이트 0-9의 암호도 해독해야합니다. 데이터가 아니라 키가 변경 되어도 변경되지 않습니다. 당신 자신이 이것을 메모합니다 : "16을 찾아야합니다." 따라서 바이트 10의 암호를 해독하는 것은 정상적으로 암호를 해독하고 출력의 0-9 바이트를 버리는 것과 동일합니다 (이는 간단합니다). 당신이 (완전히 동의 함) 말하고 CTR이 허용하는 것처럼 암호 텍스트의 적절한 양을 버리는 것이 가능합니다. 라이브러리가 그것을 위해 설계되지 않은 한 * 코드 *에서 복잡합니다. Crypto ++는 그렇지 않습니다. –
확인. 너의 의도를 알 겠어. –