commit c979478938286953220ca340f235d4fb3567ff2c Author: datalore Date: Tue Oct 24 13:14:21 2023 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f7b034 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +brute +shift +count +vigenere diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e7565c5 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +all: shift brute count vigenere +shift: shift.cc + g++ shift.cc -o shift +brute: brute.cc + g++ brute.cc -o brute +count: count.cc + g++ count.cc -o count -std=c++20 +vigenere: vigenere.cc + g++ vigenere.cc -o vigenere +clean: + rm shift brute diff --git a/brute.cc b/brute.cc new file mode 100644 index 0000000..9a8a8c3 --- /dev/null +++ b/brute.cc @@ -0,0 +1,27 @@ +#include + +void shift(char* text, int key) +{ + std::cout << key << ": "; + for(int i = 0; text[i] != 0; ++i) + { + char c = text[i]-key; + if(c < 'a') c+=26; + if(c > 'z') c-=26; + std::cout << c; + } + std::cout << std::endl; +} +int main(int argc, char** argv) +{ + if(argc < 2) + { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 0; + } + for(int i = 1; i < 26; ++i) + { + shift(argv[1], i); + } + return 0; +} diff --git a/count.cc b/count.cc new file mode 100644 index 0000000..24fccd7 --- /dev/null +++ b/count.cc @@ -0,0 +1,30 @@ +#include +#include + +int main(int argc, char** argv) +{ + if(argc < 2) + { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 0; + } + char* ciphertext = argv[1]; + std::map letters; + + for(int i = 0; ciphertext[i] != 0; ++i) + { + if(letters.contains(ciphertext[i])) + { + letters[ciphertext[i]]++; + } + else + { + letters[ciphertext[i]] = 1; + } + } + for(auto& [key, value] : letters) + { + std::cout << key << ": " << value << std::endl; + } + return 0; +} diff --git a/shift.cc b/shift.cc new file mode 100644 index 0000000..1c4d859 --- /dev/null +++ b/shift.cc @@ -0,0 +1,24 @@ +#include + +int main(int argc, char** argv) +{ + char* text; + int key; + if(argc < 3) + { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 0; + } + text = argv[1]; + key = atoi(argv[2]); + + for(int i = 0; text[i] != 0; ++i) + { + char c = text[i]+key; + if(c < 'a') c+=26; + if(c > 'z') c-=26; + std::cout << c; + } + std::cout << std::endl; + return 0; +} diff --git a/vigenere.cc b/vigenere.cc new file mode 100644 index 0000000..2c3b229 --- /dev/null +++ b/vigenere.cc @@ -0,0 +1,31 @@ +#include + +int keyvalue(char c) +{ + return c - 'a'; +} +int main(int argc, char** argv) +{ + char* text; + char* key; + if(argc < 3) + { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 0; + } + + text = argv[1]; + key = argv[2]; + + int ki = 0; + for(int pi = 0; text[pi] != 0; ++pi) + { + char c = text[pi] + keyvalue(key[ki]); + if(c < 'a') c+=26; + if(c > 'z') c-=26; + std::cout << c; + if(key[++ki] == 0) ki = 0; + } + std::cout << std::endl; + return 0; +}