Hi,
I'm writing a encrypting program that implements the AES algorithm and haven't use C for quite a while. The program first ask the user if they would like to cipher or decipher and depending on the input, the program will either cipher a plaintext or decipher a ciphertext. The user will have to enter a 16 byte key and plaintext/ciphertext in hex as the input. Is there a way to read from the command line a string of hex pairs that are entered in one line and store them in an array rather than just repeatedly prompting the user to enter each pair?
The key would be entered like this on the command line: 8e 73 b0 f7 da 0e 64 52 c8 10 f3 2b e3 45 2b f2 and each hex pair would be stored in an array key[]. So far, I was only able to come up with a for loop that scans each pair.
Code:
#include <stdio.h>
unsigned char choice;
unsigned char key[16], plaintext[16], cipher[16], state[4][4], roundKey[240];
void main() {
int i;
printf("Enter 'c' to cipher, 'd' to decipher: ");
while(choice != 'c' && choice != 'd') {
choice = getc(stdin);
switch(choice) {
case 'c':
printf("Enter the key: ");
/*get key*/
for(i=0;i<Nk*4;i++) {
scanf("%x", &key[i]);
}
printf("Please enter the plaintext you like to cipher: ");
break;
case 'd':
printf("Please enter the key to decipher: ");
break;
default:
getc(stdin); /*clear stdin of /n character*/
printf("Enter 'c' to cipher, 'd' to decipher: ");
break;
}
}
}
Right now, I'm just working on the ciphering portion so don't mind the deciphering portion
Thanks