S
S
sddvxd2018-12-15 23:54:11
C++ / C#
sddvxd, 2018-12-15 23:54:11

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';
}

After this task, a new one appeared - to decrypt the encoded text, while not having a key. Please tell me any ideas

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-12-16
@sddvxd

Kasiski Method
https://en.wikipedia.org/wiki/Kasiski_Method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question