Hello i have a encrypted message with key that was used to encrypt it .
I want to reverse it .
first i have this java snippet which works completely fine and gives correct answer too .
encrypted message : Bjj rwkcs dwpyp fwz ovors wxjs vje tcez fqg
plain text : The quick brown fox jumps over the lazy dog
now i am tryint to do same in C++
below is my code :
now the output of my code is : Thh8mrfxr9bunwn8dux8ovors:opbk2thc8oxdy9doe
i did everything same , don't know why the wrong output .
I want to reverse it .
first i have this java snippet which works completely fine and gives correct answer too .
encrypted message : Bjj rwkcs dwpyp fwz ovors wxjs vje tcez fqg
plain text : The quick brown fox jumps over the lazy dog
Code
static String decrypt(String input) {
int[] key = {8,2,5,1,2,2,0};
StringBuilder res = new StringBuilder();
int i = 0;
for(char c: input.toCharArray()) {
if(i == key.length) i = 0;
if(Character.isLetter(c)) {
char firstLetter = c <= 'Z' ? 'A' : 'a';
c = (char)(c - key[i]);
if(c < firstLetter) c = (char)(c + 26);
i++;
}
res.append(c);
}
return res.toString();
}now i am tryint to do same in C++
below is my code :
Code
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string decrypt(string encrypted_message) {
int key[] = {8,2,5,1,2,2,0};
int key_size = sizeof(key) / sizeof(key[0]);
string res;
int i = 0;
for(char c : encrypted_message)
{
if(i == key_size)
{
i = 0;
}
if(isalpha(c) || isspace(c))
{
char first_letter = c <= 'Z' ? 'A' : 'a';
c = (char)(c-key[i]);
if( c < first_letter)
{
c = (char)(c + 26);
i++;
}
res = res + c;
}
}
return res;
}
int main()
{
string res;
string _encrypted_message = "Bjj rwkcs dwpyp fwz ovors wxjs vje tcez fqg";
res = decrypt(_encrypted_message);
cout << res << endl;
return 0;
}now the output of my code is : Thh8mrfxr9bunwn8dux8ovors:opbk2thc8oxdy9doe
i did everything same , don't know why the wrong output .


