1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
void TestCryptage2()
{
char* data = "test avec un bloc de taille un peu plus important"; // à passer en entrée sous unsigned char*
const size_t size = strlen(data) + 1; // + 1 car là c'est du texte, à passer en entrée
std::string strPass = "pass";
BF_KEY* key = new BF_KEY;
memset(key, 0, sizeof(BF_KEY));
BF_set_key(key, strPass.size(), (unsigned char*)strPass.c_str());
const size_t blocksize = 8;
// 1er 8 octets devraient être la taille de la data entrante (puis ramené à %8 == 0)
char* crypted = new char[size + (blocksize - (size % blocksize))];
size_t index = 0;
while (index < size) {
unsigned char plaintext[blocksize + 1];
unsigned char cyphertext[blocksize + 1];
memset(plaintext, 0, blocksize);
memset(cyphertext, 0, blocksize);
size_t shunkSize = std::min(blocksize, (size - index));
memcpy(plaintext, data + index, shunkSize);
BF_ecb_encrypt(plaintext, cyphertext, key, BF_ENCRYPT);
memcpy(crypted + index, cyphertext, blocksize);
index += shunkSize;
}
std::cout << "encrypted = " << crypted;
// Lire les premiers 8 octets pour connaitre a taille attendu
char* decrypted = new char[size];
index = 0;
while (index < size) {
unsigned char plaintext[blocksize + 1];
unsigned char cyphertext[blocksize + 1];
memset(plaintext, 0, blocksize);
memset(cyphertext, 0, blocksize);
size_t shunkSize = std::min(blocksize, (size - index));
memcpy(cyphertext, crypted + index, blocksize);
BF_ecb_encrypt(cyphertext, plaintext, key, BF_DECRYPT);
memcpy(decrypted + index, plaintext, shunkSize);
index += shunkSize;
}
//BF_ecb_encrypt(cyphertext, plaintext, key, BF_DECRYPT);
std::cout << "encrypted = " << decrypted;
} |
Partager