Commit efc9d496 authored by Bhavesh Yadav's avatar Bhavesh Yadav

added hybrid encrypt function

parent 1781cf6f
d?9ʒp&Pd?9ʒp&Pd?9ʒp&Pd?9ʒp&Pd?9ʒp&Pd?9ʒp&Pd?9ʒp&Pd?9ʒp&P
\ No newline at end of file
......@@ -27,7 +27,7 @@ int main(int argc, char *argv[]) {
exit(1);
}
encrypt(argv[4],argv[1], argv[3]);
// encrypt(argv[4],argv[1], argv[3]);
hybrid_decrypt(argv[2], "decrypted_message.txt");
printf("output : %d\n",compare_files("message.txt","decrypted_message.txt"));
......
Nӽ34dBIȴ "*ymOH)|9\#ܱ6bko, y>
-sQѯo`K($&R7H>lu9
\ No newline at end of file
s*={r
jsƁ*_K-8~_
QUduP`8}D1AII}?
a`[npQGywW$Kxi(].BbzW7kSʭ45qmRUƣ=p `1vM*raO^Cbatx),A#_t簨j5Ta
pzXs\QeuT'O^+rH6P~-NGc zdWZX2&?VY!JxiG@z+`;5
\ No newline at end of file
gisQJ)ͺF0
\ No newline at end of file
......@@ -16,7 +16,6 @@ int padding = RSA_PKCS1_OAEP_PADDING;
RSA * createRSAWithFilename(char * filename,int public)
{
FILE * fp = fopen(filename,"rb");
if(fp == NULL)
{
printf("Unable to open file %s \n",filename);
......@@ -44,27 +43,138 @@ RSA * createRSAWithFilename(char * filename,int public)
// /* your code goes here */
// return result;
// }
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int encryptAES(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
/*
* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/*
* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decryptAES(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/*
* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
/*
* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary.
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/*
* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
void hybrid_encrypt(char * symmetric_key_file, char* message_file, char* public_key_file) {
puts("in hybrid file");
int result = 0;
RSA *rsa = createRSAWithFilename(public_key_file,0);
char symmKey[2048];
char encKey[2048];
RSA *rsa = createRSAWithFilename(public_key_file,1);
unsigned char * encKey = malloc(sizeof(RSA_size(rsa)));
FILE *fp = fopen(symmetric_key_file, "r");
puts("reading file");
fseek(fp, 0, SEEK_END);
size_t keyLen = ftell(fp);
int keyLen = ftell(fp);
rewind(fp);
fread(symmKey, keyLen, 1, fp);
puts(symmKey);
printf("%ld",keyLen);
puts("fread");
unsigned char* symmKey = malloc(sizeof(unsigned char)*keyLen + 1);
fread(symmKey, keyLen, 1, fp);
symmKey[keyLen]='\0';
result = RSA_public_encrypt(keyLen,symmKey,encKey,rsa,padding);
puts("result");
puts(encKey);
puts("enckey");
/* Init vector */
unsigned char iv[AES_BLOCK_SIZE];
for(int i = 0; i<AES_BLOCK_SIZE;i++)
iv[i] = rand();
fp = fopen(message_file, "r");
puts("reading file");
fseek(fp, 0, SEEK_END);
int msgLen = ftell(fp);
rewind(fp);
unsigned char* aes_input = malloc(sizeof(unsigned char)*msgLen + 1);
unsigned char* enc_out = malloc(sizeof(unsigned char)*msgLen*128 + 1);
fread(aes_input, msgLen, 1, fp);
int ciphertext_len = encryptAES (aes_input, strlen ((char *)aes_input), symmKey, iv,
enc_out);
enc_out[ciphertext_len]='\0';
FILE *C1 = fopen("C1","w");
FILE *C2 = fopen("C2","w");
FILE *IV = fopen("IV","w");
fprintf(C1,"%s",enc_out);
fprintf(C2,"%s",encKey);
fprintf(IV,"%s",iv);
fclose(C1);
fclose(C2);
fclose(IV);
fclose(fp);
}
int main(int argc, char *argv[]) {
......
Nӽ34dBIȴ "*ymOH)|9\#ܱ6bko, y>
-sQѯo`K($&R7H>lu9 ;ߝ
QX,.˃D_8rhˢ1{dt'c"\)_1=4am8٭6?B8bFtلmVU4b()i/&9%JA@N4:6;gisQJ)ͺFp
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment