Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

my code snippet not giving correct output

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

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 .
This is not equivalent:

Code
if (isalpha(c) || isspace(c))

Why are you using isspace()?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Nov 2, 2017 01:16 PM)0xDEAD10CC Wrote:  This is not equivalent:

Code
if (isalpha(c) || isspace(c))

Why are you using isspace()?

because spaces are ignored by isalpha() . i want both alphabets and spaces to be recognised . if the letter is alphabet or space enter the if section .
(Nov 4, 2017 02:51 AM)kakarot007 Wrote:  because spaces are ignored by isalpha() . i want both alphabets and spaces to be recognised . if the letter is alphabet or space enter the if section .

You definitely missed the point: https://msdn.microsoft.com/en-us/library....110).aspx

That's not what char.IsLetter is equivalent to. How do you assume this is equivalent code if you obviously don't read documentation? :/
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Nov 4, 2017 01:49 PM)0xDEAD10CC Wrote:  You definitely missed the point: https://msdn.microsoft.com/en-us/library....110).aspx

That's not what char.IsLetter is equivalent to. How do you assume this is equivalent code if you obviously don't read documentation? :/

Damn :( . you are right . What's equivalent of isLength in c++ ?
Simply isalpha() without isspace(). Lastly, you didn't pay close enough attention to your braces or how control conditional statements work that don't have braces. Only the line after is used...

Code
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))
        {
            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;
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
this thread just reminded of range based for loop in encryption and decryption. will try to do this with my encryption
#c/c++# & #asm#  


The way to patch bad code is to remove the bad code, not to add new bad code that hides the old bad code 


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by еlixirSep 9, 2022 06:31 AM 0 132 Sep 9, 2022 06:31 AM
Last Post: еlixir
[C#] by Saddam4liveJul 19, 2020 05:38 AM 7 154 Jul 19, 2020 05:38 AM
Last Post: Maxie
[Python] by datguy.devNov 28, 2019 10:37 PM 4 299 Nov 28, 2019 10:37 PM
Last Post: Eternal Darkness