B
B
badguy052019-12-18 21:33:10
Cryptography
badguy05, 2019-12-18 21:33:10

How can I fix the program so that the original message is equal to the decrypted one?

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;

int main() {
  setlocale(LC_ALL, "rus");
  char msg[] = "Привет мир!";
  unsigned char key = 84;
  
  int N = strlen(msg);
  cout << N << endl;
  
  
  for (int i = 0; i < N; ++i) {
  
    msg[i] ^= key;
    msg[i] = ((msg[i] >> 2) | (msg[i] << 6));
}

  

  for (int i = 0; i < N; ++i) cout << msg[i];
  cout << endl;

  
  for (int i = 0; i < N; ++i) {
    msg[i] = ((msg[i] >> 6) | (msg[i] << 2));
    msg[i] ^= key;
}


  for (int i = 0; i < N; ++i) cout << msg[i];
  cout << endl;	
  
  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ocelot, 2019-12-18
@Ocelot

Convert character to unsigned char before shifting. The left shift of a signed variable is undefined behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question