Encryption (Symmetric, Asymmetric)

beginner
5 min

Encryption (Symmetric, Asymmetric)

Welcome to our comprehensive guide on Encryption! In this tutorial, we'll dive into the fascinating world of data security, learning about symmetric and asymmetric encryption. By the end, you'll have a solid understanding of these key concepts, ready to apply them in your coding projects.

What is Encryption? 🎯

Encryption is the process of converting plain text (readable data) into an unreadable format, known as ciphertext. This ensures that sensitive data remains secure during transmission or storage.

Why Encryption Matters? πŸ’‘

  • Protects data confidentiality by making it unreadable to unauthorized users
  • Prevents data tampering and ensures data integrity
  • Secures communication over insecure networks

Symmetric Encryption πŸ“

Symmetric encryption uses the same key for both encryption and decryption.

Types of Symmetric Encryption Algorithms βœ…

  1. Advanced Encryption Standard (AES)
  2. Data Encryption Standard (DES)
  3. RSA (not strictly a symmetric algorithm but often used for this purpose)

AES Example πŸ”‘

python
from Cryptodome.Cipher import AES from Cryptodome.Random import get_random_bytes # Generate a secret key key = get_random_bytes(32) # Create a new AES cipher object with the secret key cipher = AES.new(key, AES.MODE_EAX) # Encrypt the plaintext (replace 'your_plaintext' with your data) ciphertext, tag = cipher.encrypt_and_authenticate(b'your_plaintext', b'salt') # Decrypt the ciphertext using the same key plaintext = cipher.decrypt_and_authenticate(ciphertext, tag)

πŸ“ Note: Replace 'your_plaintext' with your data, and 'salt' with a random value for added security.

Asymmetric Encryption πŸ”

Asymmetric encryption uses two different keys: a public key for encryption and a private key for decryption.

RSA Algorithm πŸ”’

RSA (Rivest–Shamir–Adleman) is one of the most commonly used asymmetric encryption algorithms.

RSA Example πŸ”‘

python
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization, asymmetric from cryptography.hazmat.primitives.asymmetric import rsa # Generate RSA keys private_key = rsa.generate_private_key( backend=default_backend(), public_exponent=65537, key_size=2048 ) public_key = private_key.public_key() # Encrypt a message with the public key encrypted_message = public_key.encrypt( bytes('your_message', 'utf-8'), oversize=64 ) # Decrypt the message with the private key decrypted_message = private_key.decrypt(encrypted_message)

πŸ“ Note: Replace 'your_message' with your data.

Quiz Time! πŸ’‘

Quick Quiz
Question 1 of 1

What is the primary purpose of encryption?

Quick Quiz
Question 1 of 1

Which encryption method uses the same key for encryption and decryption?

Keep learning and exploring the fascinating world of computer networks and encryption! πŸš€