Skip to content

Commit

Permalink
Integrated the new libraries for AES256-CBC file decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdcvlsc committed Jul 17, 2023
1 parent c90d8b1 commit a8c12ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/BytePadding
42 changes: 27 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ int main(int argc, char *args[]) {
bconst::bytestream loadKey = keygen::readKey(args[KEY]);
keygen::AES_KEYCHECK(loadKey, AES_KEY_SIZE);

Krypt::Mode::CBC<Krypt::BlockCipher::AES, Krypt::Padding::PKCS_5_7> aes_scheme(loadKey.data(), loadKey.size());
Cipher::Aes<256> aes_cipher(loadKey.data());

auto start = timing_start();
std::atomic<size_t> cnt(0);
Expand Down Expand Up @@ -494,10 +494,13 @@ int main(int argc, char *args[]) {

if (lastblock_remains) {
if (read_buffer_size) {
aes_scheme.blockDecrypt(
reinterpret_cast<unsigned char *>(last_block),
reinterpret_cast<unsigned char *>(decrypted_block_holder), iv
Mode::CBC<AES_BLOCKSIZE>::decrypt(
reinterpret_cast<unsigned char *>(last_block), iv,
[&aes_cipher](unsigned char* block) {
aes_cipher.decrypt_block(block);
}
);

output_file.write(reinterpret_cast<char *>(decrypted_block_holder), AES_BLOCKSIZE);
lastblock_remains = false;
} else {
Expand All @@ -508,23 +511,25 @@ int main(int argc, char *args[]) {
if (read_buffer_size == BUFFER_BYTESIZE) {
size_t index;
for (index = 0; index < BUFFER_SIZE_NOLAST; index += AES_BLOCKSIZE) {
aes_scheme.blockDecrypt(
reinterpret_cast<unsigned char *>(read_buffer + index),
reinterpret_cast<unsigned char *>(decrypted_block_holder), iv
Mode::CBC<AES_BLOCKSIZE>::decrypt(
reinterpret_cast<unsigned char *>(read_buffer + index), iv,
[&aes_cipher](unsigned char* block) {
aes_cipher.decrypt_block(block);
}
);
std::memcpy(read_buffer + index, decrypted_block_holder, AES_BLOCKSIZE);
}

output_file.write(reinterpret_cast<char *>(read_buffer), BUFFER_SIZE_NOLAST);
std::memcpy(last_block, read_buffer + index, AES_BLOCKSIZE);
} else if (read_buffer_size % AES_BLOCKSIZE == 0) {
size_t index;
for (index = 0; index < read_buffer_size - AES_BLOCKSIZE; index += AES_BLOCKSIZE) {
aes_scheme.blockDecrypt(
reinterpret_cast<unsigned char *>(read_buffer + index),
reinterpret_cast<unsigned char *>(decrypted_block_holder), iv
Mode::CBC<AES_BLOCKSIZE>::decrypt(
reinterpret_cast<unsigned char *>(read_buffer + index), iv,
[&aes_cipher](unsigned char* block) {
aes_cipher.decrypt_block(block);
}
);
std::memcpy(read_buffer + index, decrypted_block_holder, AES_BLOCKSIZE);
}

output_file.write(reinterpret_cast<char *>(read_buffer), read_buffer_size - AES_BLOCKSIZE);
Expand All @@ -535,10 +540,17 @@ int main(int argc, char *args[]) {
}

if (lastblock_remains) {
Krypt::ByteArray recover =
aes_scheme.decrypt(reinterpret_cast<unsigned char *>(last_block), AES_BLOCKSIZE, iv);
Mode::CBC<AES_BLOCKSIZE>::decrypt(
reinterpret_cast<unsigned char *>(last_block), iv,
[&aes_cipher](unsigned char* block) {
aes_cipher.decrypt_block(block);
}
);

if (last_block[AES_BLOCKSIZE - 1] < 0x10) {
output_file.write(reinterpret_cast<char *>(last_block), AES_BLOCKSIZE - last_block[AES_BLOCKSIZE - 1]);
}

output_file.write(reinterpret_cast<char *>(recover.array), recover.length);
lastblock_remains = false;
}

Expand Down

0 comments on commit a8c12ac

Please sign in to comment.