reversing encrytpion done by xor

I was wonder I figured out how to decrypt in xor with this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdio>
int main()
{
    char z[30];
    char y[22]= ".`}}w8_'&D_dk-_-ii:~";
    std::cin >> z;
    for(int x=0; x<1000; x++)
    {
        z[x]=z[x]^y[x];
        std::cout << z[x];
    }
    return 0;
}


But I cannot decrypt it.
You're going past the ends of both of your arrays. Change the for loop to stop at 22.

Also, you don't need to include cstdio for this program. You should remove it.
ok I changed it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
    char z[22];
    char y[22]= ".`}}w8_'&D_dk-_-ii:~";
    std::cin >> z;
    for(int x=0; x<22; x++)
    {
        z[x]=z[x]^y[x];
        std::cout << z[x];
    }
    return 0;
}


But How do i reverse the process?
You just need to xor z with y again.
ok thanks but could give me an example this is quite confusing.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
int main()
{
    char z[22];
    char y[22]= ".`}}w8_'&D_dk-_-ii:~";
    std::cin >> z;
    for(int x=0; x<22; x++)
    {
        z[x]=z[x]^y[x];
        std::cout << z[x];
        z[x]=z[x]^y[x];
        std::cout << z[x];
    }
    return 0;
}
Thanks now I need help with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>
#include <iostream>
#include <cstdlib>
#define SIZE 250
#define number 200
#define perline 100
using namespace std;
int n;
char z[5000];
char y[10]= ("password");
int main()
{
    FILE * f;
    f = fopen("file.txt", "r");
    if(f != NULL)
    {
        while(fgets(z, sizeof(z), f) != NULL)
        {
            for(int x=0; x<5000; x++)
            {
                z[x]=z[x]^y[x];
                std::cout << z[x];
            }
            cout << ("Press 1 to to exit.");
            cin >> n;
            while (n == 1)
            {
                fclose(f);
                return 0;
            }
        }
    }
}


It gives a gcc runtime error as is.

But I need to read the file decrypt and then display without creating another. Thanks.
Now you're reading past the end of y. With your encryption scheme, the password has to be at least as long as the data. Otherwise you'll have to keep repeating the password, which is a security vulnerability.

I'll clean up that code for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <fstream>
#include <iostream>
#include <vector>

//note: no defines or global variables here

using namespace std;

int main()
{
    ifstream f("file.txt", ios::binary);
    if(!f.is_open()) return -1;

    f.seekg(0, ios::end);
    unsigned size = f.tellg();
    f.seekg(0, ios::beg);

    vector <char> z(size);
    f.read(&z.front(), size);
    char y[10] = "password"; //I didn't fix the error

    //you probably don't actually want a while loop here,
    //since newline characters will typically be encrypted
    
    for(unsigned x = 0; x < size; ++x)
    {
        z[x] = z[x] ^ y[x]; //fix the overflow here
        cout << z[x];
    }
    cout << endl;

    return 0;
}
    
Last edited on
Thanks I did not put the real password because its one I use allot.
Just a note from my personal experience with this, you'll want to make sure that you open the source file in binary mode. This is because things like NULL and white space would be output to the file the same but the ASCII values are different.

Telion said:
With your encryption scheme, the password has to be at least as long as the data. Otherwise you'll have to keep repeating the password, which is a security vulnerability.
This is like saying steal is weak against thermite. For what you are doing right now OP, don't worry too much about what Telion said here. Even though he is 100% correct, attacks on a poly-alphabetic cipher rely on the repetition of the keyword to help determine its length, you could spend weeks on this topic and never finish your code. Cryptanalysis is an amazing topic and it would need it's own website just to scratch the surface of.

http://en.wikipedia.org/wiki/Kasiski_examination

The link above is one of the shortest articles I've ever read on Wikipedia, and I've never spent more time reading anything else.
Thanks I fixed it and uses a password of 48 characters.
Topic archived. No new replies allowed.