The second day I try to make encryption Xor'om, but it still does not work. Here is my code. What is wrong with me?

#include "stdafx.h" #include <iostream> #include <Windows.h> #include <string> using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int key; char text[100]; string res; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ тСкст: " << endl; // Π²Π²ΠΎΠΆΡƒ тСкст cin >> text; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠ»ΡŽΡ‡: " << endl; cin >> key; // Π²Π²ΠΎΠΆΡƒ ΠΊΠ»ΡŽΡ‡ for (int i = 0; i < strlen(text); i++) { // подгоняю Ρ€Π°Π·ΠΌΠ΅Ρ€ res[i] = text[i] ^ key; //само ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½ΠΈΠ΅ } cout << "Π—Π°ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ тСкст: " << res << endl; return 0; } 
  • What exactly does not work? In addition to checking the compliance of the length of the key and the message, I do not see crime. Well, under the res memory would be allocated - Anton Shchyrov
  • @AntonShchyrov Well, let's say, if I enter any word and key, I get a Line error: 2954, Expression: string subscript out of range - P1NG2WIN
  • 2
    I said that the memory for string should be allocated - Anton Shchyrov
  • First, tell us what problem you are solving? What is your key length? Why do you need a string type. And do you need to simultaneously store the original string and the encrypted? - Anton Shchyrov
  • @AntonShchyrov And how to allocate a dynamic count in memory? I made string * res = new string; , but it gives the error "An exception was thrown: access violation for the record. _Left was (address)" - P1NG2WIN

2 answers 2

As already said, you have the string res zero length, i.e. the string is there, but the memory for the simpoli is not allocated. Before turning to the i_tom symbol, it is necessary that this symbol be. Those. It is necessary to initialize the string res , for example, like this:

 .... char text[100]; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ тСкст: " << endl; // Π²Π²ΠΎΠΆΡƒ тСкст cin >> text; string res(test); ... 

Now you have the string res this full copy of text . But here you are right, if you do not need to keep the original line, then you can direct it and modify it like this:

 ... for (int i = 0; i < strlen(text); i++) { // подгоняю Ρ€Π°Π·ΠΌΠ΅Ρ€ text[i] = text[i] ^ key; //само ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½ΠΈΠ΅ } cout << "Π—Π°ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ тСкст: " << text << endl; ... 

But here there are pitfalls. If you enter a key that matches the ASCII code of the character from the string, then after the XOR operation, this character will be in place of \0 , which is a sign of the end of the string. Then std :: cout will print the encrypted string only up to this character and you will forever lose the original size of the string.

  • Thanks for the explanation! @ffk - P1NG2WIN
  • And how to fix the output to \ 0? @ffk - P1NG2WIN
 std::string key; std::string str; std::cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ тСкст: " << std::endl; std::getline(std::cin, str); // Π²Π²ΠΎΠ΄ΠΈΠΌ тСкст. ΠŸΠ°ΠΌΡΡ‚ΡŒ выдСляСтся автоматичСски std::cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠ»ΡŽΡ‡: " << std::endl; std::getline(std::cin, key); // Π²Π²ΠΎΠ΄ΠΈΠΌ ΠΊΠ»ΡŽΡ‡ int keyIdx = 0; // Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ индСкс Π±Π°ΠΉΡ‚Π° ΠΊΠ»ΡŽΡ‡Π° for (int i = 0; i < str.size(); i++) { str[i] = str[i] ^ key[keyIdx++]; // ΡˆΠΈΡ„Ρ€ΡƒΠ΅ΠΌ строку ΠΈ смСщаСм индСкс ΠΊΠ»ΡŽΡ‡Π° if (keyIdx == key.size()) // Ссли достигли ΠΊΠΎΠ½Ρ†Π° ΠΊΠ»ΡŽΡ‡Π° - Π½Π°Ρ‡ΠΈΠ½Π°Π΅ΠΌ сначала keyIdx = 0; } std::cout << "Π—Π°ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ тСкст: " << str << std::endl; 

The type std :: string may contain \ 0 within a string.

  • And for what a minus? - Anton Shchyrov
  • Thanks for the example! @Anton Shchyrov - P1NG2WIN February
  • And is it possible to fix the completion of encryption before the end of the line due to \ 0? - P1NG2WIN
  • @ P1NG2WIN The string type is not affected. There the size is set separately. The problem is relevant only for char* - Anton Shchyrov
  • But you yourself write that it can contain \ 0 and even with your code I don’t get something [ imgur.com/a/iAKLs ] + where for some reason I didn’t have the text inserted in English - P1NG2WIN