-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAES_test.c
125 lines (104 loc) · 3.81 KB
/
SAES_test.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**********************************************************************
* SAES_test.c
*
* My test of Simplified AES Implementation
*
* Created on: Nov 18, 2023
*
* Author: Mostafa Mahmoud Ali Ahmed
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SAES.h"
/********************************* Main Helper Functions ***********************************/
boolean Character_isHexaDecimal(char ch)
{
return ((ch >= '0' && ch <= '9') ||
(ch >= 'a' && ch <= 'f') ||
(ch >= 'A' && ch <= 'F'));
}
/********************************* Main Function ***************************************/
int main(int argc, char** argv)
{
// Check if the number of parameters passed from commandline is valid or not
if (argc != 4)
{
printf("ERROR: Invalid number of parameters\nExpects: <ENC|DEC> <key> <data>\n");
return 1; // Exiting early with an error code
}
char* operation = argv[1];
// Check if the operation is valid
if (strcmp(operation, "ENC") != 0 && strcmp(operation, "DEC") != 0)
{
printf("ERROR: Invalid Operation\n");
return 1; // Exiting early with an error code
}
char* keyString = argv[2];
// Check if the keyString length is not equal to 4
if (strlen(keyString) != 4)
{
printf("ERROR: Invalid Key length\n");
return 1; // Exiting early with an error code
}
// Check if the keyString contains only valid hexadecimal characters
for (int i = 0; keyString[i] != '\0'; i++)
{
if (!Character_isHexaDecimal(keyString[i]))
{
printf("ERROR: Key contains non-hex characters\n");
return 1; // Exiting early with an error code
}
}
// Convert keyString to a uint16 value
uint16 key = (uint16)strtoul(keyString, NULL, 16); // 16 for hexadecimal base
if (strcmp(operation, "ENC") == 0)
{
char* plainTextString = argv[3];
// Check if the plainTextString length is not equal to 4
if (strlen(plainTextString) != 4)
{
printf("ERROR: Invalid Data length\n");
return 1; // Exiting early with an error code
}
// Check if the plainTextString contains only valid hexadecimal characters
for (int i = 0; plainTextString[i] != '\0'; i++)
{
if (!Character_isHexaDecimal(plainTextString[i]))
{
printf("ERROR: Data contains non-hex characters\n");
return 1; // Exiting early with an error code
}
}
// Convert plainTextString to a uint16 value
uint16 plaintext = (uint16)strtoul(plainTextString, NULL, 16); // 16 for hexadecimal base
// Proceed with encryption...
uint16 ciphertext = SAES_Encryption(plaintext,key);
printf("%04X\n", ciphertext);
}
else if (strcmp(operation, "DEC") == 0)
{
char* cipherTextString = argv[3];
// Check if the cipherTextString length is not equal to 4
if (strlen(cipherTextString) != 4)
{
printf("ERROR: Invalid Data length\n");
return 1; // Exiting early with an error code
}
// Check if the cipherTextString contains only valid hexadecimal characters
for (int i = 0; cipherTextString[i] != '\0'; i++)
{
if (!Character_isHexaDecimal(cipherTextString[i]))
{
printf("ERROR: Data contains non-hex characters\n");
return 1; // Exiting early with an error code
}
}
// Convert cipherTextString to a uint16 value
uint16 ciphertext = (uint16)strtoul(cipherTextString, NULL, 16); // 16 for hexadecimal base
// Proceed with decryption...
uint16 plaintext = SAES_Decryption(ciphertext, key);
printf("%04X\n", plaintext);
}
return 0;
}