diff --git a/.gitignore b/.gitignore index 259148f..c0f8bfc 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app + +# Others +experiment/dum/* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..68f3ff3 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +experiment-build-a: + g++ -o bin/exp-a.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/a.cpp `libpng-config --ldflags` +experiment-build-b: + g++ -o bin/exp-b.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/b.cpp `libpng-config --ldflags` +experiment-build-c: + g++ -o bin/exp-c.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/c.cpp `libpng-config --ldflags` +experiment-build-d: + g++ -o bin/exp-d.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/d.cpp `libpng-config --ldflags` +experiment-build-e: + g++ -o bin/exp-e.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/e.cpp `libpng-config --ldflags` +experiment-build-f: + g++ -o bin/exp-f.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/f.cpp `libpng-config --ldflags` +experiment-build-all: + g++ -o bin/exp-a.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/a.cpp `libpng-config --ldflags` + g++ -o bin/exp-b.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/b.cpp `libpng-config --ldflags` + g++ -o bin/exp-c.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/c.cpp `libpng-config --ldflags` + g++ -o bin/exp-d.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/d.cpp `libpng-config --ldflags` + g++ -o bin/exp-e.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/e.cpp `libpng-config --ldflags` + g++ -o bin/exp-f.o -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/f.cpp `libpng-config --ldflags` +experiment-run-all: + ./bin/exp-a.o + ./bin/exp-b.o + ./bin/exp-c.o + ./bin/exp-d.o + ./bin/exp-e.o + ./bin/exp-f.o +experiment-run-a: + ./bin/exp-a.o +experiment-run-b: + ./bin/exp-b.o +experiment-run-c: + ./bin/exp-c.o +experiment-run-d: + ./bin/exp-d.o +experiment-run-e: + ./bin/exp-e.o +experiment-run-f: + ./bin/exp-f.o diff --git a/README.md b/README.md index bfb54a6..512b619 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ ## System Block Diagram ![System Block Diagram](data/system-block-diagram.png) + +## Deprecated System Block Diagram [[1]](https://www.researchgate.net/publication/320087595_File_To_Image_Encryption_FTIE_Menggunakan_Algoritma_Randomized_Text_Dan_Arnold_Cat_Map_ACM_Untuk_Keamanan_Transmisi_Data_Digital) +![Deprecated System Block Diagram](data/system-block-diagram-deprecated.png) diff --git a/data/system-block-diagram-deprecated.png b/data/system-block-diagram-deprecated.png new file mode 100644 index 0000000..af700ba Binary files /dev/null and b/data/system-block-diagram-deprecated.png differ diff --git a/experiment/a.cpp b/experiment/a.cpp new file mode 100644 index 0000000..db841cf --- /dev/null +++ b/experiment/a.cpp @@ -0,0 +1,131 @@ +// g++ -o bin/exp-a -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/a.cpp `libpng-config --ldflags` && ./bin/exp-a +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "acm_equal_n"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // plainfile + std::vector bytes(3210000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + // keystream + std::vector keystream(10000000); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // bbs + uint8_t bbs = generateRandom() % 30; + uint16_t p = P[bbs]; + uint16_t q = Q[bbs]; + uint32_t s = S[bbs]; + + // acm + uint16_t a = generateRandom(); + uint16_t b = a; + + uint16_t inc = 10; + uint16_t max = 300; + uint16_t init = 10; + for (uint16_t n = init; n <= max; n += inc) { + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + std::cout << n << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/b.cpp b/experiment/b.cpp new file mode 100644 index 0000000..1232863 --- /dev/null +++ b/experiment/b.cpp @@ -0,0 +1,134 @@ +// g++ -o bin/exp-b -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/b.cpp `libpng-config --ldflags` && ./bin/exp-b +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "acm_any_n"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // plainfile + std::vector bytes(3210000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + // keystream + std::vector keystream(10000000); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // bbs + uint8_t bbs = generateRandom() % 30; + uint16_t p = P[bbs]; + uint16_t q = Q[bbs]; + uint32_t s = S[bbs]; + + // acm + uint16_t a = generateRandom(); + uint16_t b = generateRandom(); + while (b == a) { + b = generateRandom(); + } + + uint16_t inc = 10; + uint16_t max = 300; + uint16_t init = 10; + for (uint16_t n = init; n <= max; n += inc) { + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + std::cout << n << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/c.cpp b/experiment/c.cpp new file mode 100644 index 0000000..a44b25f --- /dev/null +++ b/experiment/c.cpp @@ -0,0 +1,131 @@ +// g++ -o bin/exp-c -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/c.cpp `libpng-config --ldflags` && ./bin/exp-c +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "acm_equal_a"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // plainfile + std::vector bytes(3210000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + // keystream + std::vector keystream(10000000); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // bbs + uint8_t bbs = generateRandom() % 30; + uint16_t p = P[bbs]; + uint16_t q = Q[bbs]; + uint32_t s = S[bbs]; + + // acm + uint16_t n = 50; + + uint16_t init = 1; + uint16_t inc = UINT16_MAX / 30; + uint16_t max = UINT16_MAX - inc; + for (uint16_t a = init; a <= max; a += inc) { + uint16_t b = a; + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + std::cout << a << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/d.cpp b/experiment/d.cpp new file mode 100644 index 0000000..c96f548 --- /dev/null +++ b/experiment/d.cpp @@ -0,0 +1,131 @@ +// g++ -o bin/exp-d -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/d.cpp `libpng-config --ldflags` && ./bin/exp-d +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "acm_any_ab"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // plainfile + std::vector bytes(3210000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + // keystream + std::vector keystream(10000000); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // bbs + uint8_t bbs = generateRandom() % 30; + uint16_t p = P[bbs]; + uint16_t q = Q[bbs]; + uint32_t s = S[bbs]; + + // acm + uint16_t n = 50; + + uint16_t init = 1; + uint16_t inc = UINT16_MAX / 30; + uint16_t max = UINT16_MAX - inc; + for (uint16_t a = init; a <= max; a += inc) { + uint16_t b = a + 1; + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + std::cout << a << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/e.cpp b/experiment/e.cpp new file mode 100644 index 0000000..83d6302 --- /dev/null +++ b/experiment/e.cpp @@ -0,0 +1,130 @@ +// g++ -o bin/exp-e -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/e.cpp `libpng-config --ldflags` && ./bin/exp-e +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "rt"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // plainfile + std::vector bytes(3210000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + // keystream + std::vector keystream(bytes.size()); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // acm + uint16_t a = generateRandom(); + uint16_t b = generateRandom(); + uint16_t n = 50; + + uint16_t init = 0; + uint16_t inc = 1; + uint16_t max = 29; + for (uint16_t i = init; i <= max; i += inc) { + uint16_t p = P[i]; + uint16_t q = Q[i]; + uint32_t s = S[i]; + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + uint32_t m = p * q; + std::cout << m << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/f.cpp b/experiment/f.cpp new file mode 100644 index 0000000..3922245 --- /dev/null +++ b/experiment/f.cpp @@ -0,0 +1,131 @@ +// g++ -o bin/exp-f -std=c++17 ftie/*.h ftie/*.cpp experiment/tools.h experiment/tools.cpp experiment/f.cpp `libpng-config --ldflags` && ./bin/exp-f +#include "../ftie/ftie.h" +#include "../experiment/tools.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + + +std::vector P = {3, 1439, 3187, 5011, 6967, 9067, 11083, 13187, 15391, 17491, 19927, 22063, 24391, 26863, 29027, 31379, 33647, 35963, 38447, 40759, 43019, 45659, 48131, 50387, 52967, 55619, 57947, 60607, 62971, 65479}; +std::vector Q = {7, 1447, 3191, 5023, 6971, 9091, 11087, 13219, 15427, 17519, 19963, 22067, 24407, 26879, 29059, 31387, 33679, 35983, 38459, 40763, 43051, 45667, 48163, 50411, 52999, 55631, 57991, 60611, 62983, 65519}; +std::vector S = {20, 1102012, 8269800, 14654568, 8514112, 72359465, 8297696, 24662060, 111450600, 70082287, 235198879, 465814318, 180650199, 505938172, 200851637, 972337598, 426365137, 1242795649, 666777313, 704963584, 823914892, 993565933, 532403049, 2504300086, 1055897756, 1426192730, 3257141641, 359282555, 2439740097, 1549795366}; + +int main(int argc, char const *argv[]) { + try { + const char * slug = "plainfile"; + std::stringstream ss; + + ss << "experiment/dum/" << slug << "_plainfile_1"; + const std::string plainfile1FilepathStr = ss.str(); + const char * plainfile1Filepath = plainfile1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_plainfile_2"; + const std::string plainfile2FilepathStr = ss.str(); + const char * plainfile2Filepath = plainfile2FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_1"; + const std::string cipherimage1FilepathStr = ss.str(); + const char * cipherimage1Filepath = cipherimage1FilepathStr.c_str(); + ss.str(""); + + ss << "experiment/dum/" << slug << "_cipherimage_2"; + const std::string cipherimage2FilepathStr = ss.str(); + const char * cipherimage2Filepath = cipherimage2FilepathStr.c_str(); + ss.str(""); + + std::random_device generateRandom; + + // keystream + std::vector keystream(10000000); + for (int i = 0; i < keystream.size(); i++) + keystream[i] = generateRandom(); + + // bbs + uint8_t bbs = generateRandom() % 30; + uint16_t p = P[bbs]; + uint16_t q = Q[bbs]; + uint32_t s = S[bbs]; + + // acm + uint_fast16_t a = generateRandom(); + uint_fast16_t b = generateRandom(); + uint16_t n = 50; + + uint16_t init = 5; + uint16_t inc = 5; + uint16_t max = 100; + for (uint16_t mb = init; mb <= max; mb += inc) { + // plainfile + std::vector bytes(mb * 100000); + for (int i = 0; i < bytes.size(); i++) + bytes[i] = generateRandom(); + bytes[0] = 1; + std::ofstream outfile1(plainfile1Filepath, std::ios::out | std::ios::binary); + outfile1.write((const char*)&bytes[0], bytes.size()); + outfile1.close(); + + bytes[0] = 3; + std::ofstream outfile2(plainfile2Filepath, std::ios::out | std::ios::binary); + outfile2.write((const char*)&bytes[0], bytes.size()); + outfile2.close(); + + double npcr_1; + double npcr_2; + double hm_1; + double hm_2; + double ava_1; + double ava_2; + std::chrono::duration enc_1; + std::chrono::duration dec_1; + std::chrono::duration enc_2; + std::chrono::duration dec_2; + + { + enc_1 = tools::chrono_deprecated_encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_1 = tools::chrono_deprecated_decrypt(keystream, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_1 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_1 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::deprecated::encrypt(keystream, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_1 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } { + enc_2 = tools::chrono_encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage1Filepath); + dec_2 = tools::chrono_decrypt(p, q, s, a, b, n, cipherimage1Filepath, plainfile1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile1Filepath, cipherimage2Filepath); + npcr_2 = tools::calculate_npcr(cipherimage1Filepath, cipherimage2Filepath); + hm_2 = tools::calculate_entropy(cipherimage1Filepath); + + ftie::encrypt(p, q, s, a, b, n, plainfile2Filepath, cipherimage2Filepath); + ava_2 = tools::calculate_avalanche(cipherimage1Filepath, cipherimage2Filepath); + } + std::cout << (mb / 10.0) << ", "; + + std::cout << npcr_1 << ", "; + std::cout << hm_1 << ", "; + std::cout << ava_1 << ", "; + std::cout << enc_1.count() << ", "; + std::cout << dec_1.count() << ", "; + + std::cout << npcr_2 << ", "; + std::cout << hm_2 << ", "; + std::cout << ava_2 << ", "; + std::cout << enc_2.count() << ", "; + std::cout << dec_2.count() << "\n"; + } + } catch (const char * msg) { + std::cout << msg << '\n'; + } + return 0; +} diff --git a/experiment/tools.cpp b/experiment/tools.cpp new file mode 100644 index 0000000..0628d1a --- /dev/null +++ b/experiment/tools.cpp @@ -0,0 +1,128 @@ +#include "tools.h" + +#include "../ftie/ftie.h" + +#include +#include +#include +#include +#include + +#include "png++/png.hpp" + +namespace tools { + double calculate_avalanche( + const char * path_1, + const char * path_2 + ) { + std::ifstream fst(path_1, std::ios::in | std::ios::binary); + std::ifstream snd(path_2, std::ios::in | std::ios::binary); + uint64_t n = 0; + uint64_t diff = 0; + char cfst; + char csnd; + while (fst.get(cfst) && snd.get(csnd)) { + for (int i = 7; i >= 0; i--) { + bool a = ((cfst >> i) & 1); + bool b = ((csnd >> i) & 1); + if (a != b) + diff++; + n++; + } + } + double n_double = n; + return diff / n_double; + } + + double calculate_entropy( + const char * cipherimageFilepath + ) { + png::image image(cipherimageFilepath); + + uint_fast16_t N = image.get_height(); + std::vector sum(256); + for (uint_fast16_t x = 0; x < N; x++) { + for (uint_fast16_t y = 0; y < N; y++) { + png::rgb_pixel px = image[y][x]; + sum[px.red]++; + sum[px.green]++; + sum[px.blue]++; + } + } + uint_fast32_t n = N * N * 3; + + double hm = 0; + for (int i = 0; i < 256; i++) { + double p = sum[i] / (n * 1.0); + hm += p * log2(1 / p); + } + return hm; + } + + double calculate_npcr( + const char * cipherimage1Filepath, + const char * cipherimage2Filepath + ) { + png::image image_1(cipherimage1Filepath); + png::image image_2(cipherimage2Filepath); + + uint_fast16_t N = image_1.get_height(); + uint_fast32_t sum = 0; + for (uint_fast16_t x = 0; x < N; x++) { + for (uint_fast16_t y = 0; y < N; y++) { + png::rgb_pixel px_1 = image_1[y][x]; + png::rgb_pixel px_2 = image_2[y][x]; + if (!( + (px_1.red == px_2.red) && + (px_1.green == px_2.green) && + (px_1.blue == px_2.blue) + )) { + sum++; + } + } + } + double n = N * N; + + return sum / n; + } + + std::chrono::duration chrono_encrypt( + uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ) { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + ftie::encrypt(p, q, s, a, b, n, plainfileFilepath, cipherimageFilepath); + std::chrono::high_resolution_clock::time_point finish = std::chrono::high_resolution_clock::now(); + return finish - start; + } + + std::chrono::duration chrono_decrypt( + uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ) { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + ftie::decrypt(p, q, s, a, b, n, cipherimageFilepath, plainfileFilepath); + std::chrono::high_resolution_clock::time_point finish = std::chrono::high_resolution_clock::now(); + return finish - start; + } + + std::chrono::duration chrono_deprecated_encrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ) { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + ftie::deprecated::encrypt(keystream, a, b, n, plainfileFilepath, cipherimageFilepath); + std::chrono::high_resolution_clock::time_point finish = std::chrono::high_resolution_clock::now(); + return finish - start; + } + + std::chrono::duration chrono_deprecated_decrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ) { + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); + ftie::deprecated::decrypt(keystream, a, b, n, cipherimageFilepath, plainfileFilepath); + std::chrono::high_resolution_clock::time_point finish = std::chrono::high_resolution_clock::now(); + return finish - start; + } +} diff --git a/experiment/tools.h b/experiment/tools.h new file mode 100644 index 0000000..8a3c696 --- /dev/null +++ b/experiment/tools.h @@ -0,0 +1,41 @@ +#ifndef TOOLS_H +#define TOOLS_H + +#include +#include +#include + +namespace tools { + double calculate_avalanche( + const char * path_1, + const char * path_2 + ); + double calculate_entropy( + const char * cipherimageFilepath + ); + double calculate_npcr( + const char * cipherimage1Filepath, + const char * cipherimage2Filepath + ); + std::chrono::duration chrono_encrypt( + uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ); + + std::chrono::duration chrono_decrypt( + uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ); + + std::chrono::duration chrono_deprecated_encrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ); + + std::chrono::duration chrono_deprecated_decrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ); +} + +#endif diff --git a/ftie/acm.cpp b/ftie/acm.cpp index 60245d6..deb9f55 100644 --- a/ftie/acm.cpp +++ b/ftie/acm.cpp @@ -3,6 +3,7 @@ #include "png++/png.hpp" #include +#include #include @@ -128,6 +129,44 @@ void validation( throw "img's N < 2"; } +uint16_t iroot_long(uint32_t n) { + // https://en.wikipedia.org/wiki/Integer_square_root#Algorithm_using_Newton's_method + float xk = n * 1.0; + float xkp1 = (xk + (n / xk)) / 2.0; + while (std::fabs(xkp1 - xk) >= 1) { + xk = xkp1; + xkp1 = (xk + (n / xk)) / 2.0; + } + return uint16_t(xkp1); +} + +std::vector get_map_deprecated( + uint32_t a, uint32_t b, uint32_t n_t +) { + uint16_t N = iroot_long(n_t) + 1; + std::vector M; + for (uint16_t x = 0; x < N; x++) { + for (uint16_t y = 0; y < N; y++) { + uint16_t X = (x + (a * y % N)) % N; + uint16_t Y = ((b * x % N) + (((a * b % N) + 1) * y % N)) % N; + uint32_t S = N * X + Y; + if ((0 <= S) && (S < n_t)) + M.push_back(S); + } + } + return M; +} + +void validation_deprecated( + uint16_t a, uint16_t b, uint16_t n, + std::vector text +) { + if ((0 == a) || (0 == b) || (0 == n)) + throw "a or b or n is not in (0, inf)"; + if (text.size() < 2) + throw "text's N < 2"; +} + namespace ftie { namespace acm { png::image encrypt( @@ -165,4 +204,43 @@ namespace ftie { return plainimage; } } + + namespace deprecated { + namespace acm { + std::vector encrypt( + uint16_t a, uint16_t b, uint16_t n, + std::vector plaintext + ) { + validation_deprecated(a, b, n, plaintext); + uint32_t n_t = plaintext.size(); + std::vector ciphertext(n_t); + std::vector map = get_map_deprecated(a, b, n_t); + for (uint16_t t = 0; t < n; t++) { + for (uint32_t i = 0; i < n_t; i++) { + ciphertext[i] = plaintext[map[i]]; + } + plaintext = ciphertext; + } + return ciphertext; + } + + std::vector decrypt( + uint16_t a, uint16_t b, uint16_t n, + std::vector ciphertext + ) { + validation_deprecated(a, b, n, ciphertext); + uint32_t n_t = ciphertext.size(); + std::vector plaintext(n_t); + std::vector map = get_map_deprecated(a, b, n_t); + + for (uint16_t t = 0; t < n; t++) { + for (uint32_t i = 0; i < n_t; i++) { + plaintext[map[i]] = ciphertext[i]; + } + ciphertext = plaintext; + } + return plaintext; + } + } + } } diff --git a/ftie/acm.h b/ftie/acm.h index 834fbb1..c8fe2ab 100644 --- a/ftie/acm.h +++ b/ftie/acm.h @@ -18,6 +18,19 @@ namespace ftie { png::image cipherimage ); } + + namespace deprecated { + namespace acm { + std::vector encrypt( + uint16_t a, uint16_t b, uint16_t n, + std::vector plaintext + ); + std::vector decrypt( + uint16_t a, uint16_t b, uint16_t n, + std::vector ciphertext + ); + } + } } #endif diff --git a/ftie/ftie.cpp b/ftie/ftie.cpp index 755bdde..be9b9c9 100644 --- a/ftie/ftie.cpp +++ b/ftie/ftie.cpp @@ -159,4 +159,36 @@ namespace ftie { plainbytes = bytes_sequence_stripping(plainbytes); bytes_sequence_to_physical_file(plainbytes, plainfileFilepath); } + + namespace deprecated { + void encrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ) { + std::vector plainbytes = physical_file_to_bytes_sequence(plainfileFilepath); + plainbytes = bytes_sequence_padding(plainbytes); + + std::vector cipherbytes = ftie::rt::encrypt(plainbytes, keystream); + + cipherbytes = ftie::deprecated::acm::encrypt(a, b, n, cipherbytes); + + png::image cipherimage = bytes_sequence_to_image(cipherbytes); + cipherimage.write(cipherimageFilepath); + } + + void decrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ) { + png::image cipherimage(cipherimageFilepath); + std::vector cipherbytes = image_to_bytes_sequence(cipherimage); + + cipherbytes = ftie::deprecated::acm::decrypt(a, b, n, cipherbytes); + + std::vector plainbytes = ftie::rt::decrypt(cipherbytes, keystream); + + plainbytes = bytes_sequence_stripping(plainbytes); + bytes_sequence_to_physical_file(plainbytes, plainfileFilepath); + } + } } diff --git a/ftie/ftie.h b/ftie/ftie.h index bad8c2a..e92d2e6 100644 --- a/ftie/ftie.h +++ b/ftie/ftie.h @@ -9,6 +9,7 @@ #include + namespace ftie { void encrypt( uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, @@ -18,6 +19,17 @@ namespace ftie { uint16_t p, uint16_t q, uint32_t s, uint16_t a, uint16_t b, uint16_t n, const char* cipherimageFilepath, const char* plainfileFilepath ); + + namespace deprecated { + void encrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* plainfileFilepath, const char* cipherimageFilepath + ); + void decrypt( + std::vector keystream, uint16_t a, uint16_t b, uint16_t n, + const char* cipherimageFilepath, const char* plainfileFilepath + ); + } } #endif diff --git a/main.cpp b/main.cpp index ad470d6..91a1bb0 100644 --- a/main.cpp +++ b/main.cpp @@ -8,19 +8,40 @@ int main(int argc, char* argv[]) { if (argc != 10) { - std::cout << "Check given parameter is correct" << std::endl; - return -1; + if (argc == 8) { + std::cout << "You are using depracated system" << std::endl; + } else { + std::cout << "Check given parameter is correct" << std::endl; + return -1; + } } const char* MODE = argv[1]; const char* IN_FILE_PATH = argv[2]; const char* OUT_FILE_PATH = argv[3]; - uint16_t P = atoi(argv[4]); - uint16_t Q = atoi(argv[5]); - uint32_t S = atoi(argv[6]); - uint16_t A = atoi(argv[7]); - uint16_t B = atoi(argv[8]); - uint16_t N = atoi(argv[9]); - + std::vector KEYSTREAM; + uint16_t P; + uint16_t Q; + uint32_t S; + uint16_t A; + uint16_t B; + uint16_t N; + if (argc == 10) { + P = atoi(argv[4]); + Q = atoi(argv[5]); + S = atoi(argv[6]); + A = atoi(argv[7]); + B = atoi(argv[8]); + N = atoi(argv[9]); + } else if (argc == 8) { + const char* KEYSTREAM_CHARS = argv[4]; + unsigned char* buffer = (unsigned char*)KEYSTREAM_CHARS; + std::vector::size_type size = std::strlen((const char*)buffer); + std::vector ks(buffer, buffer + size); + KEYSTREAM = ks; + A = atoi(argv[5]); + B = atoi(argv[6]); + N = atoi(argv[7]); + } std::string encrypt("encrypt"); std::string decrypt("decrypt"); @@ -30,7 +51,10 @@ int main(int argc, char* argv[]) { try { std::cout << MODE << "ing " << IN_FILE_PATH << " to " << OUT_FILE_PATH << " . . . " << std::endl; auto start = std::chrono::high_resolution_clock::now(); - ftie::encrypt(P, Q, S, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); + if (argc == 10) + ftie::encrypt(P, Q, S, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); + else if (argc == 8) + ftie::deprecated::encrypt(KEYSTREAM, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); auto finish = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed = finish - start; std::cout << "Done! Elapsed time: " << elapsed.count() << " seconds" << std::endl; @@ -42,7 +66,10 @@ int main(int argc, char* argv[]) { std::cout << MODE << "ing " << IN_FILE_PATH << " to " << OUT_FILE_PATH << " . . . " << std::endl; auto start = std::chrono::high_resolution_clock::now(); try { - ftie::decrypt(P, Q, S, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); + if (argc == 10) + ftie::decrypt(P, Q, S, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); + else if (argc == 8) + ftie::deprecated::decrypt(KEYSTREAM, A, B, N, IN_FILE_PATH, OUT_FILE_PATH); auto finish = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed = finish - start; std::cout << "Done! Elapsed time: " << elapsed.count() << " seconds"<< std::endl;