Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3992432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:45:51+00:00 2026-05-20T06:45:51+00:00

I’m making a XOR based en/decryptor, that works like this. You have a plaintext

  • 0

I’m making a XOR based en/decryptor, that works like this.
You have a plaintext character, for example 3, and a user key, for example 5.
Written in bits:

3 = 00000011

5 = 00000101

Now if we do XOR operation, we get 6:

6 = 00000110

This can be reversed by saying 6 XOR 5, which is 3.

So I have made this program. But it’s really buggy, it doesn’t translate the text right, and it adds a lot of characters in the end of the file, depending which key you are using.

using namespace std;
int main(int argc, char *argv[])
{
    char buffer[5001]; 
    ifstream fin("a.txt", ifstream::in);
    ofstream fout("b.txt");
    int key;
    char znak;

    // console    
    cout << "Key: ";
    cin >> key;
    fin.get(znak);
    while(!fin.eof() && znak != ' ')
    {
       fin.get(buffer, sizeof(buffer));     
    }

    for(int i = 0; i < sizeof(buffer); i++)
    {
       fout << function(key, buffer[i]);
    }

    cout << "done" << endl;

    cin.get();
    return 0;
}

char function(int key,char input)
{
    return input ^ key;
}

Why doesn’t the program translate the text right? And Why does it add characters to the end of the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:45:52+00:00Added an answer on May 20, 2026 at 6:45 am

    From the looks of things, the encryption has almost nothing to do with anything (or at least with the problem) here.

    while(!fin.eof() && znak != ' ')
    

    A loop of the form while (!whatever.eof()) is pretty much guaranteed to work incorrectly.

        fin.get(buffer, sizeof(buffer)); 
    

    When you do this, note that it does not guarantee that it will read sizeof(buffer) characters — only that it won’t read any more than that. You typically get fewer at the end of the file, when there simply aren’t that many characters left to read. [Edit: I should also mention that you can read fewer in other places as well — e.g., if you’re reading from a network connection, it’s fairly common to receive a partial buffer, but more later when it arrives.]

    for(int i = 0; i < sizeof(buffer); i++)
    

    So here, when you attempt to process sizeof(buffer) characters, chances are pretty good that (especially on the last iteration) you’re attempting to process more characters than you actually read. You can retrieve the number you read with gcount, though I’m not sure I’d really recommend using it.

    Personally, I’d probably do something like this:

    class function { 
        char key;
    public:
        function(char k) : key(k) { }
        char operator()(char input) { return key ^ input; }
    };
    
    int main() { 
        std::ifstream fin("a.txt");
        std::ofstream fout("b.txt");
    
        fin.noskipws();
    
        std::transform(std::istream_iterator<char>(fin),
                       std::istream_iterator<char>(),
                       std::ostream_iterator<char>(fout),
                       function(key));
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.