Cryptography is the art and science of securing communication and data by transforming it into an unreadable format, ensuring that only authorized parties can access or decipher the information. In an era of cyber threats and data breaches, cryptography is more crucial than ever in safeguarding sensitive information, such as passwords, financial transactions, and personal data.
Symmetric cryptography relies on a single secret key to both encrypt and decrypt data. This method is faster and more efficient than asymmetric cryptography but requires secure key exchange. If an attacker gains access to the secret key, the encryption is compromised.
Common Symmetric Algorithms:
Code Example: AES Encryption (Python)
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# Generate a random 256-bit key for AES
key = get_random_bytes(32) # 32 bytes = 256 bits
# Create AES cipher in CBC mode
cipher = AES.new(key, AES.MODE_CBC)
# Plaintext message to encrypt
plaintext = b"This is a secret message."
# Pad the plaintext to be a multiple of block size (AES block size is 16 bytes)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
# Decrypting the message
decipher = AES.new(key, AES.MODE_CBC, iv=cipher.iv)
decrypted = unpad(decipher.decrypt(ciphertext), AES.block_size)
print(f"Encrypted: {ciphertext.hex()}")
print(f"Decrypted: {decrypted.decode('utf-8')}")
Explanation:
Asymmetric cryptography uses two keys: one for encryption (public key) and one for decryption (private key). The keys are mathematically linked, but the private key is never shared, making it more secure for tasks such as key exchange.
Common Asymmetric Algorithms:
Code Example: RSA Encryption and Decryption (Python)
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Random import get_random_bytes
# Generate RSA key pair (2048-bit)
key = RSA.generate(2048)
# Public and private keys
public_key = key.publickey()
private_key = key
# Create RSA cipher for encryption and decryption
cipher_rsa = PKCS1_OAEP.new(public_key)
decipher_rsa = PKCS1_OAEP.new(private_key)
# Encrypting a message
message = b"Confidential information"
ciphertext = cipher_rsa.encrypt(message)
# Decrypting the message
decrypted_message = decipher_rsa.decrypt(ciphertext)
print(f"Encrypted: {ciphertext.hex()}")
print(f"Decrypted: {decrypted_message.decode('utf-8')}")
Explanation:
PKCS1_OAEP
cipher is used for encryption, which is a recommended padding scheme that ensures security against certain types of attacks.A hash function is a one-way mathematical function that takes an input (or "message") and returns a fixed-length string of characters, which is typically a hash value or checksum. Unlike encryption, hashing is not reversible, meaning you cannot retrieve the original input from the hash value. Hash functions are commonly used for data integrity checks, storing passwords securely, and digital signatures.
Common Hash Functions:
Code Example: SHA-256 Hashing (Python)
import hashlib
# Message to hash
message = "This is a secure message."
# Create SHA-256 hash object
hash_object = hashlib.sha256()
# Update the hash object with the message
hash_object.update(message.encode())
# Get the hexadecimal representation of the hash
hashed_message = hash_object.hexdigest()
print(f"Original message: {message}")
print(f"SHA-256 Hash: {hashed_message}")
Explanation:
hashlib
module to hash the message using the SHA-256 algorithm.hexdigest()
function returns the hash as a hexadecimal string.A digital signature is a cryptographic mechanism that verifies the authenticity and integrity of a message. It combines a hash function with public-key encryption to provide proof that the message came from the sender and hasn't been tampered with. Digital signatures are commonly used in email, software distribution, and blockchain transactions.
Code Example: Digital Signature (Python)
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
# Generate RSA key pair (2048-bit)
key = RSA.generate(2048)
# Create message and its hash
message = b"This is a signed message."
hash_message = SHA256.new(message)
# Create signature using private key
private_key = key
signature = pkcs1_15.new(private_key).sign(hash_message)
# Verify the signature with the public key
public_key = key.publickey()
try:
pkcs1_15.new(public_key).verify(hash_message, signature)
print("Signature is valid.")
except (ValueError, TypeError):
print("Signature is invalid.")
Explanation:
Whenever you see "https://" at the beginning of a URL, it indicates that the communication between your browser and the website's server is encrypted using SSL/TLS protocols. These protocols employ asymmetric cryptography for key exchange and symmetric cryptography for the actual data transfer.
How HTTPS Works:
Cryptocurrencies like Bitcoin rely on cryptography to secure transactions and control the creation of new units. Bitcoin uses a combination of: