<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Encryption :: CS2 course</title><link>https://robark.gitlab.io/encryption/index.html</link><description>Chapter 9 Encryption Symmetric AES and Asymmetric RSA</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 02 Feb 2021 22:59:32 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/encryption/index.xml" rel="self" type="application/rss+xml"/><item><title>Concepts</title><link>https://robark.gitlab.io/encryption/concepts/index.html</link><pubDate>Wed, 27 Jan 2021 19:47:44 +0000</pubDate><guid>https://robark.gitlab.io/encryption/concepts/index.html</guid><description>Lesson 31: Overview of Symmetric AES and Asymmetric RSA Recommended Videos: Diffie-Hellman Key Exchange
AES steps explained</description></item><item><title>AES</title><link>https://robark.gitlab.io/encryption/aes/index.html</link><pubDate>Thu, 28 Jan 2021 19:34:46 +0000</pubDate><guid>https://robark.gitlab.io/encryption/aes/index.html</guid><description>Lesson 32: AES (CBC and EAX mode) using pycryptodome Note: you should substitute your own desired file to encrypt in place of tux.png
CBC mode encrypt:
#AES CBC mode encryption from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes with open('tux.png', 'rb') as f: data = f.read() data = pad(data, AES.block_size) #pad to 16 bytes key = get_random_bytes(16) #using 128bit encryption #save aes key to aeskey file with open('aeskey','wb') as f: f.write(key) cipher = AES.new(key, AES.MODE_CBC) #creates iv automatically e_data = cipher.encrypt(data) with open('enc_data','wb') as f: f.write(cipher.iv) #16 bytes at the top of the file f.write(e_data) CBC mode decrypt:</description></item><item><title>RSA</title><link>https://robark.gitlab.io/encryption/rsa/index.html</link><pubDate>Fri, 29 Jan 2021 19:05:24 +0000</pubDate><guid>https://robark.gitlab.io/encryption/rsa/index.html</guid><description>Lesson 33: RSA asymmetric encryption using pycryptodome Generate public/private key pair:
from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA key=RSA.generate(2048) privkey= key.exportKey() pubkey= key.publickey().exportKey() with open('public.pem','wb') as f: f.write(pubkey) with open('private.pem','wb') as f: f.write(privkey) Encrypt using others public key:
from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA with open('public.pem') as f: #use others public key (not your own) pubkey= f.read() key = RSA.importKey(pubkey) cipher = PKCS1_OAEP.new(key) with open('secret.txt') as f: #note: secret.txt max size ~245 bytes s=f.read().encode() enc = cipher.encrypt(s) with open('secret.enc','wb') as f: f.write(enc) Decrypt using your own private key:</description></item><item><title>AES + RSA Hybrid</title><link>https://robark.gitlab.io/encryption/hybrid/index.html</link><pubDate>Mon, 01 Feb 2021 23:23:25 +0000</pubDate><guid>https://robark.gitlab.io/encryption/hybrid/index.html</guid><description>Lesson 34: AES + RSA Hybrid encryption Sender:
#hybrid eax mode sender from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA #create random aes symmetric key aeskey = get_random_bytes(16) #16 bytes = 128bit encryption #get recipients public rsa key with open('rpub.pem', 'rb') as f: pubkey= f.read() rsakey = RSA.importKey(pubkey) rsacipher = PKCS1_OAEP.new(rsakey) e_aeskey = rsacipher.encrypt(aeskey) #use aes key to encrypt file (AES symmetric) with open('tux.png', 'rb') as f: data = f.read() aescipher = AES.new(aeskey, AES.MODE_EAX) e_data, tag = aescipher.encrypt_and_digest(data) #write both encrypted aes key and encrypted file to one bundled file with open('bundle.enc', 'wb') as f: f.write(e_aeskey) #256 bytes f.write(aescipher.nonce) #16 bytes f.write(tag) #16 bytes f.write(e_data) Recipient:</description></item><item><title>End to End</title><link>https://robark.gitlab.io/encryption/end2end/index.html</link><pubDate>Tue, 02 Feb 2021 22:59:32 +0000</pubDate><guid>https://robark.gitlab.io/encryption/end2end/index.html</guid><description>Lesson 35: Simple End to End encryption Hybrid sender:
# rsa-aes eax sender import sys from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA if len(sys.argv) != 4: print(f'usage: python3 {sys.argv[0]} pubkey fileToEncrypt fileToSend') sys.exit() #create random aes symmetric key aeskey = get_random_bytes(16) #16 bytes = 128bit encryption #get recipients public rsa key with open(sys.argv[1], 'rb') as f: pubkey= f.read() rsakey = RSA.importKey(pubkey) rsacipher = PKCS1_OAEP.new(rsakey) e_aeskey = rsacipher.encrypt(aeskey) #use aes key to encrypt file (AES symmetric) with open(sys.argv[2], 'rb') as f: data = f.read() aescipher = AES.new(aeskey, AES.MODE_EAX) e_data, tag = aescipher.encrypt_and_digest(data) #write both encrypted aes key and encrypted file to one bundled file with open(sys.argv[3], 'wb') as f: f.write(e_aeskey) #256 bytes f.write(aescipher.nonce) #16 bytes f.write(tag) #16 bytes f.write(e_data) Hybrid reciever:</description></item></channel></rss>