diff options
author | crupest <crupest@outlook.com> | 2021-12-09 22:03:01 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-12-09 22:03:01 +0800 |
commit | 82eb15120c635c6b10e3fb0c6916fa1e49a3b387 (patch) | |
tree | 6ffb0839fd7ee7a2c078c9800070d467b242fb7c /works/life | |
parent | 7fffbcb5ed573ecbc5ecf3f7b5c18d4f37d94fc6 (diff) | |
download | crupest-82eb15120c635c6b10e3fb0c6916fa1e49a3b387.tar.gz crupest-82eb15120c635c6b10e3fb0c6916fa1e49a3b387.tar.bz2 crupest-82eb15120c635c6b10e3fb0c6916fa1e49a3b387.zip |
import(life): ...
Diffstat (limited to 'works/life')
5 files changed, 101 insertions, 0 deletions
diff --git a/works/life/information-security-experiment/.gitignore b/works/life/information-security-experiment/.gitignore new file mode 100644 index 0000000..7194ea7 --- /dev/null +++ b/works/life/information-security-experiment/.gitignore @@ -0,0 +1,2 @@ +.cache +build diff --git a/works/life/information-security-experiment/CMakeLists.txt b/works/life/information-security-experiment/CMakeLists.txt new file mode 100644 index 0000000..f44e2f1 --- /dev/null +++ b/works/life/information-security-experiment/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.20) + +set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake + CACHE STRING "Vcpkg toolchain file") + +project(information-security-experiment) + +set(CMAKE_CXX_STANDARD 20) + +find_package(OpenSSL REQUIRED) + +add_executable(des des.cpp) +target_link_libraries(des PRIVATE OpenSSL::Crypto) + +add_executable(md5 md5.cpp) +target_link_libraries(md5 PRIVATE OpenSSL::Crypto) + +add_executable(rsa rsa.cpp) +target_link_libraries(rsa PRIVATE OpenSSL::Crypto) diff --git a/works/life/information-security-experiment/des.cpp b/works/life/information-security-experiment/des.cpp new file mode 100644 index 0000000..93a6ea3 --- /dev/null +++ b/works/life/information-security-experiment/des.cpp @@ -0,0 +1,80 @@ +#include <openssl/des.h> +#include <openssl/evp.h> + +#include <cstdlib> +#include <iomanip> +#include <iostream> + +void PrintCblock(const DES_cblock &b, bool end_line = true) { + std::cout << std::hex << std::right << std::setfill('0'); + + for (auto c : b) { + std::cout << std::setw(2) << static_cast<int>(c); + } + + if (end_line) { + std::cout << std::endl; + } +} + +int main() { + auto cipher = EVP_des_cfb(); + + std::cout << "Selected cipher: des_cfb" << std::endl; + + auto key_length = EVP_CIPHER_key_length(cipher); + std::cout << "Key length: " << key_length << std::endl; + + auto iv_length = EVP_CIPHER_iv_length(cipher); + std::cout << "IV length: " << iv_length << std::endl; + + auto block_length = EVP_CIPHER_block_size(cipher); + std::cout << "Block length: " << block_length << std::endl; + + DES_cblock key; + + std::srand(std::time(nullptr)); + + std::cout << std::hex << std::setfill('0'); + + if (DES_random_key(&key) == 0) { + std::cerr << "Failed to generate key." << std::endl; + return 1; + } else { + std::cout << "Succeeded to generate key." << std::endl; + std::cout << "Key: "; + PrintCblock(key); + } + + DES_key_schedule key_schedule; + + { + auto result = DES_set_key_checked(&key, &key_schedule); + + if (result == -1) { + std::cerr << "Parity is wrong." << std::endl; + return 1; + } else if (result == -2) { + std::cerr << "Key is too weak." << std::endl; + } + } + + DES_cblock clear_text = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + + std::cout << "Clear text: " << std::hex; + PrintCblock(clear_text); + + DES_cblock cipher_text; + + DES_ecb_encrypt(&clear_text, &cipher_text, &key_schedule, 1); + + std::cout << "Cipher text after encrypted: " << std::hex; + PrintCblock(cipher_text); + + DES_ecb_encrypt(&cipher_text, &clear_text, &key_schedule, 0); + + std::cout << "Clear text after decrypted: " << std::hex; + PrintCblock(clear_text); + + return 0; +} diff --git a/works/life/information-security-experiment/md5.cpp b/works/life/information-security-experiment/md5.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/works/life/information-security-experiment/md5.cpp diff --git a/works/life/information-security-experiment/rsa.cpp b/works/life/information-security-experiment/rsa.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/works/life/information-security-experiment/rsa.cpp |