aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/information-security-experiment
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/life/information-security-experiment')
-rw-r--r--store/works/life/information-security-experiment/.gitignore2
-rw-r--r--store/works/life/information-security-experiment/CMakeLists.txt19
-rw-r--r--store/works/life/information-security-experiment/des.cpp80
-rw-r--r--store/works/life/information-security-experiment/md5.cpp25
-rw-r--r--store/works/life/information-security-experiment/rsa.cpp65
5 files changed, 191 insertions, 0 deletions
diff --git a/store/works/life/information-security-experiment/.gitignore b/store/works/life/information-security-experiment/.gitignore
new file mode 100644
index 0000000..7194ea7
--- /dev/null
+++ b/store/works/life/information-security-experiment/.gitignore
@@ -0,0 +1,2 @@
+.cache
+build
diff --git a/store/works/life/information-security-experiment/CMakeLists.txt b/store/works/life/information-security-experiment/CMakeLists.txt
new file mode 100644
index 0000000..f44e2f1
--- /dev/null
+++ b/store/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/store/works/life/information-security-experiment/des.cpp b/store/works/life/information-security-experiment/des.cpp
new file mode 100644
index 0000000..93a6ea3
--- /dev/null
+++ b/store/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/store/works/life/information-security-experiment/md5.cpp b/store/works/life/information-security-experiment/md5.cpp
new file mode 100644
index 0000000..9714127
--- /dev/null
+++ b/store/works/life/information-security-experiment/md5.cpp
@@ -0,0 +1,25 @@
+#include <openssl/evp.h>
+
+#include <iomanip>
+#include <iostream>
+
+int main() {
+ EVP_MD_CTX *ctx = EVP_MD_CTX_create();
+ EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
+
+ EVP_DigestUpdate(ctx, "abc", 3);
+
+ unsigned char digest[EVP_MAX_MD_SIZE];
+ unsigned int digest_size;
+ EVP_DigestFinal_ex(ctx, digest, &digest_size);
+
+ EVP_MD_CTX_destroy(ctx);
+
+ std::cout << "MD5 digest of 'abc' is: " << std::hex << std::setfill('0');
+ for (unsigned int i = 0; i < digest_size; i++) {
+ std::cout << std::setw(2) << (int)digest[i];
+ }
+ std::cout << std::endl;
+
+ return 0;
+}
diff --git a/store/works/life/information-security-experiment/rsa.cpp b/store/works/life/information-security-experiment/rsa.cpp
new file mode 100644
index 0000000..5b88284
--- /dev/null
+++ b/store/works/life/information-security-experiment/rsa.cpp
@@ -0,0 +1,65 @@
+#include <openssl/evp.h>
+#include <openssl/rsa.h>
+
+#include <iomanip>
+#include <iostream>
+#include <vector>
+
+int main() {
+
+ auto ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
+ EVP_PKEY *pkey = NULL;
+ EVP_PKEY_keygen_init(ctx);
+ EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
+ EVP_PKEY_keygen(ctx, &pkey);
+ EVP_PKEY_CTX_free(ctx);
+ ctx = nullptr;
+
+ unsigned char cleartext[] = "abc";
+ int cleartext_size = sizeof(cleartext) / sizeof(cleartext[0]);
+
+ EVP_MD_CTX *md_ctx = EVP_MD_CTX_create();
+ EVP_DigestInit_ex(md_ctx, EVP_sha256(), NULL);
+ EVP_DigestUpdate(md_ctx, cleartext, cleartext_size);
+ unsigned char digest[EVP_MAX_MD_SIZE];
+ unsigned int digest_size;
+ EVP_DigestFinal_ex(md_ctx, digest, &digest_size);
+ EVP_MD_CTX_destroy(md_ctx);
+ md_ctx = nullptr;
+ std::cout << "SHA256 digest of 'abc' is: " << std::hex << std::setfill('0');
+ for (unsigned int i = 0; i < digest_size; i++) {
+ std::cout << std::setw(2) << (int)digest[i];
+ }
+ std::cout << std::endl;
+
+ std::vector<unsigned char> sig;
+ size_t sig_len;
+
+ ctx = EVP_PKEY_CTX_new(pkey, nullptr);
+ EVP_PKEY_sign_init(ctx);
+ EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
+ EVP_PKEY_sign(ctx, nullptr, &sig_len, digest, digest_size);
+ sig.resize(sig_len);
+ EVP_PKEY_sign(ctx, sig.data(), &sig_len, digest, digest_size);
+ EVP_PKEY_CTX_free(ctx);
+
+ std::cout << "Signature: " << std::hex << std::setfill('0');
+ for (auto c : sig) {
+ std::cout << std::setw(2) << static_cast<int>(c);
+ }
+ std::cout << std::endl;
+
+ ctx = EVP_PKEY_CTX_new(pkey, nullptr);
+ EVP_PKEY_verify_init(ctx);
+ EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
+ auto result = EVP_PKEY_verify(ctx, sig.data(), sig_len, digest, digest_size);
+ if (result == 1) {
+ std::cout << "Verification succeeded." << std::endl;
+ } else if (result == 0) {
+ std::cout << "Verification failed." << std::endl;
+ } else {
+ std::cerr << "Verification error." << std::endl;
+ }
+
+ return 0;
+} \ No newline at end of file