2016-11-11 2 views
0

데이터 암호화/해독에 라이브러리 Crypto++을 사용하고 있습니다. 공식 페이지는 https://www.cryptopp.com입니다. 나는 this tutorial을 팔로우하고 있습니다. 그것은 Crypto++으로 블록 암호를 사용하는 방법을 보여줍니다. 이 부분은 "블록 암호를 사용하여"라는 키워드로 찾을 수 있습니다.Visual C++에서 함수에 전달할 때 배열의 크기가 자동으로 변경됨

데모를 원활하게 실행할 수 있습니다. 그들은 키를 사용하여 데이터를 암호화 한 다음 동일한 키를 사용하여 데이터를 해독합니다. encrypt()decrypt() 코드로 코드를 분할하고 싶습니다. 아래에서 제 encrypt() 기능을 볼 수 있습니다.

#include "E:\Working\Improve\CPP\cryptopp565\osrng.h" 
using CryptoPP::AutoSeededRandomPool; 

#include <iostream> 
using std::cout; 
using std::cerr; 
using std::endl; 

#include <string> 
using std::string; 

#include <cstdlib> 
using std::exit; 

#include "E:\Working\Improve\CPP\cryptopp565\cryptlib.h" 
using CryptoPP::Exception; 

#include "E:\Working\Improve\CPP\cryptopp565\hex.h" 
using CryptoPP::HexEncoder; 
using CryptoPP::HexDecoder; 

#include "E:\Working\Improve\CPP\cryptopp565\filters.h" 
using CryptoPP::StringSink; 
using CryptoPP::StringSource; 
using CryptoPP::StreamTransformationFilter; 

#include "E:\Working\Improve\CPP\cryptopp565\aes.h" 
using CryptoPP::AES; 

#include "E:\Working\Improve\CPP\cryptopp565\ccm.h" 
#include "E:\Working\Improve\CPP\cryptopp565\modes.h" 
using CryptoPP::ECB_Mode; 
#include <fstream> 

#include "assert.h" 


코드 체 :

// My encrypt function 
void encrypt(byte cbCipherText[AES::BLOCKSIZE], byte *plainText, 
      byte key[AES::DEFAULT_KEYLENGTH], int sizeKey) { 
    int size = sizeof(key); 
    ECB_Mode<AES>::Encryption Encryptor(key, sizeKey); 

    Encryptor.ProcessData(cbCipherText, plainText, sizeof(plainText)); 
} 

void main() { 
    byte PlainText[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 
         'r', 'l', 'd', 0x0, 0x0, 0x0, 0x0, 0x0}; 

    byte key[AES::DEFAULT_KEYLENGTH]; 
    ::memset(key, 0x01, AES::DEFAULT_KEYLENGTH); 

    // Encrypt data 
    int size = sizeof(key); 
    int default = AES::DEFAULT_KEYLENGTH; 
    ECB_Mode<AES>::Encryption Encryptor(key, size); 

    // Next three lines are tutorial's code for encrypt 
    byte cbCipherText[AES::BLOCKSIZE]; 
    Encryptor.ProcessData(cbCipherText, PlainText, sizeof(PlainText)); 
    ECB_Mode<AES>::Decryption Decryptor(key, sizeof(key)); 

    // Next two lines are my code to call the encrypt() function, I "cloned" the 
    // code 
    // from above three line!. Comment out them we will have the code like the 
    // demo. 

    byte myCipherText[AES::BLOCKSIZE]; 
    encrypt(myCipherText, PlainText, key, size); 

    // Decrypt 
    byte cbRecoveredText[AES::BLOCKSIZE]; 

    Decryptor.ProcessData(cbRecoveredText, cbCipherText, sizeof(cbCipherText)); 

    // std::string PlainText ="Voltaire said, Prejudices are what fools use for 
    //reason"; 

    cout << endl << "Recovered text: " << cbRecoveredText << endl; 
    getchar(); 
} 

키가 \x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1 값으로 만든
부 포함된다. 데모 코드에서 키 값은 변경되지 않으며 크기는 항상 16입니다. 내 encrypt() 함수를 호출하고 key을 전달하면 키 크기 (sizeof(key))는 작성 당시에는 16이지만 함수에 전달 된 후, 길이는 항상 4입니다 (!). 그리고 핵심 가치는 x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1ĂŒĂŒĂŒĂŒĂŒĂŒĂŒĂŒHello World (!!!)입니다. 따라서 함수에 들어가면 내 코드가 항상 "AES: 4 is not valid key length"이됩니다. 왜 이런 일이 있었는지와 어떻게 고쳐야하는지 이해할 수 없습니다. 어떤 도움을 주시면 감사하겠습니다!

+0

"AES : 4 is not valid key length"오류가 발생했다고 주장합니다. 그러나,'size' ('sizeof (key) '로 계산)는'encrypt' 함수의 어느 곳에 나 전달하지 마십시오. 어떻게 그 오류를 가져올 수 있었습니까? – AnT

+0

첫 번째 #include를 변경하십시오. 그것은 계속해서 읽는 것을 멈추게합니다. – LeDYoM

답변

3

함수 프로토 타입의 최상위 배열은 프로그래머에게 힌트 일뿐입니다.

다음 프로토 타입 당신은 포인터의 크기를 측정하고, sizeof(x)와 정확히 같은

void foo(int x[20]); 
void foo(int x[]); 
void foo(int* x); 

있습니다.

대신 std::array을 사용하면이 문제를 피할 수 있지만 값으로 전달하지 않는 것이 좋습니다.

C와 유사한 API로 절대적으로 작업해야하는 경우 배열의 요소 수를 별도의 매개 변수로 전달해야합니다. 포인터에서 가져 오는 표준 방법은 없습니다.

+0

API에서 ProcessData() 함수를 사용하므로 Encryptor.ProcessData (cbCipherText, plainText, sizeof (plainText))에 byte * 또는 byte []를 전달해야합니다. 이 경우 std :: array를 사용할 수 없을 수도 있습니다 : \ – Andiana

+1

@Andiana 왜'ProcessData'는 크기를 다른 매개 변수로 전달해야합니까?왜 그들은'sizeof (data)'를 호출 할 수 없는가? – Slava

+0

@Andiana 그러면 함수에 다른 매개 변수를 추가해야합니다. – krzaq

1

@krzaq 코멘트 주셔서 감사합니다. 나는 내 문제를 해결했다. 문제 : 키의 크기와 plainText의 크기는 작동하기 위해 숫자로 전달되어야합니다. 포인터를 함수에 전달한 후 sizeof()를 사용하여 크기를 검색 할 수 없습니다.

나는 코드 수정 :

// My encrypt function 
void encrypt(byte cbCipherText[AES::BLOCKSIZE], byte *plainText, 
      byte key[AES::DEFAULT_KEYLENGTH], int sizeKey, int sizeKey) { 
    int size = sizeof(key); 
    ECB_Mode<AES>::Encryption Encryptor(key, sizeKey); 

    Encryptor.ProcessData(cbCipherText, plainText, textKey); 
} 
... 
void main() { 
... 
    int sizeText = sizeOf(plainText); 
    encrypt(myCipherText, PlainText, key, sizeKey, sizeText); 

... 
    } 

을 그리고 지금은 일!