2013-07-09 4 views
2

Я пытаюсь расшифровать данные с использованием AES CBC и CTR. Зашифрованный текст был добавлен с 16-байтом IV.AES CBC расшифровывает работы, CTR не

У меня есть данные зашифрованного в следующем формате:

vector<vector<byte>> CBCMessages; 
vector<vector<byte>> CBCKeys; 
vector<vector<byte>> CTRMessages; 
vector<vector<byte>> CTRKeys; 

Я использую Crypto ++ для расшифровки данных. Это мой код:

for (int i = 0; i < CBCMessages.size(); i++) 
{ 
    std::string decryptedtext; 

    // split IV from ciphertext 
    byte iv[16]; 
    std::copy(CBCMessages[i].begin(), CBCMessages[i].begin()+16, iv); 
    CBCMessages[i].erase(CBCMessages[i].begin(), CBCMessages[i].begin()+16); 

    // this block works fine 
    AES::Decryption aesDecryption(&(CBCKeys[i][0]), CBCKeys[i].size()); 
    CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv); 
    StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext)); 
    stfDecryptor.Put(reinterpret_cast<const unsigned char*>(&(CBCMessages[i][0])), CBCMessages[i].size()); 
    stfDecryptor.MessageEnd(); 

    std::cout << decryptedtext << std::endl; 
} 

for (int i = 0; i < CTRMessages.size(); i++) 
{ 
    std::string decryptedtext; 

    // split IV from ciphertext 
    byte iv[16]; 
    std::copy(CTRMessages[i].begin(), CTRMessages[i].begin()+16, iv); 
    CTRMessages[i].erase(CTRMessages[i].begin(), CTRMessages[i].begin()+16); 

    // this block produces junk 
    AES::Decryption aesDecryption(&(CTRKeys[i][0]), CTRKeys[i].size()); 
    CTR_Mode_ExternalCipher::Decryption ctrDecryption(aesDecryption, iv); 
    StreamTransformationFilter stfDecryptor(ctrDecryption, new CryptoPP::StringSink(decryptedtext)); 
    stfDecryptor.Put(reinterpret_cast<const unsigned char*>(&(CTRMessages[i][0])), CTRMessages[i].size()); 
    stfDecryptor.MessageEnd(); 

    std::cout << decryptedtext << std::endl; 

    // try again with different method - this works fine 
    decryptedtext.clear(); 
    CTR_Mode<AES>::Decryption d; 
    d.SetKeyWithIV(&(CTRKeys[i][0]), CTRKeys[i].size(), iv, 16); 
    StringSource(reinterpret_cast<const unsigned char*>(&(CTRMessages[i][0])), CTRMessages[i].size(), true, 
     new StreamTransformationFilter(d, 
      new StringSink(decryptedtext) 
     ) 
    ); 

    std::cout << decryptedtext << std::endl; 
} 

Как вы можете видеть, средний блок (первый блок для дешифрования CTR) производит вывод вредного. Обратите внимание, что этот блок должен быть фактически идентичен блоку, используемому для дешифрования CBC.

Блок, используемый для дешифрования CBC, в основном копируется с this FAQ entry (ответ на 2005-окт-21 10:38 утра jeffrey). Затем я изменил этот блок, чтобы использовать его для дешифрования CTR, когда он не работал. Второй блок CTR вдохновлен разделом «Пример программы» here.

Какая проблема в первом блоке кода CTR?

+0

Coursera класс крипто, ха? – c00000fd

ответ

2

Это, вероятно, потому, что

AES :: aesDecryption расшифровки (& (CTRKeys [I] [0]), CTRKeys [I] .size());

https://upload.wikimedia.org/wikipedia/commons/3/3c/CTR_decryption_2.svg

CTR режим необходим AES :: Шифрование для расшифровки ciphertest

https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

+0

Спасибо, что решил! Я просто заменил «AES :: Decryption» на «AES :: Encryption», и он работает как шарм! – Chris

Смежные вопросы