1

I have a trouble with this. I writing a code for the "vigenere cipher".

I have text and key. But i want to get alphabet from the user. what the user wants etc:"abcdfgh" or "sdgfjdgkfdsgs" just what the user wants.

So but i can't do it.

How do I get alphabet from the user?

Firstly, i want to do get alphabet from the user. After, I want it to enter the word and encrypt it. But the words alphabet is user's alphabet.

Here is the codes:
 #include <iostream>
 #include <string>
 using namespace std;

 // Bu fonksiyon bir key oluşturur.
  string generateKey(string str, string key) 
 {
 int x = str.size();

 for (int i = 0; ; i++)
  {
      if (x == i)
        i = 0;
      if (key.size() == str.size()) // eğer oluşturulan key boyutu girilen 
    metnin boyutuna eşitse fonksiyonu durdur.
        break;
    key.push_back(key[i]);
}
return key;
    }
    string cipherText(string str, string key) // "/Bu fonksiyon orjinal metni şifreler \"
      {
    string cipher_text;

   for (int i = 0; i < str.size(); i++)
  {
    // converting in range 0-25 
    int x = (str[i] + key[i]) % 26;

    // alfabeyi ASCII kodlarina dönüştür:
    x += 'A';

    cipher_text.push_back(x);
 }
       return cipher_text;
  }

    //  "/Bu fonksiyon şifreli metni orjinal hale getirir\"

      string originalText(string cipher_text, string key)
    {
string orig_text;

for (int i = 0; i < cipher_text.size(); i++)
{
    // converting in range 0-25 
    int x = (cipher_text[i] - key[i] + 26) % 26;

    // convert into alphabets(ASCII) 
    x += 'A';
    orig_text.push_back(x);
}
return orig_text;
       }
        int main()
          {
cout << " Sifrelenmesini istediginiz kelimeyi/cumleyi giriniz" << endl;
string str;
getline(cin, str);

//string str = "METINBUGRA";
cout << "Anahtar kelimeyi giriniz." << endl;
string keyword;
getline(cin, keyword);
//string keyword = "ABC";

string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);

cout << "Sifrelenmis Kelime : "
    << cipher_text << "\n";

cout << "Cozumlenmis kelime : "
    << originalText(cipher_text, key);
system("pause");
return 0;
   }
peeebeee
  • 2,273
  • 6
  • 18
  • 24
M.Bugra
  • 11
  • 2

1 Answers1

1

If I correctly understood your question, you want to use a custom alphabet instead of English alphabet. For instance you may add digits.

  1. Instead of actual letters you must operate on numbers: 0, 1, 2, ... N-1, where N is the size of the alphabet. For English alphabet this means you must use 0 instead of A (0x41), 1 instead of B (0x42), ... 25 instead of Z.

  2. If the size of the key is M, the encryption algorithm for letter at position i is:

    ( L[i] + K[i mod M] ) mod N

    Once you have a functional algorithm that operates on numbers, all you have to do is map your input from letters to numbers and your output from numbers to letters.

  3. Mapping numbers to letters is easy; you just have to store the alphabet into a string – this answers your question:

    string n_to_letter; // alphabet
    //...
    int main()
    {
      //...
      cin >> n_to_letter; // read the alphabet
      //...
    
  4. Mapping letters to numbers is probably beyond your current knowledge; you must use a map:

    #include <map>
    //...
    string n_to_letter; // alphabet
    map< char, int > letter_to_n;
    
    void init_letter_to_n() //...
    

    If you do not know how to use a map, there is a workaround: just search for the letter in the alphabet string, or use a 256 characters vector/string.

DEMO

zdf
  • 3,363
  • 2
  • 14
  • 25