1. Cryptography in R
Introduction
In Cryptography, an Adversary is a malicious entity, which aims to retrieve precious information or data thereby undermining
the principles of information security. Data Confidentiality, Data Integrity, Authentication and Non-repudiation are core
principles of modern-day cryptography.
Cryptography and Network Security. Cryptography, which translates as "secret writing," refers to the science of concealing
the meaning of data so only specified parties understand a transmission's contents.
Network security protocols defend the network against malware, alteration of its programs or the deletion of important files.
The data that is encrypted using cryptology can only be decrypted using a specific key.
Three types of cryptography: secret-key, public key, and hash function.
Disclaimer
Use of losing data. Make sure to know what you do at any time. Only for educational purposes. Use only with dummy data.
Creating Keys
#### Tutorial of Keygen ####
# Used Libraries #
library(openssl)
library(tidyverse)
# Generate RSA Keys #
if(!exists("keys")){dir.create("keys")}
#Generate your private Keys
set.seed(1)
rsa_keygen(bits = 5099) %>%
write_pem(path = "keys/private.pem", password = "")
set.seed(1)
rsa_keygen(bits = 5099) %>%
write_pem(path = "keys/private1.pem", password = "")
#Generate your public Keys (N.B. optional. Use to encrypt/ decrypt)
read_key(file = "keys/private.pem", password = "") %>%
2. #extract element of the list and write to file
'[['("pubkey") %>% write_pem("keys/public.pub")
#Generate your public Keys (N.B. optional. Use to encrypt/ decrypt)
read_key(file = "keys/private1.pem", password = "") %>%
#extract element of the list and write to file
'[['("pubkey") %>% write_pem("keys/public1.pub")
### Understand RSA Keys ###
#read private key from file and output structure of the object
private_key = read_key("keys/private.pem", password = "")
private_key1 = read_key("keys/private1.pem", password = "")
#Structure of the key
str(private_key)
#Read public key from file
public_key = read_pubkey("keys/public.pub")
str(public_key)
private_key$pubkey$ssh
private_key1$pubkey$ssh
#Delete history and empty environment
rm(private_key)
Encryption
#### Tutorial of Encryption ####
#Encrypt the message with public key
"Hello World!" %>%
#serialize the object
3. serialize(connection = NULL) %>%
# encrypt the object
encrypt_envelope("keys/public.pub") %>%
#write encrypted data to file
write_rds("message.enc")
#Encrypt the message with private key
"Hello World!" %>%
#serialize the object
serialize(connection = NULL) %>%
# encrypt the object
encrypt_envelope("keys/private.pem") %>%
#write encrypted data to file
write_rds("message.enc")
Decryption
#### Tutorial of Decryption ####
#Decrypt the message with private key
secret_encrypted = read_rds("message.enc")
# Decrypt the list from R environment
decrypt_envelope(data = secret_encrypted$data,
iv = secret_encrypted$iv,
session = secret_encrypted$session,
key = "keys/private.pem", password = "") %>%
#unserialize ba ck original object
unserialize()
#removesecret_encrypted message
rm(secret_encrypted)
Hashing