[CT437]: Add Assignment 2
This commit is contained in:
Binary file not shown.
@ -0,0 +1,9 @@
|
||||
/* Create a header file for each source code file using the same name, but with a "*.h" extension. */
|
||||
/* Keep it in the same folder as the source code file, and include it via #include "file.h" */
|
||||
|
||||
#ifndef FILENAME /* This symbolic name is unique and should match the file name. */
|
||||
#define FILENAME /* This expression makes sure that this header file is only included once. */
|
||||
|
||||
/* Add all your function prototypes, macros, #defines, etc. below. */
|
||||
|
||||
#endif
|
Binary file not shown.
@ -0,0 +1,18 @@
|
||||
-----BEGIN ENCRYPTED PRIVATE KEY-----
|
||||
MIIC5TBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQQGu/gxc3FhYBp92+c
|
||||
BEpe2wICCAAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEAQIEEDxv/2oUKKtyHx5c
|
||||
2yvzEasEggKA4QcvHZ8/T2kaYp+UblFzFHbMWQLpeUjL/Z1c1hVi42gCcUZ74Tms
|
||||
LFosqZP77fiyVTSz04LBpCy9weu17uuwVTm/jWn8cNH2wVPRcOVaenkbJdOp1noC
|
||||
UM4JyHwst/6ixp1gMasnIAAN/3+n5PMQUjH2z5vq6bKvyii2GCfYMr2IeuS6ejDi
|
||||
2CFCWde+Xwp9Dpn5sFrEpV4D7GVmxgOEE0fkGgH5jpcu/MkB1jdcMeqeGRe29Kon
|
||||
obde0eaeZWg5veAfPR3A8gc3abBKgfz3P611e5CiYh0fVXaZMFlZoqmlkFjjFJiS
|
||||
CQfTE6JGi67EPg9PCz0owfZua0vUQyx0ysdRPEbBUGd2+xIelduo8AIrGAwBE54T
|
||||
MPqlpwCAOW+w03Y5+epe01s8MAB0XK/EyFRrLXBZKX9Bscqb8TLwlx8n6WXAsyWI
|
||||
dkaFulD4m7n/HQMcfqXFqS0FUGW5q7CIIBZtw9w3LJQ9YVmutoqa6tbRf+LyNeiH
|
||||
AA0HWfl/a71OMEvwsxNFX6ibpUFJ5CsLDclFflywqXzHLoUkQvOV4W7kQVJJILuQ
|
||||
X4OAZia+VqxaOSm9ZvW5vhSrFMmf62g0a1VYr0fAOOxF4s6pCPeEGQmqmZpvgaAF
|
||||
Wl7o+RTjkehs9v2q6Q7JUlR5l4WzjWbjmgeDRxQRgv4+PZPXVpTnca4E/zqcAc22
|
||||
xg3zjt9QA9tIybIs+uMo7eSa3LKBfIlDQmIVWLUraSvlgzfOfro1YL9s62sqb3+J
|
||||
K0pvo8cR+MQkBrNl/JYnZE7PHq555nQiEUobEbRHDcetOq720G27QzN7qHCiF1Uu
|
||||
NgZVLboGiZ8kGj9gsXhAY53GXLbJIp1fSg==
|
||||
-----END ENCRYPTED PRIVATE KEY-----
|
BIN
year4/semester2/CT437/assignments/assignment2/tutorial/openssl_encrypt
Executable file
BIN
year4/semester2/CT437/assignments/assignment2/tutorial/openssl_encrypt
Executable file
Binary file not shown.
@ -0,0 +1,50 @@
|
||||
// Compile this code via gcc -o openssl_encrypt openssl_encrypt.c -lssl -lcrypto
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/err.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void handleErrors(void) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext) {
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
int len;
|
||||
int ciphertext_len;
|
||||
|
||||
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
|
||||
|
||||
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
handleErrors();
|
||||
|
||||
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
|
||||
handleErrors();
|
||||
ciphertext_len = len;
|
||||
|
||||
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
|
||||
ciphertext_len += len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return ciphertext_len;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
|
||||
unsigned char *iv = (unsigned char *)"0123456789012345";
|
||||
unsigned char *plaintext = (unsigned char *)"This is a secret message";
|
||||
unsigned char ciphertext[128];
|
||||
|
||||
int ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
|
||||
|
||||
printf("PLaintext: %s\n", plaintext);
|
||||
printf("Ciphertext:\n");
|
||||
BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
@ -0,0 +1,86 @@
|
||||
// Compile this code via gcc -o openssl_encrypt_long openssl_encrypt_long.c -lssl -lcrypto
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/err.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void handleErrors(void) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext) {
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
int len;
|
||||
int ciphertext_len;
|
||||
|
||||
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
|
||||
|
||||
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
handleErrors();
|
||||
|
||||
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
|
||||
handleErrors();
|
||||
ciphertext_len = len;
|
||||
|
||||
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
|
||||
ciphertext_len += len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return ciphertext_len;
|
||||
}
|
||||
|
||||
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *plaintext) {
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
int len;
|
||||
int plaintext_len;
|
||||
|
||||
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
|
||||
|
||||
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
handleErrors();
|
||||
|
||||
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
|
||||
handleErrors();
|
||||
plaintext_len = len;
|
||||
|
||||
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
|
||||
plaintext_len += len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return plaintext_len;
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
const int LEN = 100000000;
|
||||
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
|
||||
unsigned char *iv = (unsigned char *)"0123456789012345";
|
||||
unsigned char *plaintext = (unsigned char *) malloc(LEN);
|
||||
unsigned char *ciphertext = (unsigned char *) malloc(LEN + 100);
|
||||
|
||||
int decryptedtext_len;
|
||||
unsigned char *decryptedtext = (unsigned char *) malloc(LEN + 100);
|
||||
|
||||
memset(plaintext, 0x72, LEN);
|
||||
plaintext[LEN - 1] = 0;
|
||||
printf("Start\n");
|
||||
int ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
|
||||
|
||||
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
|
||||
decryptedtext[decryptedtext_len - 1] = '\0';
|
||||
|
||||
printf("End\n");
|
||||
|
||||
// Print the first 100 characters of the original and decoded text.
|
||||
printf("Original plaintext: %.100s\nDecoded ciphertext: %.100s\n", plaintext, decryptedtext);
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDm822B5kbggiD6c3T5z1/V+Io3
|
||||
HgrVyHnd9mLHHJtv6gryfqwBYylSMabJnET85f4HYa/JtW0D4m8keQGNPxlRSqOD
|
||||
caNrwVYYQ27O2TEB+9Lqkr6bUv1ChVgO2D0nwYDCA5a50qWLKZrvp/xSOb3l8owk
|
||||
JFcgvsmffjm7sSM+DQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
Reference in New Issue
Block a user