Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cryptography-cs742
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
BHAVESHKUMAR SHYAMSUNDAR YADAV
cryptography-cs742
Commits
efc9d496
Commit
efc9d496
authored
Nov 06, 2019
by
Bhavesh Yadav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added hybrid encrypt function
parent
1781cf6f
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
138 additions
and
15 deletions
+138
-15
week6/programmingExercises/making faster/test_decrypt/C1
week6/programmingExercises/making faster/test_decrypt/C1
+0
-0
week6/programmingExercises/making faster/test_decrypt/C2
week6/programmingExercises/making faster/test_decrypt/C2
+1
-0
week6/programmingExercises/making faster/test_decrypt/IV
week6/programmingExercises/making faster/test_decrypt/IV
+1
-0
week6/programmingExercises/making faster/test_decrypt/hybrid_test_decrypt.c
...xercises/making faster/test_decrypt/hybrid_test_decrypt.c
+1
-1
week6/programmingExercises/making faster/test_decrypt/testhybriddecrypt
...ingExercises/making faster/test_decrypt/testhybriddecrypt
+0
-0
week6/programmingExercises/making faster/test_encrypt/C1
week6/programmingExercises/making faster/test_encrypt/C1
+2
-0
week6/programmingExercises/making faster/test_encrypt/C2
week6/programmingExercises/making faster/test_encrypt/C2
+5
-0
week6/programmingExercises/making faster/test_encrypt/IV
week6/programmingExercises/making faster/test_encrypt/IV
+1
-0
week6/programmingExercises/making faster/test_encrypt/hybrid_test_encrypt.c
...xercises/making faster/test_encrypt/hybrid_test_encrypt.c
+124
-14
week6/programmingExercises/making faster/test_encrypt/output
week6/programmingExercises/making faster/test_encrypt/output
+3
-0
week6/programmingExercises/making faster/test_encrypt/testhybridencrypt
...ingExercises/making faster/test_encrypt/testhybridencrypt
+0
-0
No files found.
week6/programmingExercises/making faster/test_decrypt/C1
0 → 100644
View file @
efc9d496
File added
week6/programmingExercises/making faster/test_decrypt/C2
0 → 100644
View file @
efc9d496
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
week6/programmingExercises/making faster/test_decrypt/IV
0 → 100644
View file @
efc9d496
j
\ No newline at end of file
week6/programmingExercises/making faster/test_decrypt/hybrid_test_decrypt.c
View file @
efc9d496
...
...
@@ -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"
));
...
...
week6/programmingExercises/making faster/test_decrypt/testhybriddecrypt
0 → 100755
View file @
efc9d496
File added
week6/programmingExercises/making faster/test_encrypt/C1
0 → 100644
View file @
efc9d496
Nӽ34dBIȴ "*ymOH)|9\#ܱ6bko, y>
-sQѯo`K($&R7H>lu9
\ No newline at end of file
week6/programmingExercises/making faster/test_encrypt/C2
0 → 100644
View file @
efc9d496
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
week6/programmingExercises/making faster/test_encrypt/IV
0 → 100644
View file @
efc9d496
gisQJ)ͺF0
\ No newline at end of file
week6/programmingExercises/making faster/test_encrypt/hybrid_test_encrypt.c
View file @
efc9d496
...
...
@@ -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
);
in
t
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
[])
{
...
...
week6/programmingExercises/making faster/test_encrypt/output
0 → 100644
View file @
efc9d496
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
week6/programmingExercises/making faster/test_encrypt/testhybridencrypt
View file @
efc9d496
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment