Reputation:
I want to write one simple a program that will encrypt one test using <openssl/aes.h>
and at the same time decrypt it. I wrote below program
Adding my whole code here:
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <string.h>
int main(void)
{
//encryption testing
unsigned char inputb[2048] = {'\0'};
unsigned char encpb[2048]= {'\0'};
unsigned char opb[2048]= {'\0'};
#define MAX_SIZE 100
unsigned char oneKey[] = "6BC1BEE22E409F96E93D7E117393172A";
AES_KEY key;
AES_KEY key1;
char testchat[] = "!!!test doctors file!!! @Hospitan name(norman) SICKAPP_NAME=9873471093 @Duration (Duration\
of doctor visitdfwhedf in months)higibujiji TESTATION=-5 #Expiry date MADICINE_NAME=678041783478\n";
char NULL_byte[16] = {0};
memcpy((char*)inputb, (testchat), strlen(testchat)+1);
printf("\n\ninputb= %s strlen(testchat)=%d \n\n",inputb, strlen(testchat));
AES_set_encrypt_key(oneKey, 128, &key);
unsigned char tmp_char[50] = {'\0'};
char* pChar = (char*)inputb;
unsigned char tmp_char_encpb[MAX_SIZE];
while(*pChar != '\0') {
memset(tmp_char, '\0', 50);
memset(tmp_char_encpb, '\0', MAX_SIZE);
if(strlen(pChar) < 16) {
strncpy((char*)tmp_char, (char*)pChar, strlen(pChar)+1);
strncat((char*)tmp_char, NULL_byte, 16 - strlen(pChar)+1);
}
else
strncpy((char*)tmp_char, (char*)pChar, 16);
printf("Line:%d tmp_char = %s pChar=%d\n", __LINE__, tmp_char, strlen(pChar));
AES_encrypt(tmp_char, tmp_char_encpb, &key);
strcat((char*)encpb, (char*)tmp_char_encpb);
pChar += 16;
}
printf("len encpb=%d\n", strlen((char*)encpb));
//now test with decrypting and check if all okk....
unsigned char oneKey1[] = "6BC1BEE22E409F96E93D7E117393172A";
AES_set_decrypt_key(oneKey1,128,&key1);
unsigned char tmp_char_dencpb[MAX_SIZE];
pChar = (char*)encpb;
while(*pChar != '\0') {
memset(tmp_char, '\0', 50);
if(strlen(pChar) < 16) {
strncpy((char*)tmp_char, (char*)pChar, strlen(pChar)+1);
strncat((char*)tmp_char, NULL_byte, 16 - strlen(pChar)+1);
}
else
strncpy((char*)tmp_char, (char*)pChar, 16);
AES_decrypt(tmp_char, tmp_char_dencpb, &key1);
strncat((char*)opb, (char*)tmp_char_dencpb,16);
memset(tmp_char_dencpb, '\0', MAX_SIZE);
pChar += 16;
}
printf("\n\nopb = %s\n\n",opb);
return 0;
}
I am building via:
g++ mytest.cpp -lssl -lcrypto
running through GDB:
Program received signal SIGSEGV, Segmentation fault.
0x0000003e48437122 in ____strtoll_l_internal () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6_2.12.x86_64 keyutils-libs-1.4-3.el6.x86_64 krb5-libs-1.9-22.el6_2.1.x86_64 libcom_err-1.41.12-11.el6.x86_64 libgcc-4.4.6-3.el6.x86_64 libselinux-2.0.94-5.2.el6.x86_64 libstdc++-4.4.6-3.el6.x86_64 openssl-1.0.0-20.el6_2.4.x86_64 zlib-1.2.3-27.el6.x86_64
(gdb) backtrace
#0 0x0000003e48437122 in ____strtoll_l_internal () from /lib64/libc.so.6
#1 0x0000000000400e9b in GetExpiryDate (exp_date=0x7fffffffd970) at LicReader.cpp:66
#2 0x0000000000400eeb in IsLicenseExpired () at LicReader.cpp:74
#3 0x0000000000400f3b in main (argc=1, argv=0x7fffffffda88) at LicReader.cpp:86
(gdb)
OP: in out put some time I got current decrypted string and some time getting with junk character.(when i/p string changed)
Am I missed something anywhere? Can anyone tell why AES_decrypt not workin gsometimes?
Upvotes: 0
Views: 117
Reputation: 5449
Zero-terminated string manipulation is not how to manage encrypted data... for example you're using strcat
to add encrypted data to encpb
... but what happens if there's a zero in the encrypted data? What happens is you don't get all the data. Deal instead with the actual block-size which is 16 bytes. What happens if the data you encrypt is not a multiple of 16 bytes? You have to pad it out to a multiple of 16. How? Lots of different ways, like PKCS7. Plus you should look into cipher-block-chaining and salting... lots to learn!
Upvotes: 1