Skip to content

Instantly share code, notes, and snippets.

@savolla
Last active December 22, 2019 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savolla/6cce2ae04598bd4e06ab7554bff84a22 to your computer and use it in GitHub Desktop.
Save savolla/6cce2ae04598bd4e06ab7554bff84a22 to your computer and use it in GitHub Desktop.
/*
@AUTHOR: Oleksiy Nehlyadyuk
@NUMARA: 182113201
@TITLE: Affine Cipher Konsol Uygulaması
*/
// ÖNEMLİ NOT: program cin fonksiyonunun azizliğine uğramıştır. veri alınırken boşluk kullanılamıyor. getline ile alınmaya
// çalıştığımda ise program plaintext'i es geçip keyi almaya çalışıyor. 208. satırda iki yolu da yazdım
#include <iostream>
#include <string>
#include <chrono> // sleep için
#include <thread> // bu da sleep için
using namespace std::this_thread; // sleep ZZzz..
using namespace std::chrono; // sleep
using namespace std;
#define ALPHABET_SIZE 69 // alfabenin boyutunu global olarak tanımladım
// c++'da Türkçe karakterleri bastırmanın standart bir yolunu bulamadığım için sadece ascii karakterler kullandım
// noktalama işaretlerini de dahil etmek istedim ancak bazı semboller sorun çıkardı
char ALPHABET[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0','1','2','3','4','5','6','7','8','9' };
// dinamik dizi kullanmak yerine kendi listemi yazmaya karar verdim
// bütün bağlı liste fonksiyonlarını implemente etmedim gerek olmadığı için
struct node {
int data;
node* next = nullptr;
};
// ekle fonksiyonu
node* add(node* root, int x) {
if (root == nullptr) {
root = new node();
root->data = x;
}
else {
node* tmp = root;
while (tmp->next != nullptr) {
tmp = tmp->next;
}
tmp->next = new node();
tmp->next->data = x;
}
return root;
}
// get, bağlı listenin içinden indis numarasına ile veri çekmeye yarar
int get(node* root, int index) {
if (root == nullptr) {
cout << "ERROR: List is empty" << endl;
return -1;
}
else {
node* tmp = root;
for (int i = 0; i < index; ++i) {
tmp = tmp->next;
if (tmp == nullptr) {
cout << "ERROR: There is no such index like "
<< index << endl;
return -1;
}
}
return tmp->data;
}
}
// size: listenin boyutunu verir
int size(node* root) {
int length = 0;
while (root != nullptr) {
root = root->next;
length++;
}
return length;
}
// listenin içeriğini bastırır. programı debug etmek için kullandım
void dump(node* root) {
while (root != nullptr)
{
cout << root->data << endl;
root = root->next;
}
}
// listenin istenilen indisindeki veriyi değiştirir
void replace(node* root, int index, int new_value) {
node* tmp = root;
for (int i = 0; i < index; ++i)
{
tmp = tmp->next;
}
tmp->data = new_value;
}
// parametre olarak verilen string verinin içindeki harflerin, yukarıda global olarak
// tanımlanmış alfabenin içinde arama yaparak indis değerlerini, bir bağlı listeye
// depolayan fonksiyon
node* convert_to_numbers(string value_to_convert) {
node* numbers = nullptr; // sayıların depolanacağı bağlı listenin rootunu oluşturdum
for (int i = 0; i < value_to_convert.length(); ++i) { // string içindeki her harf için şunları yap:
for (int j = 0; j < ALPHABET_SIZE; ++j) { // stringden alınan harfi alfabenin içinde ara
if (value_to_convert[i] == ALPHABET[j]) { // eğer harf, alfabenin içindeyse
numbers = add(numbers, j); // bağlı listeye alfabedeki indisi(sayıyı) depola
}
}
}
return numbers; // sayılarla dolu olan bağlı listeyi gönder
}
// bu sefer de bağlı listeden sayısal verileri teker teker çekip
// alfabede hangi karaktere karşılık geliyorsa onu alıp text değişkenine
// ekler. yukarıdakinin tam tersi
string convert_to_letters(node* value_to_convert) {
string text = ""; // depo string değişken tanımladım
for (int i = 0; i < size(value_to_convert); ++i) { // bağlı listedeki her sayı için şunları yap:
for (int j = 0; j < ALPHABET_SIZE; ++j) { // bağlı listeden alınan sayıyı, alfabedeki hangi harfin indexine denk geliyor ara
if (get(value_to_convert, i) == j) // eğer sayı ve index eşleştiyse
{
text += ALPHABET[j]; // text değişkenine harfi depola
}
}
}
return text; // oluşan texti gönder
}
// gelen string veriyi, key ile birlikte şifreleyen fonksiyon
string encrypt(string i_value, string i_key) {
node* value = convert_to_numbers(i_value); // value bağlı listesine, parametre olarak alınan stringin sayısal değeri depolanıyor
node* key = convert_to_numbers(i_key); // aynı şekilde key bağlı listesine de key parametresinin sayısal değeri atılıyor
int control = 0; // anahtarın döngüsel olarak dönmesi için bir kontrol değişkeni
int limit = i_key.length(); // anahtar'ın string uzunluğu alınıyor
int tmp; // bağlı listeyi dolaylı yoldan modifiye etmek için geçici değişken
int tmp2; // bu da key listesi için geçici değişken
for (int i = 0; i < size(value); ++i) { // şifrelenecek olan veriyi tutan bağlı listedeki her eleman için şunları yap:
if (control == limit) { // eğer anahtar son karaktere ulaştıysa
control = 0; // baş harfe dönmek için indisi 0 yap
}
tmp = get(value, i); // tmp değişkenine bağlı listenin i indisindeki sayı atanıyor
tmp2 = get(key, control); // aynı şekilde tmp2 içine key değeri atanıyor
tmp += tmp2; // asıl şifreleme işlemi gerçekleşiyor
tmp %= ALPHABET_SIZE; // eğer tmp değeri alfabenin boyutunu geçtiyse tur attır
replace(value, i, tmp); // çıkan şifrelenmiş değeri o anki indisteki veriyle değiştir
control++; // anahtarın indisini 1 arttır
}
string new_value = convert_to_letters(value); // bağlı listenin şifrelenmiş halini alıp stringe çeviriyoruz ve new_value'ye atıyoruz
return new_value; // new_value'yu gönder
}
// defişre fonksiyonu
string decrypt(string i_value, string i_key) {
node* value = convert_to_numbers(i_value); // şifrelenmiş string ifadedeki harfleri çek ve sayısal değerleri bağlı listeye ata
node* key = convert_to_numbers(i_key); // aynı şekilde key bağlı listesine de anahtarın sayısal değerlerini ata
int control = 0;
int limit = i_key.length();
int tmp = 0;
int tmp2 = 0;
for (int i = 0; i < size(value); ++i) {
if (control == limit) {
control = 0;
}
tmp = get(value, i);
tmp2 = get(key, control);
tmp += ALPHABET_SIZE; // burda hiç bir şekilde eksili değer çıkmasın diye alfabenin boyutuyla topladık
tmp -= tmp2; // sonra anahtarın o anki indisine karşılık gelen sayısal değeri şifreliden çıkardık
tmp %= ALPHABET_SIZE; // alfabenin boyutuyla topladığımiz için bunu normalize etmemiz lazım
replace(value, i, tmp); // şifreli değer deşifre edilmiş değer ile değiştiriliyor
control++; // anahtarın indexi 1 arttırılıyor
}
string new_value = convert_to_letters(value); // deşifre edilmiş sayısal değerler karakter olarak new_value'ye atanıyor
return new_value; // deşifreli stringi gönder
}
int main(void) {
// bundan sonrası tamamen kozmetik(fasafiso) kısmı
char choice;
string plaintext, key, cipher;
cout << "....................................." << endl;
cout << "...../\\\\............................." << endl;
cout << "..../ \\\\...+++.+++.+.+...+...+.+...." << endl;
cout << ".../ \\\\..++..++..+.+...+....+....." << endl;
cout << "../ \\\\.+...+...+.+++.+++..+....." << endl;
cout << "./________\\\\........................." << endl;
cout << "....................................." << endl;
cout << "-------------------------------------" << endl;
cout << " Welcome To Affily Encryptor!" << endl;
cout << "-------------------------------------" << endl;
cout << " What do you want to do?" << endl;
cout << " [0] -> Encrypt String" << endl;
cout << " [1] -> Decrypt String" << endl;
cout << "-------------------------------------" << endl;
cout << "choice: ";
cin >> choice;
if (choice == '0')
{
string animation[] = { "/", " -", " \\", " |", " /", " -" };
system("cls");
//system("clear");
cout << "....................................." << endl;
cout << ".............. ................" << endl;
cout << "............. ... ..............." << endl;
cout << "............. ..... ..............." << endl;
cout << "........... ............." << endl;
cout << "........... ::: ............." << endl;
cout << "........... ............." << endl;
cout << "-------------------------------------" << endl;
cout << " Encrypting " << endl;
cout << "-------------------------------------" << endl;
// I. YOL: plaintext'i es geçiyor ve keyi istiyor...
/*
cout << "text: "; getline(cin, plaintext);;
cout << "key : "; getline(cin, key);
*/
// II. YOL: burda da düzgün çalışıyor ama tek telime alıyor
cout << "text: "; cin >> plaintext;;
cout << "key : "; cin >> key;
for (int i = 0; i < 6; ++i)
{
system("cls");
//system("clear");
cout << "....................................." << endl;
cout << ".............. ................" << endl;
cout << "............. ... ..............." << endl;
cout << "............. ..... ..............." << endl;
cout << "........... ............." << endl;
cout << "........... ::: ............." << endl;
cout << "........... ............." << endl;
cout << "-------------------------------------" << endl;
cout << " Encrypting " << endl;
cout << "-------------------------------------" << endl;
cout << "text: " << plaintext << endl;
cout << "key : " << key << endl;
cout << "-------------------------------------" << endl;
cout << "CALCULATING" << animation[i] << endl;
sleep_until(system_clock::now() + milliseconds(200));
}
string cipher = encrypt(plaintext, key);
cout << "-------------------------------------" << endl;
cout << "cipher: " << cipher << endl;
cout << "-------------------------------------" << endl;
}
else if (choice == '1')
{
string animation[] = { "...", " ...", " ...", " ...", " ...", "..." };
//system("clear");
system("cls");
cout << "....................................." << endl;
cout << "... ............................" << endl;
cout << ".. . ..........................." << endl;
cout << ".. : : ..." << endl;
cout << ".. ' ................ ......" << endl;
cout << "... ................. . . ......" << endl;
cout << "....................................." << endl;
cout << "-------------------------------------" << endl;
cout << " Decrypting " << endl;
cout << "-------------------------------------" << endl;
cout << "cipher: "; cin >> cipher;
cout << "key : "; cin >> key;
for (int i = 0; i < 6; ++i)
{
system("cls");
//system("clear");
cout << "....................................." << endl;
cout << "... ............................" << endl;
cout << ".. . ..........................." << endl;
cout << ".. : : ..." << endl;
cout << ".. ' ................ ......" << endl;
cout << "... ................. . . ......" << endl;
cout << "....................................." << endl;
cout << "-------------------------------------" << endl;
cout << " Decrypting " << endl;
cout << "-------------------------------------" << endl;
cout << "cipher: " << cipher << endl;
cout << "key : " << key << endl;
cout << "-------------------------------------" << endl;
cout << "CALCULATING" << animation[i] << endl;
sleep_until(system_clock::now() + milliseconds(200));
}
string decrypted = decrypt(cipher, key);
cout << "-------------------------------------" << endl;
cout << "plaintext: " << decrypted << endl;
cout << "-------------------------------------" << endl;
}
else {
cout << "an Affily technician is coming to your place to implement choice " << choice << " please wait." << endl;
return 0;
}
cout << "Come again later <3" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment