Initial commit

This commit is contained in:
datalore 2023-10-24 13:14:21 +02:00
commit c979478938
6 changed files with 127 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
brute
shift
count
vigenere

11
Makefile Normal file
View File

@ -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

27
brute.cc Normal file
View File

@ -0,0 +1,27 @@
#include <iostream>
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] << " <ciphertext>" << std::endl;
return 0;
}
for(int i = 1; i < 26; ++i)
{
shift(argv[1], i);
}
return 0;
}

30
count.cc Normal file
View File

@ -0,0 +1,30 @@
#include <map>
#include <iostream>
int main(int argc, char** argv)
{
if(argc < 2)
{
std::cout << "Usage: " << argv[0] << " <ciphertext>" << std::endl;
return 0;
}
char* ciphertext = argv[1];
std::map<char, int> 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;
}

24
shift.cc Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
int main(int argc, char** argv)
{
char* text;
int key;
if(argc < 3)
{
std::cout << "Usage: " << argv[0] << " <plain text> <key>" << 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;
}

31
vigenere.cc Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
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] << " <plain text> <key>" << 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;
}