How to put a line of a text file into an array for encryption? (AES)

I am writing a program for AES Encryption which is all but done as far as that the program encrypts the text and prints the encrypted text in the command prompt:

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
  void main()
{
	int a;

	unsigned char CipherKey[16] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
	unsigned char PlainText[16] = {0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};


    for(a=0;a<16;a++)
    {
        Key[a] = CipherKey[a];
        input[a] = PlainText[a];
    }

    KeyExpansion();
    Cipher();
    
	cout << endl;
    cout << "Encryted output text:" << endl;

    for(a=0;a<16;a++) // For loop is used to print every character of the output array onto the console
    {
        printf("%02x ",output[a]); // printf is used in order to print the encrypted text in hexidecimal
    }

    cout << endl;

}


encrypted text output on command prompt = 39 25 84 1d 02 dc 09 fb dc 11 85 97 19 6a 0b 32

However, my project asks to "read blocks of data from a data file". Therefore I need to alter the main function to read the following arrays from a data file.

1
2
unsigned char CipherKey[16] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
unsigned char PlainText[16] = {0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};


Thinking this would be easy to do, I've come across a lot of problems and I don't know how to approach this.

Firstly, I don't know how to take each individual part in the data file to store into each byte in the array i.e. 0x2B into CipherKey[0] of the array.
Secondly, How do I deal with the fact that each byte is in hexidecimal? How should my data file look?

this is what my data file Input.dat looks like at the minute:

1
2
0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34 


the first line being the CipherKey array and the second being PlainText. I'm not sure if the '0x' or the commas are required.

I basically want to take the data from a file, store them in arrays in the program itself, encrypt the Input array PlainText and create a new file with the encrypted output array stored in it (I will deal with this part of the program after I get the input file done first).


I'm sorry if this is too long and I have attempted this but nothing is working. I appreciate any help I can get.
Last edited on
bump please
this is what my data file Input.dat looks like at the minute:
Who stores the data?

I'm not sure if the '0x' or the commas are required.
No, it's not required. You can read/write it binary

Who stores the data?

I'm not sure what you mean but what I mean is that the program will open the data file (Input.dat/Input.txt, I don't think it matters) using inFile.open(Input.txt) and extracts the first line of code and places each of the 16 characters (e.g. 0x2B) of the line into each byte of the array
 
unsigned char CipherKey[16];


and then the second line is placed into the array
 
unsigned char PlainText[16];


Once this is done the rest of my program will take these unsigned char Hexidecimal values and encrypts them.

Taking:
1
2
unsigned char CipherKey[16] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
unsigned char PlainText[16] = {0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};


from an external text file to encrypt instead of taking this in the program itself.
I'm not sure what you mean but what I mean is that the program will open the data file
Yes, but before you can open a file it must come somehow into existence. How this is done decides about the format. For instance if you're doing it by hand you won't be able to store the data as binary.

If you want hex you can read/write it with the stream:
http://www.cplusplus.com/reference/ios/hex/
I don't understand what you mean sorry.
Thank you for your help.

I've done something that to me looks right but I'm coming out with the wrong answer

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
void main()
{
	int a;

	unsigned char CipherKey[16]; 
	unsigned char PlainText[16];

	ifstream inFile;
	ofstream outFile;

	inFile.open("Input.dat"); // Opens input file with required info to place into arrays initialised above
	
	for(a=0;a<16;a++)
	{
		inFile >>  CipherKey[a]; // The first 16 unsigned chars in the file are placed in the array CipherKey[16]
// i.e. 0x2B is placed in CipherKey[0], 0xAE is placed in CipherKey[5] etc
	}

	for(a=0;a<16;a++)
	{
		inFile >>  PlainText[a]; // The last 16 unsigned chars in the file are placed in the array PlainText[16]
	}

	inFile.close();
...// Rest of code 


The answer should be : 39 25 84 1d 02 dc 09 fb dc 11 85 97 19 6a 0b 32
whereas I am getting : 60 a6 f1 d6 7a 14 32 1a 40 55 50 e8 0c fc 5f cc

I made a small program that uses this method to get the sum integer values in a file to see if this way could work with the AES program and it seems like it can.

This is with the data file looking like this:
1
2
0x2B 0x7E 0x15 0x16 0x28 0xAE 0xD2 0xA6 0xAB 0xF7 0x15 0x88 0x09 0xCF 0x4F 0x3C
0x32 0x43 0xF6 0xA8 0x88 0x5A 0x30 0x8D 0x31 0x31 0x98 0xA2 0xE0 0x37 0x07 0x34


I also altered the data file by getting rid of "0x" and writing my code like this:
1
2
3
4
5
for(a=0;a<16;a++)
	{
		inFile >> std::hex >> CipherKey[a];
	}
...

However it also gave me the wrong answer with the std::hex not changing the outcome of the answer.

Does this look like im along the right lines or way off the mark?
Last edited on
Since you're not telling anything about the input file I cannot tell why you're getting this values
I finally got it. thank you! I used the std::hex to convert the integer array to hex and placed it in the array similar to what I did above
Topic archived. No new replies allowed.