|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +// AES 128bit = 16 bytes |
| 5 | +#define BYTES 16 |
| 6 | + |
| 7 | +int main(){ |
| 8 | + unsigned char plainText[BYTES]; // plaintext |
| 9 | + unsigned char key[BYTES]; // encryption key |
| 10 | + unsigned short int sizeCheck; // temporary saving the length of plaintext and key |
| 11 | + |
| 12 | + unsigned int plainTextHEX[BYTES]; // plaintext in hexadecimal |
| 13 | + unsigned int keyHEX[BYTES]; // key in hexadecimal |
| 14 | + |
| 15 | + int i; // counter for loops |
| 16 | + |
| 17 | + // read the text for encryption |
| 18 | + do{ |
| 19 | + printf("Text: "); |
| 20 | + gets(plainText); |
| 21 | + //scanf("%[^\n]s", plainText); |
| 22 | + |
| 23 | + // clear the input buffer |
| 24 | + fflush(stdin); |
| 25 | + |
| 26 | + // save the length of plaitext |
| 27 | + sizeCheck = strlen(plainText); |
| 28 | + }while(sizeCheck != BYTES); |
| 29 | + |
| 30 | + // print the plaintext as a string and in hexadecimal form |
| 31 | + printf("%s = ", plainText); |
| 32 | + for(i = 0; i < BYTES; i++){ |
| 33 | + // saving hexadecimal value to new array |
| 34 | + plainTextHEX[i] = plainText[i]; |
| 35 | + printf("%x ", plainTextHEX[i]); |
| 36 | + } |
| 37 | + |
| 38 | + // line feed |
| 39 | + printf("\n\n"); |
| 40 | + |
| 41 | + // read the key for encryption |
| 42 | + do{ |
| 43 | + printf("Key: "); |
| 44 | + gets(key); |
| 45 | + //scanf("%[^\n]s", key); |
| 46 | + |
| 47 | + // clear the input buffer |
| 48 | + fflush(stdin); |
| 49 | + |
| 50 | + // save the length of key |
| 51 | + sizeCheck = strlen(key); |
| 52 | + }while(sizeCheck != BYTES); |
| 53 | + |
| 54 | + // print the key as a string and in hexadecimal form |
| 55 | + printf("%s = ", key); |
| 56 | + for(i = 0; i < BYTES; i++){ |
| 57 | + // saving hexadecimal value to new array |
| 58 | + keyHEX[i] = key[i]; |
| 59 | + printf("%x ", keyHEX[i]); |
| 60 | + } |
| 61 | + |
| 62 | +return 0; |
| 63 | +} |
0 commit comments