Napisałem taki sobie algorytm do szyfrowania.
Działa on tak:
- Losuje klucz.
- Szyfruje podany tekst za pomocą klucza.
- Odszyfrowuję zaszyfrowaną wiadomość za pomocą klucza.
Punkt 1 działa dobrze (punkt) 2 szyfruje pierwszą literę a dla pozostałych daję minusowe wartości
natomiast (punkt) 3 wyświetla znak '=' dla każdego znaku.
Nie wiem gdzie robię błąd - oto kod:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
char array_of_characters[] = "AaĄąBbCcĆćDdEeĘęFfGgHhIiJjKkLlŁłMmNnŃńOoÓóPpRrSsŚśTtUuWwXxYyZzŹźŻż";
int *Key(int size)
{
srand( time( NULL ) );// potrzebne do losowania liczb pseudolosowych
int *array_codes = new int [ size ];
int x = 1, index = 0, i = 1, checked_index = 0;
//cout << "Klucz:";
while(x == 1)
{
array_codes[index] = rand()% 999999;
i = 1;
checked_index = 0;
while(i == 1)
{
if(array_codes[index] == array_codes[checked_index])
{
array_codes[index] = rand()%999999;//indeks tablicy ma wartość wylooswanej liczby
}
checked_index++;
if(checked_index >= (size - 1))i = 0;
}
//cout << " " << array_codes[index];
index ++;
if(index == (size -1 ))x = 0;
}
return array_codes;
}
int *Cypher(char *board, int *code, int number_of_encoded_text)
{
int checked_mark = 0, comparison_with_an_array_of_characters = 0, index = 0;
int *An_array_of_character_code = new int [ number_of_encoded_text ];
while(checked_mark <= (number_of_encoded_text - 1))
{
while(comparison_with_an_array_of_characters <= (number_of_encoded_text - 1))
{
if(board[checked_mark] == array_of_characters[comparison_with_an_array_of_characters]) An_array_of_character_code[checked_mark] = code[comparison_with_an_array_of_characters];
comparison_with_an_array_of_characters++;
}
checked_mark++;
}
return An_array_of_character_code;
}
char *Decoding(char *array_of_characters, int *array_codes, int *array_ciphers )
{
int size_an_array_of_character_code = sizeof(array_of_characters)/sizeof(int);
int size_array_codes = sizeof(array_of_characters)/sizeof(char);
char *decoded_message = new char [ size_array_codes ];
bool x = 1, y = 1;
int index_code = 0, index = 0;
while(x <= (size_an_array_of_character_code - 1))
{
index = 0;
while(y <= (size_array_codes - 1))
{
if(array_ciphers[index_code] == array_codes[index])decoded_message[index_code] = array_of_characters[index];
index++;
}
index_code++;
}
return decoded_message;
}
void main()
{
char text[] = "AaBb"; // TEKST DO ZAKODOWANIA
int size_array_of_characters = sizeof(array_of_characters)/sizeof(char);
int size_text = sizeof(text)/sizeof(char);
int *array_of_encoded_characters = new int [size_array_of_characters - 1];
int *tablica = new int [size_array_of_characters-1];
char *decoded_message = new char [ size_text];
int index = 0, i = 0, x = 0, y = 0;
cout << "Ilosc kodowanych znakow " << (size_array_of_characters - 1) << endl;
tablica = Key(size_array_of_characters);
cout << endl << "Klucz:" << endl;
while(i <= (size_array_of_characters ))
{
if(i == 10 || i == 20 || i == 30 || i == 40 || i == 50 || i == 60 || i == 70)cout << endl;
cout << tablica[i] << endl;
i++;
}
cout << endl << "Koniec klucza "<< endl << endl;
//////////////////////////////////////////////
array_of_encoded_characters = Cypher(text, tablica, (size_text - 1));
while(x <= (size_text - 2))
{
if(x == 10 || x == 20 || x == 30 || x == 40 || x == 50 || x == 60 || x == 70)cout << endl;
cout << array_of_encoded_characters[x] << endl;
x++;
}
cout << endl;
//////////////////////////////////////////////
cout << endl << "Wiadomosc to:" << endl;
decoded_message = Decoding(array_of_characters, tablica, array_of_encoded_characters);
while(y <= (size_text - 2))
{
cout << decoded_message[y] << endl;
y++;
}
//////////////////////////////////////////////
cout << endl;
delete[]array_of_encoded_characters;
delete[]tablica;
delete[]decoded_message;
system("pause");
}