Struggling with Array Encryption

Pages: 12
So C++ is my first programming language that i'm learning and I've been struggling with a file encryption assignment using arrays and a simple substitution cipher without the use of functions.
I basically need this input file:

If people do not believe that mathematics is simple, it is only
because they do not realize how complicated life is.

Remark made by von Neumann as keynote speaker at
the first national meeting of the Association for Computing Machinery
in 1947, as mentioned by Franz L. Alt at the end of 'Archaeology of
computers - Reminiscences, 1945--1947', Communications of the ACM,
volume 15, issue 7, July 1972, special issue --- Twenty-fifth
anniversary of the Association for Computing Machinery, p. 694.

Encrypted to:

UL^BDQBOD~VQ#XQR`MDOUDTD:RAER^KERADKERUNJ~UJ#JUKBOD;`UR:UJ^QXOH~
MDNEYJD#RADH`VQ:XQR^ZDEOUFD~AQG#NQKBOUNERDV`OULD:UJ.ZDKEZP^KEVD~
MH#TQX`XDYKEXX:EJ^PDHXQRD~JBDEPDZ#ER`RAD:LUZJR^XERUQXEO~KDDRUXW#
QL`RAD:EJJQNUERUQX^LQZ~NQKBYRUXW#KENAUXDZH`UX:8052;^EJ~KDXRUQXDV#
MH`LZEXF:O.^EOR~ER#RAD`DXV:QL^EZNAEDQOQWH~QL#NQKBYRDZJ`ZDKUXUJND
XNDJ;:8054//8052;^NQKKYXUNERUQXJ~QL#RAD`ENK;:TQOYKD^84;~UJJYD#2;`CYO
H:8027;^JBDNUEO~UJJYD#RGDXRH/LULRA`EXXUTDZJEZH:QL^RAD~EJJQNUERUQX#
LQZ`NQKBYRUXW:KENAUXDZH;^B.~305.

I'm able to do everything until I get to the actual encryption and I've been at this for almost 3 days straight.
Any help would be appreciated, even if it's just a place to start.

Code posted below.


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

int main()
{
int SIZE = 26;
char data;
int x = 0;


    ifstream fin;
    fin.open("eFile.txt");
    if(!fin)
    {
        cout << "Program is terminated - cannot locate input file";
        cout << " or wrong file name.";
        return 1;
    }

    char letters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0'};
    char encryptL[] = {'E', 'M', 'N', 'V', 'D', 'L', 'W', 'A', 'U', 'C', 'P',
     'O', 'K', 'X', 'Q', 'B', 'I', 'Z', 'J', 'R', 'Y', 'T', 'G', 'S', 'H', 'F', '\0'};

    int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int encryptN[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

    char special[] = {'!', '$', '%', 39, '(', ')', '*', '+', ',', '-', '.', '/', ';',
    '<', '=', '>', '?', '@', '[', ']', '}', '{', '\0'};
    char encryptS[] = {'{', '}', ']', '[', '@', '?', '>', '=', '<', ';', '/', '.', '-',
    ',', '+', '*', ')', '(', 39, '%', '$', '!', '\0'};
    char encryptSpace[] = {'^', '~', '#', '`', ':', '\0'};

    while (fin)
    {
        fin.get(data);
        if (data >= 'A' && data <='Z' || data >='a' && data <= 'z')
            {
                if (islower(data))
                 data = toupper(data);

            cout << data;
            }



        else if (data == 32)
            cout << data;


        else if (data >= 33 && data <= 47)
            cout << data;

        else if (data >= 48 && data <= 57)
            cout << data;

    }


    return 0;
}
Last edited on
I don't see where you're actually doing any Encryption. Somewhere you need to substitute a value from your encryption array for the character you retrieved from the file.

Hello dasen0, what's the exercise? Do have to implement a certain encryption algorithm, or do you need to find the algorithm by your self by comparing the plain text with the encrypted text?
As jlb said you not encrypting anything, you're just storing characters and numbers in arrays, what are these for?

Don't you need an encryption key to know which way you're supposed to modify the original message to get the encrypted one you mentioned above?

I've been struggling with a file encryption assignment involving arrays without the use of functions.

Why not use a function? I think that would make your assignment a lot less tedious.
The exercise is to use a simple substitution cipher to encrypt the file. My professor also told us that we can't use anything we haven't learned yet so functions are out. I need help understanding how this works and where to actually have the substitution cipher. The notes I have from the class don't help, the only hint he gave is that we need the arrays to compare the values and that the while(fin) statement to read in the data character by character was really important.
Last edited on
Sorry for not posting this as well, this are the keys he gave us.
Plaintext alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher text alphabet : EMNVDLWAUCPOKXQBIZJRYTGSHF
Numbers :0123456789
Cipher numbers :9876543210
Special symbols :!$%&'()*+,-./;<=>?@[]}{
Cipher special symbols :{}][@?>=<;/.-,+*)('&%$!
Space replacer symbols :^~#`:
If I understand you right, then all you have to do is, assigning each encrypted letter the letter for plain text, where some encypted letters have the same plain text letter (space).

Think about this pseudo code:


FOR EACH encrypted letter IN encryped_text :
    Calculate the index of the encrypted letter.
   Substitute encrypted letter by its plain-text-letter with by using the index.


Calculating the index is easy, if you know that all characters are represented as consecutive enumerations at the ASCII (google helps if this topic is unknown). For example you could calculate the Index of 'H' by 'H' - 'A'.

Oh wait, sorry, it's not helpful to calculate the differences between ASCII characters, but rather working with indices.

Below I have made a working program based on your code. I strongly recommend you not to look at the code and rather trying the exercise by your mind!

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;
    fin.open("eFile.txt");
    if(!fin)
    {
        cout << "Program is terminated - cannot locate input file";
        cout << " or wrong file name.";
        return 1;
    }

    char letters[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char encryptL[] = "EMNVDLWAUCPOKXQBIZJRYTGSHF";

    char numbers[]  = "0123456789";
    char encryptN[] = "9876543210";

    char special[]  = "!$%'()*+,-./<=>?@[]}{";
    char encryptS[] = "{}][@?>=<;/.-,+*)(%$!";
    char encryptSpace[] = "^~#`:";

    while (true)
    {
        char data;
        fin.get(data);
        
        if( !fin ) break; // end of stream or others
        
        data = toupper(data);
        
        if (data >= 'A' && data <='Z')
        {
            for (int i = 0; i < sizeof(encryptL); ++i)
            {
                if( data == encryptL[i] )
                {
                    cout << letters[i];
                    break;
                }
            }
         }
         else if( data >= '0' && data <= '9')
         {
             for( int i = 0; i < sizeof(encryptN); ++i)
             {
                 if( data == encryptN[i] )
                 {
                     cout << numbers[i];
                     break;
                 }
             }
         }
         else
         {
             bool character_found = false;
             for( int i = 0; i < sizeof(encryptS); ++i)
             {
                 if( data == encryptS[i])
                 {
                     character_found = true;
                     cout << special[i];
                     break;
                 }
             }
             
             if( character_found == true)
                 continue;
             
             for( int i = 0; i < sizeof(encryptSpace); ++i)
             {
                 if( data == encryptSpace[i] )
                 {
                     character_found = true;
                     cout << ' ';
                     break;
                 }
             }
                 
             if( character_found == true)
                 continue;
             else
                 // handling all others like line break u co
                 cout << data; 
         }
    }
}
Last edited on
Thank you so much, I'll definitely look this over and try to understand where I went wrong. I just have to go home and run it and I'll let you know how it works out for me
So if I want the space ciphers to cycle through, would i need another loop after this if statement
if( data == encryptSpace[i] )
{
character_found = true;
cout << ' ';
break;

in order for it to go to next in the array?
example:
plain: Hi how are you?
cipher: AU^AQG~EZD#HQY(
Last edited on
I don't know what you mean, why would you need another loop?. The 'if'-branch inside the loop detects whether any of the 'data' character is one of from 'encryptedSpace' array. If so, it prints a space and jumps out of the for-loop and the program goes trying to read in the next char from 'fin'.
The way the statement works is when it reads a space it goes to replace it with one of the characters in the encryptSpace array, but it only uses the first one. It doesn't use any of the others, so I'm wondering if I need to include another loop so it will cycle through the encryptSpace array and use the next one then repeat that process.
It doesn't use the first one but rather the one which matches the 'data' character.

But, could it be you bother of trying to encrypt some stuff, am I right? Then, if you want to use different substitutions for encrypting the space, you need a mechanism which selects any of them from 'encryptSpace[]'. A good way would be selecting a character by using std::rand(). Don't forget to set an initial random seed with std::srand(std::time(nullptr)).
Maybe there is some misunderstanding: Each cycle of the overall loop handles only one character! So if there would be more then one space in consecutive, then the others would get handled at other cycles of the overall loop.
It's not random though, the way the input is ciphers out is that the space ciphers cycle through and repeat in order:
UL^BDQBOD~VQ#XQR`MDOUDTD:RAER^KERADKERUNJ
~UJ#JUKBOD;`UR:UJ^QXOH~
MDNEYJD#RADH`VQ:XQR^ZDEOUFD~AQG#NQKBOUNERDV
`OULD:UJ.ZDKEZP^KEVD~
MH#TQX`XDYKEXX:EJ
Which is why I was asking if I need a loop to do that
Last edited on
Then you could use a sort of memory variable which serves as an index, and each time you would detect a space you could increment that variable, setting it to 0 if it cycles to the last character (via % operator).
So could I use some sort of do, while statement?
I have no idea what the memory vairable would even look like to do that.
You could replace the for-loop which handles the spaces by:

1
2
3
4
5
6
if( data == encryptSpace[space_index] ) 
{
    cout << ' ';
    space_index = (space_index + 1) % sizeof(encryptSpace);
    continue;
}
Last edited on
When i run it i get the same problem, the spaces all just get ciphered to ^. I'm assuming I have to make a declaration for space_index and set it to 0?
Yea of course, above the overall loop.
Pages: 12