Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <algorithm>
- using namespace std;
- int indeks_w_alfabecie(char c)
- {
- char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for(int i = 0; i < strlen(alfabet); i++)
- {
- if(alfabet[i] == c)
- {
- return i;
- }
- }
- }
- char* obetnij_klucz(char* klucz)
- {
- string result;
- int dlugosc_klucza = strlen(klucz);
- for(int i = 0; i < dlugosc_klucza; i++)
- {
- char letter = klucz[i];
- size_t n = std::count(result.begin(), result.end(), letter);
- if(n == 0)
- {
- result += klucz[i];
- }
- }
- return const_cast<char*>(result.c_str());
- }
- char* szyfr_vigenere( char* tekst, char* klucz )
- {
- klucz = obetnij_klucz(klucz);
- char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char tab[26][26];
- int spot = 0;
- for(int i = 0; i < 26; i++)
- {
- for(int j = 0; j < 26; j++)
- {
- spot = (j + i) % 26;
- if(spot < 0)
- {
- spot += 26;
- }
- tab[i][j] = alfabet[spot];
- }
- }
- /* wydrukuj tablice
- for(int i = 0; i < 26; i++)
- {
- cout << endl;
- for(int j = 0; j < 26; j++)
- {
- cout << tab[i][j];
- }
- }
- */
- int dlugosc_klucza = strlen(klucz);
- int dlugosc_tekstu = strlen(tekst);
- char* result = new char[dlugosc_tekstu + 1];
- // przejdz przez caly tekst
- for(int i = 0; i < dlugosc_tekstu; i++)
- {
- char literka_tekstu = tekst[i];
- int n = indeks_w_alfabecie(tekst[i]);
- int spot = i % dlugosc_klucza;
- int m = indeks_w_alfabecie(klucz[spot]);
- result[i] = tab[m][n];
- }
- return result;
- }
- int main()
- {
- char klucz[50];
- cout << "podaj klucz: " << endl;
- cin >> klucz;
- cout << "wybrano klucz: " << klucz << endl;
- char tekst[1000];
- cout << "podaj tekst " << endl;
- cin >> tekst;
- char* szyfrogram = szyfr_vigenere(tekst,klucz);
- cout << szyfrogram;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement