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 6998705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:26:42+00:00 2026-05-27T20:26:42+00:00

The following program attempts to encrypt a string and save in to a text

  • 0

The following program attempts to encrypt a string and save in to a text file, and open the file, decrypt and show the message.

Here is the code-

private: System::Void saveToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
             SaveFileDialog^ dlg = gcnew SaveFileDialog();
             dlg->Filter = "Text Files|*.txt";
             char key = 'a';
             if(dlg->ShowDialog()==Windows::Forms::DialogResult::OK)
             {
                 String^ stream = txtOutput->Text;
                 char* num = new char[stream->Length];
                 char* xorchar = (char*)(void*)Marshal::StringToHGlobalAnsi(stream);
                 int i=0;
                 for(i=0;i<stream->Length;++i){
                     num[i] = *xorchar ^ key;
                     *xorchar++;
                 }

                 num[i] = '\0';  //add trailing NULL
                 //Marshal::FreeHGlobal((System::IntPtr)(void*)xorchar);  THIS WAS GIVING AN ERROR, NOT SURE WHY
                 String^ save = gcnew String(num);
                 System::IO::File::WriteAllText(dlg->FileName, save);
             }

         }
private: System::Void openToolStripMenuItem_Click(System::Object^  sender,      System::EventArgs^  e) {
         OpenFileDialog^ dlg = gcnew OpenFileDialog();
         dlg->Filter = "Text Files|*.txt";
         String^ stream;
         char key = 'a';
         if(dlg->ShowDialog()==Windows::Forms::DialogResult::OK)
         {
            stream = System::IO::File::ReadAllText(dlg->FileName);


            char* num = (char*)(void*)Marshal::StringToHGlobalAnsi(stream);
            int i=0;
            for (i=0;i<stream->Length;++i)
            {
                num[i] = num[i] ^ key;   //DECRYPT
            }

            String^ orig_stream = gcnew String(num);
            txtOutput->Text = orig_stream;

         }
     }

Now the issue is, when i input a string, it works only for characters which are not equal to the key.
Example, Let key = ‘a’

eg: INPUT: “I Like This” // This will decrypt correctly.

INPUT: “I Like apples” //Only upto ‘I Like’ will decrypt, rest doesnt show up.

ie, If it encounters the character ‘key’ (in this case ‘a’), it stops decryption.

Any help is appreciated. Thanks!

  • 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-27T20:26:43+00:00Added an answer on May 27, 2026 at 8:26 pm

    A value xor’d with itself will always produce 0. A 0 char is also called the null-terminator (0 == '\0'). Strings in C++ are null-terminated, aka they stop at the null-terminator.

    Simple example:

    #include <iostream>
    
    int main(){
      char str[] = "hi there!\0this will not get printed.";
      std::cout << str;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following program: static void Main (string[] args) { int i; uint ui; i
I have the following program to open lot's of sockets, and hold them open
I am writing a PHP program which reads file with file_get_contents then attempts to
The following simple Java program appears to display the string Hello World through the
I get the following error when my program attempts to connect to a URL
I have the following XML : <sample> <Message number=2> <Title>Program pointer has been reset</Title>
The following program is very simple: it outputs a single dot each half a
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
I have the following program: ~/test> cat test.cc int main() { int i =

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.