Skip to content

Commit 699db12

Browse files
committed
client modified to take encryption method from cl, good to merge
1 parent 36a3527 commit 699db12

File tree

2 files changed

+79
-35
lines changed

2 files changed

+79
-35
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ gcc *.c -w -g -lm -o aes256
77

88
# usage
99

10-
`./aes256` Run with the data and key from the NIST FIPS 197 document Appendix C.3 example.
10+
`./aes256 -ecb [Message to encrypt] [256 bit key in 02x hex]` Encrypt data using AES256-ECB
1111

12-
`./aes256 [Message to encrypt]` Run with data from command line.
13-
14-
`./aes256 [Message to encrypt] [Key in 02x hex]` Run with data and key from command line.
12+
`./aes256 -cbc [Message to encrypt] [256 bit key in 02x hex] [128 bit iv in 02x hex]` Encrypt data using AES256-CBC
1513

1614
# note
17-
If the data and key from the command line didn't tip you off, don't use this for anything that you actually want to encrypt. This implementation was an educational exercise and is slow, probably naive, and vulnerable to all of the cryptographic no-nos. Use [OpenSSL](https://github.com/openssl/openssl) for a free, open-source, and infinitely more secure encryption option.
15+
If the data and key from the command line didn't tip you off, don't use this for anything that you actually want to encrypt. This implementation was an educational exercise and is slow, probably naive, and vulnerable to all of the cryptographic no-nos. Use [OpenSSL](https://github.com/openssl/openssl) for a fast, free, open-source, and infinitely more secure encryption option.

main.c

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,89 @@ int main(int argc, char *argv[]){
7373

7474
char * message = "";
7575

76-
if(argc == 4){
77-
message = argv[1];
78-
char *keystring = argv[2];
79-
if(strlen(keystring) != 64){
80-
printf("key must be 32 bytes (%d)\n",strlen(keystring)/2);
76+
if(strcmp(argv[1], "-ecb") == 0){
77+
if(argc == 4){
78+
message = argv[2];
79+
char *keystring = argv[3];
80+
if(strlen(keystring) != 64){
81+
printf("key must be 32 bytes\n");
82+
exit(-1);
83+
}
84+
parse_02X(key,keystring,32);
85+
}else{
86+
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex]\n",argv[0],argv[0]);
8187
exit(-1);
8288
}
83-
parse_02X(key,keystring,32);
84-
char *ivstring = argv[3];
85-
if(strlen(ivstring) != 32){
86-
printf("iv must be 16 bytes (%d)\n",strlen(ivstring)/2);
89+
90+
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
91+
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
92+
93+
if(byteBuffer == NULL || returnBuffer == NULL){
94+
printf("error allocating buffers\n");
95+
return(-1);
96+
}
97+
98+
uint32_t returnlen = 0;
99+
100+
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
101+
str_to_bytes(message, byteBuffer);
102+
printf("Message is... \n%s\n", message);
103+
AES256MainECB(key, byteBuffer, strlen(message), returnBuffer, &returnlen, true);
104+
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
105+
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
106+
AES256MainECB(key, returnBuffer, returnlen, newBuffer, &returnlen, false);
107+
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
108+
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
109+
}else if(strcmp(argv[1], "-cbc") == 0){
110+
if(argc == 5){
111+
message = argv[2];
112+
char *keystring = argv[3];
113+
if(strlen(keystring) != 64){
114+
printf("key must be 32 bytes (%d)\n",strlen(keystring)/2);
115+
exit(-1);
116+
}
117+
parse_02X(key,keystring,32);
118+
char *ivstring = argv[4];
119+
if(strlen(ivstring) != 32){
120+
printf("iv must be 16 bytes (%d)\n",strlen(ivstring)/2);
121+
exit(-1);
122+
}
123+
parse_02X(iv,ivstring,16);
124+
}else{
125+
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex] [IV in 02x hex]\n",argv[0]);
87126
exit(-1);
88127
}
89-
parse_02X(iv,ivstring,16);
128+
129+
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
130+
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
131+
132+
if(byteBuffer == NULL || returnBuffer == NULL){
133+
printf("error allocating buffers\n");
134+
return(-1);
135+
}
136+
137+
uint32_t returnlen = 0;
138+
139+
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
140+
printf("IV is...\n%s", bytes_to_hexstr(iv, 16));
141+
str_to_bytes(message, byteBuffer);
142+
printf("Message is... \n%s\n", message);
143+
AES256MainCBC(key, byteBuffer, iv, strlen(message), returnBuffer, &returnlen, true);
144+
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
145+
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
146+
AES256MainCBC(key, returnBuffer, iv, returnlen, newBuffer, &returnlen, false);
147+
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
148+
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
149+
}else if(strcmp(argv[1], "-ctr") == 0){
150+
printf("Not yet implemented.\n");
151+
exit(-1);
90152
}else{
91-
printf("Usage:\n%s [Message to encrypt] [Key in 02x hex] [IV in 02x hex]\n",argv[0]);
153+
printf("Supported Modes:\n");
154+
printf("-ecb\n");
155+
printf("-cbc\n");
156+
printf("-ctr\n");
92157
exit(-1);
93158
}
94159

95-
uint8_t *byteBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
96-
uint8_t *returnBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
97-
98-
if(byteBuffer == NULL || returnBuffer == NULL){
99-
printf("error allocating buffers\n");
100-
return(-1);
101-
}
102160

103-
uint32_t returnlen = 0;
104-
105-
printf("Key is...\n%s", bytes_to_hexstr(key, 32));
106-
printf("IV is...\n%s", bytes_to_hexstr(iv, 16));
107-
str_to_bytes(message, byteBuffer);
108-
printf("Message is... \n%s\n", message);
109-
AES256MainCBC(key, byteBuffer, iv, strlen(message), returnBuffer, &returnlen, true);
110-
printf("Encrypted data is...\n%s",bytes_to_hexstr(returnBuffer, returnlen));
111-
uint8_t *newBuffer = (uint8_t*)malloc((uint32_t)MAX_MSG_LGTH);
112-
AES256MainCBC(key, returnBuffer, iv, returnlen, newBuffer, &returnlen, false);
113-
printf("Unencrypted data is...\n%s",bytes_to_hexstr(newBuffer, returnlen));
114-
printf("Unencrypted message is...\n%s\n",bytes_to_str(newBuffer, returnlen));
115161
}

0 commit comments

Comments
 (0)