Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,237 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,253 people online right now. Registration is fast and FREE... Join Now!




XOR ENCRYPTION/DECRYPTION

 
Reply to this topicStart new topic

XOR ENCRYPTION/DECRYPTION, my program is out of control

anya_ritika
13 Oct, 2008 - 04:35 AM
Post #1

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 15

i am asked to write a program that takes an ASCII string, as well as
an encryption key and mode specifier from the command line and processes the string
using the encryption key.
for example:
Lab.exe 51 e "Omnia mutantur"
gives the output: 124094093090082019094070071082093071070065
and:

Lab.exe 51 d 124094093090082019094070071082093071070065
gives the output: Omnia mutantur

i havent done the argc and argv yet, which i will do later...but the thing is first of all my encryption is not working...i m getting different output...can u help me with this please..

CODE

#include <iostream>
#include <cstdlib>

using namespace std;


using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
int main( )
{
// create a string to encrypt
char string[ ] = "Omnia mutantur";
cout << "Original string is: " << string << endl;
encrypt( string );
// call to the function encrypt( )
cout << "Encrypted string is: " << string << endl;
decrypt( string );
// call to the function decrypt( )
cout << "Decrypted string is: " << string << endl;
system ("pause");
return 0;
}// main

//encrypt data
void encrypt (char e[] )
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) {
for(; * ePtr != '\0'; ++* ePtr ) --(* ePtr);
}

i desperately need help...

User is offlineProfile CardPM
+Quote Post

GWatt
RE: XOR ENCRYPTION/DECRYPTION
13 Oct, 2008 - 05:02 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,178



Thanked: 18 times
Dream Kudos: 450
My Contributions
You're giving the program a string, telling it to encrypt the string, and the program encrypts it. You then give the program the encrypted string, and tell it to decrypt the string, which it does.

It seems to be working. What exactly is your issue?
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: XOR ENCRYPTION/DECRYPTION
13 Oct, 2008 - 05:15 AM
Post #3

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE
am asked to write a program that takes an ASCII string, as well as
an encryption key and mode specifier from the command line


So, where is your encryption key?
an XOR encryption algorithm XORs the key with your string to encrypt it and does same with encrypted text to decrypt it.

You are not doing that, which is the reason why you are not getting the expected output.

let's go like this.

key is your key ans string is your character array, then it will be something like this.

CODE

for (i=0,j=0;i<strlen(string);i++)
{
   string[i]=string[i]^key[j++];
   if(j==strlen(key)) j=0;
}



I hope this will help you. smile.gif
User is online!Profile CardPM
+Quote Post

anya_ritika
RE: XOR ENCRYPTION/DECRYPTION
15 Oct, 2008 - 02:06 AM
Post #4

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 15

thanx for the help but..
somehow i dont know how should i apply this in my code...i tried, but still gives the same result...can u please paste the code u provided above(regarding key) at the appropriate place in my code, so that i get the desired result...my code is just above...thanx for help...
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: XOR ENCRYPTION/DECRYPTION
15 Oct, 2008 - 02:19 AM
Post #5

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
I have done modifications in your code. Just go through it and you will understand the mistakes.

cpp

#include <iostream>
#include <cstdlib>

using namespace std;


void encrypt( char [], char [] ); // prototypes of functions used in the code
void decrypt( char [],char [] );

int main( )
{
// create a string to encrypt
char string[ ] = "AmitTheInfinity";
char key[] = "12345";
cout << "Original string is: " << string << endl;
encrypt( string, key );
// call to the function encrypt( )
cout << "Encrypted string is: " << string << endl;
decrypt( string, key );
// call to the function decrypt( )
cout << "Decrypted string is: " << string << endl;
cin.get();
return 0;
}// main

//encrypt data
void encrypt (char e[], char key[] )
{
int len = strlen(e);
for (int i=0, j=0;i<len;i++)
{
e[i]=e[i]^key[j++];
if(j==strlen(key)) j=0;
}

} // encrypt

//decrypt data
void decrypt( char * ePtr, char key[] ) {
int len = strlen(ePtr);
for (int i=0, j=0;i<len;i++)
{
ePtr[i]=ePtr[i]^key[j++];
if(j==strlen(key)) j=0;
}
}



I hope this will help you. smile.gif
User is online!Profile CardPM
+Quote Post

anya_ritika
RE: XOR ENCRYPTION/DECRYPTION
15 Oct, 2008 - 09:43 PM
Post #6

New D.I.C Head
*

Joined: 16 Mar, 2008
Posts: 15

thanx for the help...but there is a problem..
when i run the program : say for the string being...
"Omnia mutantur"
i get an encrypted output is not:
124094093090082019094070071082093071070065
which is the expected output...
how can i solve this problem..
???help me soon..please..need to submit tonight...

This post has been edited by anya_ritika: 15 Oct, 2008 - 09:45 PM
User is offlineProfile CardPM
+Quote Post

Hyper
RE: XOR ENCRYPTION/DECRYPTION
15 Oct, 2008 - 10:39 PM
Post #7

D.I.C Head
**

Joined: 15 Oct, 2008
Posts: 134



Thanked: 3 times
My Contributions
QUOTE(anya_ritika @ 15 Oct, 2008 - 10:43 PM) *

thanx for the help...but there is a problem..
when i run the program : say for the string being...
"Omnia mutantur"
i get an encrypted output is not:
124094093090082019094070071082093071070065
which is the expected output...
how can i solve this problem..
???help me soon..please..need to submit tonight...


I wrote, compiled, and use this in Dev-C++.

- main.cpp
CODE

/*
  Name: Encryptor
  Copyright: 2008, September 17th -- Wednesday
  Author: Danielle
  Date: 18/09/08 02:55
  Description: Program encrypts/decrypts a message stored within a file
*/

#include <iostream>
#include <fstream>
#include <stdio.h> // included specifically for _flushall()

using namespace std;

#include "main.h"

int main()
{
    char *Filename = new char[20];
    int ans;

    while (ans != 4) {
    cout << "1 = Encrypt message to file" << endl
         << "2 = Encrypt file" << endl
         << "3 = Decrypt file" << endl
         << "4 = Quit" << endl << endl
         << "Encrypt or decrypt? ";
    cin >> ans;

    switch (ans) {
           case 1 :
                cout << "Please type a filename to encrypt a MESSAGE to: ";
                cin >> Filename;
                _flushall();

                EncryptMessage(Filename);
                system("PAUSE");
                system("CLS");
                break;

           case 2 :
                cout << "Please type a FILENAME to encrypt: ";
                cin >> Filename;
                _flushall();

                EncryptFile(Filename);
                system("PAUSE");
                system("CLS");
                break;

           case 3 :
                cout << "Please type a FILENAME to decrypt the message: ";
                cin >> Filename;
                _flushall();

                Decrypt(Filename);

                system("PAUSE");
                system("CLS");
                break;

           case 4 :
                delete(Filename);
                exit(0);
                break;

    default : system("CLS"); cout << "Incorrect input entered!" << endl << endl;
    }
}
    cout << endl;
    system("PAUSE");
    system("CLS");
    
    delete(Filename);

    return EXIT_SUCCESS;
}


- main.h
CODE

#ifndef MAIN_H
#define MAIN_H

struct Vars {
       char EncryptFile[20001];  // Encrypt fi)le
       char message[20001];      // Encrypt message
       char Decrypt[20001];      // Decrypt message
       long int Encrypt;         // Encryption-key
       int n;                    // Counter
       } Key;

void EncryptMessage(char *Filename) {

     Key.n = 0;
     ofstream out;

    // Open the file to write to it
    out.open(Filename);

    if (out.fail()) {
    cout << "Couldn't create the file\n\n";
    return;
    }

    cout << "Enter message to encrypt: ";
    cin.getline(Key.message, 20001), '\n';

    cout << "Enter key to encrypt with: ";
    cin >> Key.Encrypt;
    cout << "<<---------------------------->>" << endl << endl;

    // Loop to encrypt message -- XOR
    while (Key.message[Key.n] != '\0') {
          Key.message[Key.n] ^= Key.Encrypt;
          Key.n++;
    }

    out << Key.message;
    out.close();

    // Open the file to write to it
    out.open("Key.txt");

    if (out.fail()) {
    cout << "Couldn't create the file\n\n";
    return;
    }
    
    out << "Filename: " << Filename << endl;
    out << "Encryption key: " << Key.Encrypt << endl;
    out.close();

     return;
}

void Decrypt(char *Filename) {

     Key.n = 0;

     char FileCharacter;
     fstream FileMod;
    
    FileMod.open(Filename, ios_base::in);

    if (!FileMod.is_open()) {
    cout << "\nFile not found.\n\n";
    return;
    }

    cout << "Enter key to decrypt message with: ";
    cin >> Key.Encrypt;
    cout << "<<---------------------------->>" << endl << endl;

     while ( (FileCharacter = FileMod.peek()) != EOF)  {
           FileMod.get(Key.Decrypt[Key.n]);
           Key.n++;
           }

     FileMod.close();

     Key.n = 0;

    // Loop to dencrypt message -- XOR
    while (Key.Decrypt[Key.n] != '\0') {
          Key.Decrypt[Key.n] ^= Key.Encrypt;
          Key.n++;
    }

    cout << Key.Decrypt;
    cout << endl << endl << "<<---------------------------->>" << endl << endl;

     return;
}

void EncryptFile(char *Filename) {

     Key.n = 0;

     char FileCharacter;
     ifstream inFile;
     ofstream outFile;

    inFile.open(Filename, ios_base::in);

    if (!inFile.is_open()) {
    cout << "\nFile not found.\n\n";
    return;
    }

    cout << "Enter key to encrypt FILE with: ";
    cin >> Key.Encrypt;
    cout << "<<---------------------------->>" << endl << endl;

    // Read file into Key.EncryptFile
     while ( (FileCharacter = inFile.peek()) != EOF)  {
           inFile.get(Key.EncryptFile[Key.n]);
           Key.n++;
           }

     inFile.close();

     Key.n = 0;

    // Loop to dencrypt message -- XOR
    while (Key.EncryptFile[Key.n] != '\0') {
          Key.EncryptFile[Key.n] ^= Key.Encrypt;
          Key.n++;
    }

    outFile.open(Filename, ios_base::in);

    if (!outFile.is_open()) {
    cout << "\nFile not created.\n\n";
    return;
    }

    outFile << Key.EncryptFile;
    outFile.close();
/*
    // Open the file to write to it
    outFile.open("Key.txt");

    if (outFile.fail()) {
    cout << "Couldn't create the file\n\n";
    return;
    }
    
    outFile << "Filename: " << Filename << endl;
    outFile << "Encryption key: " << Key.Encrypt << endl;
    outFile.close();
*/
     return;
}

#endif


I hope it helps, perhaps you can extract your answer from it.
I'm not the best of programmers I don't think, but at least it works and isn't 'all over the place.'

I'm not a fan of OOP, I'm unsure if you were required/suggested to do it OOP-style.
Good luck with your homeowrk!
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: XOR ENCRYPTION/DECRYPTION
15 Oct, 2008 - 11:43 PM
Post #8

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
Well, the code I gave you works fine and it's giving the same output you expected. It's just that it is bit modified and unformatted. smile.gif

The value you said as an expected output is printed characters as integers and with addition of zero after every character.

see the input is.

Omnia mutantur

The output my program gives [it's integer equivalent]
124 94 93 90 82 19 94 70 71 82 93 71 70 65

and Your expected output
124094093090082019094070071082093071070065

if you observe closely, all those spaces [I added those spaces to make it more clear, actual output was all numbers together] are replaced with 0 in your output.

So I have done small tweaks in the program for you.

cpp

#include <iostream>
#include <cstdlib>

using namespace std;


void encrypt( char [], char [], int ); // prototypes of functions used in the code
void decrypt( char [], char[], int );

int main( )
{
// create a string to encrypt
char str[ ] = "Omnia mutantur"; // The string you had in example
char encrypted[(strlen(str)+1)*2]; // This will hold the encrypted array.
int key = 51; // This is the key you had in your example
int i;
cout << "Original string is: " << str << endl;
encrypt( str, encrypted, key );
// call to the function encrypt( )
cout << "Encrypted string is: ";
for(i= 0;i< (strlen(str)*2)-1;i++)
cout << (int)encrypted[i]; // You wanted to print the integer version of encrypted string.
cout << endl;
cin.get();
for(i=0;i<=strlen(str); i++)
str[0]='\0'; // clearing the original string, here we will restore the text we get after decryption.
decrypt( encrypted, str, key );
// call to the function decrypt( )
cout << "Decrypted string is: " << str << endl;

cin.get();
return 0;
}// main

//encrypt data
void encrypt (char e[], char ans[], int key )
{
int len = strlen(e);
int i,j;
for ( i=0, j=0;i<len;)
{
ans[j++]=e[i++]^key; // actual XOR logic.
if(i<len)
{
ans[j++]=0; // We are adding this to make the output you wanted.
}


}
ans[j]='\0';

} // encrypt

//decrypt data
void decrypt( char * ePtr,char *original, int key ) {
int len = strlen(ePtr);
for (int i=0, j=0;i<len;i+=2,j++) +2 for i because we added 0 after every character while encryption, we need to ignore them while decryption.
{
original[j]=ePtr[i]^key;
}
}


I hope this will help you. smile.gif

This post has been edited by AmitTheInfinity: 15 Oct, 2008 - 11:43 PM
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 03:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month