Sizeof array auto changed when passed into function in Visual C++ -


i using library crypto++ encrypting/decrypting data. official page https://www.cryptopp.com. following this tutorial. shows how use block cipher crypto++. can see part find keyword "using block cipher".

i can run demo smoothly. encrypt data using key, decrypt data using same key. want split code encrypt() , decrypt() function. can see encrypt() function below.
include part:

#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" 


code body:

// 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 3 lines tutorial's code encrypt   byte cbciphertext[aes::blocksize];   encryptor.processdata(cbciphertext, plaintext, sizeof(plaintext));   ecb_mode<aes>::decryption decryptor(key, sizeof(key));    // next 2 lines code call encrypt() function, "cloned"   // code   // above 3 line!. comment out them have code   // 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 fools use   //reason";    cout << endl << "recovered text: " << cbrecoveredtext << endl;   getchar(); } 

the key created value \x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1. in demo code, key's value never changed , size 16. when call encrypt() function , pass key it, key size (sizeof(key)) 16 when created, after passed function, length 4 (!). , key value x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1ĂŒĂŒĂŒĂŒĂŒĂŒĂŒĂŒhello world (!!!). therefore, code gets error "aes: 4 not valid key length" if jump function. don't understand why happened , how fix it. appreciated!

top-level arrays in function prototypes nothing more hints programmer, if that.

the following prototypes same

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

in other words, sizeof(x), you're measuring size of pointer.

you can avoid using std::array instead (but you'll want avoid passing value).

if absolutely need work c-like api, need pass number of elements in array separate parameter. there's no standard way of getting pointer.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -