Answer the question
In order to leave comments, you need to log in
How to write an algorithm to decrypt text?
Good evening!
On the instructions of the book, I wrote an algorithm for encrypting and decrypting text by key in such a way that xor is used for the character s: s ^ key[i], where key is the key of the algorithm. every time the key pointer points to the end of the array, it starts counting over again
int main(){
const char* str = "va ahava ma agava";
char* crypted = (char*)malloc(strlen(str)+1);
const char* key = "i am a key. very hard key";
//pointers
char* pcrypted = crypted;
const char* const pkey = key;
const char* const pstr = str;
//pointers
cout << "Original text: " << str << "\nCrypted text:\n";
while(*str){
char k = *key++;
char cr;
if(*str == k) //Если *key == *str, то *key ^ *str
cr = *str++ ^ k+1; //будет равно 0
else //этого нельзя допустить
cr = *str++ ^ k;
*crypted++ = cr;
cout << char(cr);
if(!*key) key = pkey;
}
cout << "\nUncrypted text:\n";
key = pkey;
crypted = pcrypted;
while (*crypted) {
if((*crypted ^ *key) == (*key)+1) //Убрать предохранитель от '\0'
cout << char((*crypted++ ^ *key++)-1);
else
cout << char(*crypted++ ^ *key++);
if(!*key) key = pkey;
}
cout << '\n';
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question