├── .gitignore ├── Dockerfile ├── Makefile ├── openssl_rsa.h ├── openssl_rsa.cpp ├── main.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | 3 | *.o 4 | *.txt 5 | *.bin 6 | *.patch 7 | 8 | private_key 9 | public_key 10 | 11 | rsa_encrypt_decrypt 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | LABEL maintainer = "Everybody :)" 3 | 4 | COPY . /usr/src/openssl_app 5 | WORKDIR /usr/src/openssl_app 6 | 7 | RUN apk update && \ 8 | apk upgrade && \ 9 | apk --update add \ 10 | g++ \ 11 | make \ 12 | openssl-dev \ 13 | bash \ 14 | rm -rf /var/cache/apk/* && \ 15 | make 16 | 17 | ENTRYPOINT ["./rsa_encrypt_decrypt"] -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OBJECTS += \ 2 | main.o \ 3 | openssl_rsa.o \ 4 | 5 | TERGET = rsa_encrypt_decrypt 6 | 7 | LIB += -lcrypto 8 | 9 | CFLAG += -c -fPIC -w 10 | 11 | EXTRA_GEN_FILE += \ 12 | decrypted_file.txt \ 13 | encrypted_file.bin \ 14 | private_key \ 15 | public_key \ 16 | 17 | 18 | all: ${OBJECTS} 19 | g++ ${OBJECTS} -o ${TERGET} ${LIB} 20 | 21 | %.o:%.cpp 22 | g++ ${CFLAG} $< -o $@ $(LIB) 23 | 24 | clean: 25 | rm -rf ${OBJECTS} ${TERGET} 26 | 27 | deep_clean: 28 | rm -rf ${OBJECTS} ${TERGET} ${EXTRA_GEN_FILE} 29 | -------------------------------------------------------------------------------- /openssl_rsa.h: -------------------------------------------------------------------------------- 1 | /* 2 | * @author Batuhan AVLAYAN - b.avlayan@gmail.com 3 | * @version V1.0 4 | * @brief This file declares a programming API for OpenSSL RSA encryption and decryption operations. 5 | */ 6 | 7 | #ifndef RSA_ALGORITHM_H 8 | #define RSA_ALGORITHM_H 9 | 10 | #define KEY_LENGTH 2048 11 | #define PUBLIC_EXPONENT 59 //Public exponent should be a prime number. 12 | #define PUBLIC_KEY_PEM 1 13 | #define PRIVATE_KEY_PEM 0 14 | 15 | #define LOG(x) \ 16 | cout << x << endl; \ 17 | 18 | /* 19 | * @brief create_RSA function creates public key and private key file 20 | * 21 | */ 22 | RSA * create_RSA(RSA *keypair, int pem_type, char *file_name); 23 | 24 | /* 25 | * @brief public_ecrypt function encrypts data. 26 | * @return If It is fail, return -1 27 | */ 28 | int public_encrypt(int flen, unsigned char* from, unsigned char *to, RSA* key, int padding); 29 | 30 | /* 31 | * @brief private_decrypt function decrypt data. 32 | * @return If It is fail, return -1 33 | */ 34 | int private_decrypt(int flen, unsigned char* from, unsigned char *to, RSA* key, int padding); 35 | 36 | /* 37 | * @brief create_ecrypted_file function creates .bin file. It contains encrypted data. 38 | */ 39 | void create_encrypted_file(char* encrypted, RSA * key_pair); 40 | 41 | #endif //RSA_ALGORITHM_H 42 | -------------------------------------------------------------------------------- /openssl_rsa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "openssl_rsa.h" 7 | 8 | using namespace std; 9 | 10 | RSA * create_RSA(RSA * keypair, int pem_type, char *file_name) { 11 | 12 | RSA *rsa = NULL; 13 | FILE *fp = NULL; 14 | 15 | if(pem_type == PUBLIC_KEY_PEM) { 16 | 17 | fp = fopen(file_name, "w"); 18 | PEM_write_RSAPublicKey(fp, keypair); 19 | fclose(fp); 20 | 21 | fp = fopen(file_name, "rb"); 22 | PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL); 23 | fclose(fp); 24 | 25 | } 26 | else if(pem_type == PRIVATE_KEY_PEM) { 27 | 28 | fp = fopen(file_name, "w"); 29 | PEM_write_RSAPrivateKey(fp, keypair, NULL, NULL, NULL, NULL, NULL); 30 | fclose(fp); 31 | 32 | fp = fopen(file_name, "rb"); 33 | PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL); 34 | fclose(fp); 35 | 36 | } 37 | 38 | return rsa; 39 | } 40 | 41 | int public_encrypt(int flen, unsigned char* from, unsigned char* to, RSA* key, int padding) { 42 | 43 | int result = RSA_public_encrypt(flen, from, to, key, padding); 44 | return result; 45 | } 46 | 47 | int private_decrypt(int flen, unsigned char* from, unsigned char* to, RSA* key, int padding) { 48 | 49 | int result = RSA_private_decrypt(flen, from, to, key, padding); 50 | return result; 51 | } 52 | 53 | void create_encrypted_file(char* encrypted, RSA* key_pair) { 54 | 55 | FILE* encrypted_file = fopen("encrypted_file.bin", "w"); 56 | fwrite(encrypted, sizeof(*encrypted), RSA_size(key_pair), encrypted_file); 57 | fclose(encrypted_file); 58 | } 59 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "openssl_rsa.h" 8 | 9 | using namespace std; 10 | 11 | 12 | int main() { 13 | 14 | LOG("OpenSSL_RSA has been started."); 15 | 16 | RSA *private_key; 17 | RSA *public_key; 18 | 19 | char message[KEY_LENGTH / 8] = "Plain text"; 20 | char *encrypt = NULL; 21 | char *decrypt = NULL; 22 | 23 | RSA *keypair = NULL; 24 | BIGNUM *bne = NULL; 25 | int ret = 0; 26 | 27 | char private_key_pem[12] = "private_key"; 28 | char public_key_pem[11] = "public_key"; 29 | 30 | LOG(KEY_LENGTH); 31 | LOG(PUBLIC_EXPONENT); 32 | 33 | // RSA *keypair = RSA_generate_key(KEY_LENGTH, PUBLIC_EXPONENT, NULL, NULL); //Old 34 | 35 | bne = BN_new(); 36 | ret = BN_set_word(bne, PUBLIC_EXPONENT); 37 | if (ret != 1) { 38 | // goto free_stuff; 39 | LOG("An error occurred in BN_set_word() method"); 40 | } 41 | keypair = RSA_new(); 42 | ret = RSA_generate_key_ex(keypair, KEY_LENGTH, bne, NULL); 43 | if (ret != 1) { 44 | // goto free_stuff; 45 | LOG("An error occurred in RSA_generate_key_ex() method"); 46 | } 47 | LOG("Generate key has been created."); 48 | 49 | private_key = create_RSA(keypair, PRIVATE_KEY_PEM, private_key_pem); 50 | LOG("Private key pem file has been created."); 51 | 52 | public_key = create_RSA(keypair, PUBLIC_KEY_PEM, public_key_pem); 53 | LOG("Public key pem file has been created.");; 54 | 55 | encrypt = (char*)malloc(RSA_size(public_key)); 56 | int encrypt_length = public_encrypt(strlen(message) + 1, (unsigned char*)message, (unsigned char*)encrypt, public_key, RSA_PKCS1_OAEP_PADDING); 57 | if(encrypt_length == -1) { 58 | LOG("An error occurred in public_encrypt() method"); 59 | } 60 | LOG("Data has been encrypted."); 61 | 62 | create_encrypted_file(encrypt, public_key); 63 | LOG("Encrypted file has been created."); 64 | 65 | decrypt = (char *)malloc(encrypt_length); 66 | int decrypt_length = private_decrypt(encrypt_length, (unsigned char*)encrypt, (unsigned char*)decrypt, private_key, RSA_PKCS1_OAEP_PADDING); 67 | if(decrypt_length == -1) { 68 | LOG("An error occurred in private_decrypt() method"); 69 | } 70 | LOG("Data has been decrypted."); 71 | 72 | FILE *decrypted_file = fopen("decrypted_file.txt", "w"); 73 | fwrite(decrypt, sizeof(*decrypt), decrypt_length - 1, decrypted_file); 74 | fclose(decrypted_file); 75 | LOG("Decrypted file has been created."); 76 | 77 | RSA_free(keypair); 78 | free(private_key); 79 | free(public_key); 80 | free(encrypt); 81 | free(decrypt); 82 | BN_free(bne); 83 | LOG("OpenSSL_RSA has been finished."); 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Encrypt-Decrypt-with-OpenSSL-RSA 2 | 3 | ## What is OpenSSL ? 4 | OpenSSL is opensource library that provide secure communication over networks using TLS (Transfer Secure Layer) and SSL (Secure Socket Layer). 5 | It supports many cryptographic algorithm AES, DSA, RSA, SHA1, SHA2, MD5.. More information about [OpenSSL](https://en.wikipedia.org/wiki/OpenSSL) 6 | 7 | ## What is RSA ? 8 | RSA is algorithm using for encrypting and decrypting data. 9 | It is in the class of asymmetric cryptographic algorithm (public key cryptography). 10 | Asymmetric cryptographic algorithm has two different keys. 11 | They are *public key and private key*. Public key is given everyone. 12 | Private key is secret. 13 | Data is encrypted by public key then decrypted by private key. 14 | More information about [RSA Algorithm](https://simple.wikipedia.org/wiki/RSA_(algorithm)) 15 | 16 | ## Steps of RSA Algorithm 17 | **1 -** Define two different prime numbers. ( p and q)
18 | **2 -** Calculate modulus for private key and public key. n = p * q
19 | **3 -** Caluclate totient. Q(n) = (p -1) * (q -1)
20 | **4 -** Define public key exponent (e). e must be in 1 < e < Q(n). e and Q(n) are relatively prime.
21 | **5 -** Define private key exponent (d). It must be secret. d*e = 1 + kQ(n). d must be in 1 < d < Q(n)
22 | 23 | ### Encrypt Message 24 | 25 | c = m^e mod (n) 26 | 27 | ### Decrypt Message 28 | 29 | m = c^d mod (n) 30 | 31 | ### Sample 32 | 33 | **1 -** p = 3 and q = 11
34 | **2 -** modulus n = 3 * 11 = 33
35 | **3 -** totient Q(n) = (3 - 1) * (11 - 1) = 20
36 | **4 -** 1 < e < 20 and e = 7
37 | **5 -** de mod Q(n) = 1 and 7d mod 20 = 1, d = 3
38 |
39 | Message can be 4. m = 4
40 | **Encrypt message:** c = 4^7 mod (33) = 16384 mod (33) and c = 16. Encrypted message is 16
41 | **Decrypt message:** m = 16^3 mod (33) = 4096 mod (33) and m = 4. Decrypted message is 4
42 | 43 | ## Project Code 44 | This project encrypts and decrypts message in a simple way. Let's examine *openssl_rsa.h* file.
45 | 46 | `create_RSA` function creates public_key.pem and private_key.pem file. Public_key.pem file is used to encrypt message. Private_key.pem file is used to decrypt message.
47 | 48 | `public_encrypt` function encrypts message using public_key.pem file
49 | 50 | `private_decrypt` function decrypts encrypted message using private_key.pem
51 | 52 | `create_encrypted_file` function creates encryted file as .bin file. 53 | 54 | ## Compilation Process 55 | For compile the entire project 56 | ```sh 57 | make 58 | ``` 59 | 60 | For clean the project directory 61 | ```sh 62 | make clean 63 | ``` 64 | 65 | For clean the project directory and remove generated files during execution 66 | ```sh 67 | make deep_clean 68 | ``` 69 | 70 | ## Docker build and run 71 | 72 | For docker build to create an image named opensslapp 73 | ``` 74 | docker build --tag opensslapp . 75 | ``` 76 | 77 | For run image as container 78 | ``` 79 | docker run -i -t --rm opensslapp 80 | ``` --------------------------------------------------------------------------------