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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:11:39+00:00 2026-06-03T17:11:39+00:00

Write a cryptographic program that works similarly to my crypto program:• Prompt the user

  • 0

Write a cryptographic program that works similarly to my crypto program:• Prompt the user to enter a secret key and use it to compute the seed of a random number generator • Prompt the user to give the names of an input file and the output coded/decoded file• Create the sequence of random bytes from a random number generator with the seed obtained from user’s secret key. • Perform the coding of bit x using a random bit r as follows: x⊕ = r • Since ∀r ∈{0,1}, r ⊕r = 0, perform decoding using the same sequence of random bytes and the same operation x⊕ = r . Decoding is based on the operation x⊕r ⊕r = x⊕(r ⊕r) = x⊕0 = x

Below is the code that works for encryption but when it come to decryption, the cipher text contains only 3 characters. I have not been able to figure why the decoding does not work. I am using Dev-C++. Your help will be very appreciated.

  #include<iostream>
  #include<conio.h>
  #include<math.h>
  #include<stdlib.h>
  #include<fstream>

  using namespace std;


  int main()
  {   
    char  n, line[1024],  keystring[100]; char FitoEncr[100], NewFiCrypt[100];
    char FiletobeDecrypted[100];
    ifstream IS ("FitoEncr.txt", ios::in);
    ofstream OS ("NewFiCrypt.txt", ios::out); 

    unsigned int psswd=0, number;
    cout<<"Please, enter a secret key :";
    cin.getline(keystring, 100);
    for( int i=0;keystring[i]!='\0'; i++)

      psswd=(psswd+3)*keystring[i];
    cout<<"initial password: "<<keystring<<endl;
    cout<<"encrypted password: "<<psswd<<endl;

    cout<<"please, enter the name of the input file: ";
    cin.getline(FitoEncr,20);
    cout<<"please, enter the name of the output file: ";
    cin.getline(NewFiCrypt,20);

    srand(psswd);    //not sure about creating the sequence of random bytes from a...
    number=rand() % 255; //random number generator with the seed obtained from user's secret key

   //ENCRYPTION

    while(IS.get(n))
     {
         if(IS.get(n))
         {      
           n^=rand();
           OS<<n;
           cout<<"character coded:"<<n;   
         }

     }
     IS.close();
     OS.close();

    //DECRYPTION

     ifstream IS1;
     ofstream OS1; 

     IS1.open("NewFiCrypt.txt", ios::in);
     OS1.open("FilDecr.txt", ios::out); 

     while(IS1.get(n))
     {
       if(IS1.get(n))
       {      
         n^=rand();  //I think the problem starts on this line...
         OS1<<n;
         cout<<"character coded:"<<n;   
       }

     }       
     IS1.close();
     OS1.close();     

     getch();

     return 0;
  }
  • 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-06-03T17:11:45+00:00Added an answer on June 3, 2026 at 5:11 pm

    You seed the random number generator before encrypting, but not before decrypting, so the series of random numbers will be different; obviously they would have to be the same for this to work. If you add another

    srand(psswd);  
    

    before the decryption second, that will get you closer.

    On closer inspection, though, there are some significant other problems. For example, each time you call get(n), you consume a character from your input file. You’re calling get(n) in your while loop conditions, but then immediately calling it again in your if; that means you’ll effectively be skipping every other character on input and again on output; the decrypted file would then end up with just 25% of the characters from the original file! Just remove the if statements altogether, and your loop will be right. Hopefully having fixed both of these things your program will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a program that sums the sequence of integers as well as the smallest
Write a recursive function search(l,key) that returns a boolean: True if key is in
Write a simple program that reads a line from the keyboard and outputs the
Write an applet that draws the rectangles (use fillPolygon() function) turned by angle ALPHA
Write a program to determine whether a computer is big-endian or little-endian. bool endianness()
write a script that takes two optional boolean arguments,--verbose‚ and ‚--live, and two required
I write programs used internally by our company. I've created several support DLLs that
I write a c++ windows application (A), that uses LogonUser, LoadUserProfile and ImpersonateLoggedOnUser to
i write some utility class but how to get name ? Can that send
I need to implement some crypto protocol on C# and want to say that

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.