A renoir painting of an old man listening to a computer with a s

Basics of Cryptographic Systems

In the universe of cryptography, explore the basics of symmetric and asymmetric encryption, hash functions, digital signatures, and cryptographic protocols. Discover the building blocks of secure communication.

July 2, 2023· 3 min read
9 score

The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards

  • Gene Spafford

In the universe of cryptography, there exist fundamental building blocks, commonly referred to as "primitives." Think of them as the elementary particles of cryptography, the bricks that build the walls of cryptographic structures.

Let's uncover what these bricks are:

Symmetric Encryption

Symmetric encryption is like a safe that uses the same key to lock and unlock it. Algorithms like the Advanced Encryption Standard (AES), Data Encryption Standard (DES), and Triple DES (3DES) fall into this category. In Python, we can use the PyCryptodome library to apply AES encryption as follows:

from Crypto.Cipher import AESfrom Crypto.Random import get_random_bytes key = get_random_bytes(16)cipher = AES.new(key, AES.MODE_EAX)ciphertext, tag = cipher.encrypt_and_digest(b'Alice and Bob know a secret.')

The challenge here, of course, is securely sharing the key. How can Alice send Bob the key without Eve intercepting it? That's where asymmetric encryption comes into play.

Asymmetric Encryption

Asymmetric encryption, also known as public key encryption, involves a pair of keys - one public, one private. Anyone can use the public key to encrypt a message, but only someone with the corresponding private key can decrypt it. Alice can share her public key with Bob (and the rest of the world), and anyone can use it to send her a secret message.

from Crypto.PublicKey import RSAfrom Crypto.Cipher import PKCS1_OAEP key = RSA.generate(2048)private_key = key.export_key()public_key = key.publickey().export_key() recipient_key = RSA.import_key(public_key)cipher_rsa = PKCS1_OAEP.new(recipient_key)encrypted_data = cipher_rsa.encrypt(b'Alice knows another secret.')

RSA (Rivest-Shamir-Adleman), DSA (Digital Signature Algorithm), ECC (Elliptic Curve Cryptography), and ElGamal are some of the algorithms that make this magic possible.

Hash Functions

A hash function takes an input of any length and produces a fixed-length string of characters. It's like a magical blender that blends your message into a fixed size smoothie, but once blended, you can't get your original fruits (message) back.

from Crypto.Hash import SHA256 hash_object = SHA256.new(data=b'Hello Bob!')hex_dig = hash_object.hexdigest()

Common examples of hash functions include MD5, SHA-1, and SHA-256. They're crucial in validating data integrity, among other things.

Digital Signatures

A digital signature lets Alice send a message with a guarantee that it came from her and hasn't been tampered with. It combines the power of hash functions and asymmetric encryption. Algorithms used include RSA, DSA, and ECDSA.

from Crypto.Signature import pkcs1_15from Crypto.Hash import SHA256 key = RSA.generate(2048)private_key = key.export_key()public_key = key.publickey().export_key() message = b'This is Alice.'h = SHA256.new(message) signature = pkcs1_15.new(key).sign(h)

Cryptography Protocols

SSL/TLS and PGP are examples of cryptographic protocols, higher-level constructions that use the above primitives to secure data and communication. SSL/TLS provides security over network connections, while PGP secures data at rest.

Cryptanalysis and Security

Cryptanalysis is the cryptographer's game of cat and mouse, trying to crack cryptographic systems and reveal their secrets

. The measure of a cryptographic system's strength lies in its resistance to such cryptanalysis.

# A trivial example of cryptanalysisdef frequency_analysis(ciphertext):    frequency = {}    for letter in ciphertext:        if letter in frequency:            frequency[letter] += 1        else:            frequency[letter] = 1    return frequency

This simple Python function conducts frequency analysis on a given ciphertext, one of the oldest and simplest methods of cryptanalysis.

The above cryptographic primitives form the building blocks for most cryptographic systems. The curious cryptographer might ask: what about cryptographic protocols like IKE, IPsec, SSH, and so on? Yes, there's an entire universe out there, but this constellation of primitives gives us a robust base from which to explore it.

A renoir painting of an old man listening to a computer with a stethoscope

Related Articles