2016-11-11 11 views
0

データの暗号化/復号化にライブラリCrypto++を使用しています。公式ページはhttps://www.cryptopp.comです。私はthis tutorialに従っています。 Crypto++でブロック暗号を使用する方法を示します。この部分はfind keyword "using block cipher"で見ることができます。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になります。関数を呼び出して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"というエラーが表示されます。しかし、 'encrypt'関数のどこでも' size'( 'sizeof(key)'として計算されます)を渡すことは決してありません。あなたはどうしたらそのエラーを得ることができましたか? – AnT

+0

最初の#includeを変更してください。それは私が読書を続けるのを止める。 – LeDYoM

答えて

3

関数プロトタイプの最上位の配列は、プログラマーのヒント以上のものではありません。

次のプロトタイプは、言い換えれば、まったく同じ

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

あり、sizeof(x)で、あなたはポインタのサイズを測定しています。

代わりにstd::arrayを使用することで回避できます(値渡しは避けてください)。

CのようなAPIを使用する必要がある場合は、配列の要素数を別のパラメータとして渡す必要があります。それをポインタから取得する標準的な方法はありません。

+0

APIからProcessData()関数を使用しているので、Encryptor.ProcessData(cbCipherText、plainText、sizeof(plainText))にバイト*またはバイト[]を渡す必要があります。このケースでは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); 

... 
    } 

をそして今、その働きました!

関連する問題