Skip to content
Cryptography ๐Ÿ”

Cryptography: concepts and applications ๐Ÿ”

Why cryptography? ๐Ÿค”

Cryptography isn’t just spy or nerd stuff: it’s the foundation for protecting data in every digital context. Without it, you’re like a safe with the combination written on the outside. Today cryptography is essential for the security of communications (HTTPS, VPN, email), the protection of data at rest (disks, databases, backups), the security of digital identities (two-factor authentication, electronic signatures), and user privacy (GDPR, anyone?).

Here’s why cryptography is so important:

  • Confidentiality: ensures only those authorized can access data (e.g. encryption of files, email, communications).
  • Integrity: ensures data isn’t altered during transmission or storage.
  • Authenticity: allows verification of the identity of whoever accesses or sends data (e.g. login, digital signatures, certificates).
  • Non-repudiation: prevents whoever sends a message or signs a document from denying they did so (e.g. digital signatures, signed logs).

Cryptography is like coffee: if you use it badly, you risk staying up all nightโ€ฆ recovering lost data. โ˜•๏ธ

Hardware encryption: beyond software ๐Ÿ”’

Beyond software solutions, cryptography can also be implemented at the hardware level, often offering a higher level of security thanks to the physical protection of devices and isolated key management. Combining software and hardware encryption is a best practice for high-security scenarios.

  • TPM (Trusted Platform Module): a chip built into devices that provides authentication functions, platform integrity, full disk encryption, and software license protection.
  • HSM (Hardware Security Module): specialized devices for generating, managing, and safeguarding cryptographic keys, equipped with physical protections and dedicated computing capacity.
  • Disk encryption: many modern disks (SSD/HDD) support native hardware encryption, often integrated with TPM or HSM for secure key management and encryption operations.

Hardware encryption reduces the risk of key compromise through software attacks and guarantees high performance, making it ideal for enterprise, cloud, and critical infrastructure environments.

Symmetric and asymmetric algorithms โš–๏ธ

When talking about cryptography, it’s essential to distinguish between the main types of algorithms used to protect data. Here’s an overview of the most common categories and their practical applications:

  • Symmetric: same key to encrypt and decrypt (e.g. AES, Blowfish). Fast, but key management is a nightmare if you have lots of friends. Used to encrypt large amounts of data (disks, files, VPN tunnels).
  • Asymmetric: public/private key pair (e.g. RSA, ECC). Slower, but perfect for exchanging secrets without shouting them in the hallway. Fundamental for secure key exchange, digital signatures, and end-to-end encryption in modern chat apps.
  • Hybrid: many protocols (like TLS) use both: asymmetric to exchange the key, symmetric to encrypt the actual data.

Symmetric algorithms ๐Ÿ”‘

Symmetric algorithms use the same key to both encrypt and decrypt data. They’re extremely fast and suitable for encrypting large amounts of information, but secure key management represents a significant challenge, especially in distributed environments.

  • DES (Data Encryption Standard): one of the first standards, uses 56-bit keys and 64-bit blocks. Today it’s considered insecure due to its vulnerability to brute force attacks. It has been replaced by more robust algorithms.
  • 3DES (Triple DES): an evolution of DES that applies the algorithm three times with different keys, increasing security but reducing performance. 3DES is also now considered obsolete.
  • Blowfish: a block algorithm with 64-bit blocks and variable keys up to 448 bits. Designed to be fast and secure, it has been widely used in software and protocols like SSH.
  • Twofish: the successor to Blowfish, works on 128-bit blocks and supports 128, 192, or 256-bit keys. It offers excellent performance and a high level of security, making it suitable for embedded devices too.
  • Threefish: designed for long-term security, uses 256, 512, or 1024-bit blocks and keys up to 1024 bits. It’s known for its resistance to attacks and execution speed.
  • AES (Advanced Encryption Standard): the current standard for symmetric encryption. It supports 128, 192, and 256-bit keys, with 10-14 rounds of encryption. It’s widely adopted worldwide for protecting sensitive data, both in the private and government sectors.

Protecting the symmetric key ๐Ÿ›ก๏ธ

The security of symmetric encryption depends entirely on protecting the key. Here are a few fundamental strategies:

  • Limit access: only authorized users and processes should be able to access the key.
  • Secure transmission: send the key over channels separate and protected from the encrypted data, for example through asymmetric encryption.
  • Regeneration: change the key immediately if you suspect a compromise.
  • Limit use and duration: use different keys for different sessions or data, and set a limited lifetime for each key.
  • Mitigate chosen plaintext attacks: adopt encryption techniques and modes that reduce the predictability of the encrypted data.

Password Based Key Derivation Function (PBKDF2) ๐Ÿง‚

When keys are derived from passwords, it’s essential to use robust derivation functions to prevent attacks:

  • PBKDF2: transforms a password into a secure cryptographic key through many iterations of HMAC and the use of a random salt. This process slows down brute force attacks and makes precomputed tables (rainbow tables) ineffective.
  • Parameters: choose a high number of iterations and a sufficiently long salt to increase security.
  • Modern alternatives: also consider algorithms like bcrypt, scrypt, or Argon2 for even greater protection.

Blocks, diffusion, and modes of operation ๐Ÿงฉ

Block algorithms operate by dividing data into fixed-size portions called blocks (for example, 128 bits for AES). Each block is encrypted separately or in combination with other blocks, depending on the chosen mode of operation. If the data isn’t a multiple of the block size, padding is applied to complete the last block.

Modes of operation determine how blocks are processed and linked together, affecting security and resistance to attacks:

  • ECB (Electronic Codebook): each input block is encrypted separately. Not very secure because identical blocks produce identical output, making pattern analysis of the encrypted data possible.
  • CBC (Cipher Block Chaining): each plaintext block is combined with the previous encrypted block before encryption. Improves diffusion and security compared to ECB.
  • CFB (Cipher Feedback) / OFB (Output Feedback): turn a block cipher into a stream cipher, allowing encryption of variable-length data without the need for padding.
  • CTR (Counter Mode): uses a counter to generate a keystream, offering parallelization and high performance.
  • IV (Initialization Vector): a random value used to initialize the encryption of the first block, ensuring the same message encrypted multiple times produces different output. The IV must be unique and, in many cases, public.

Choosing the mode of operation and correctly managing IV and padding are essential to avoid vulnerabilities and ensure the security of symmetric encryption.

Asymmetric cryptography ๐Ÿ—๏ธ

Asymmetric cryptography uses a pair of keys: one public and one private. One key is used to encrypt data, while the other is used to decrypt it. This mechanism allows exchanging sensitive information even over insecure channels and forms the basis for many modern applications, including secure exchange of symmetric keys, digital signatures, and authentication.

Main algorithms: RSA, DSA, ECC ๐Ÿ“

  • RSA: used for both encryption and digital signatures, requires keys of at least 2048 bits to ensure a good level of security.
  • DSA: designed exclusively for digital signatures.
  • ECC (Elliptic Curve Cryptography): uses elliptic curves, offers the same security as RSA with much smaller keys; ideal for mobile devices and blockchain applications.

Digital signatures and non-repudiation โœ๏ธ

Digital signatures guarantee the authenticity and non-repudiation of a document. The process involves calculating the hash of the document and encrypting it with the sender’s private key. Verification takes place by decrypting the signature with the public key and comparing the resulting hash with that of the original document.

Attacks and best practices ๐Ÿšจ

  • Always use robust, up-to-date hash functions.
  • Never sign third-party documents without verification.
  • Apply secure padding schemes (e.g. OAEP for RSA) to prevent known vulnerabilities.
  • Carefully protect the private key: its compromise invalidates the security of the entire system.

Hash, HMAC, and digital signatures ๐Ÿ“

Beyond encryption, cryptography offers tools to ensure data integrity and authenticity. Here are the main mechanisms used in practice:

  • Hash: a digital fingerprint of data (e.g. SHA-256). If even one bit changes, everything changes. Used for integrity, digital signatures, password storage.
  • HMAC: hash with a secret key, to authenticate messages and ensure they haven’t been altered.
  • Digital signatures: hash encrypted with the private key, to say “I swear I wrote this”. Used in email, software, blockchain.
  • MAC and authentication: Message Authentication Code, similar to HMAC, used in protocols like IPsec.

Hashes must be unpredictable and have high entropy. Collision resistance is essential: two different inputs must not produce the same hash.

Main hash algorithms ๐Ÿงฎ

  • MD5: 128 bits, 64 rounds, compromised today.
  • SHA-1: 160 bits, 80 rounds, deprecated.
  • SHA-2: 224-512 bits, 64-80 rounds, still secure.
  • SHA-3: 1600-bit state, not yet very widespread.

JWT: JSON Web Token ๐Ÿช™

JWTs (JSON Web Tokens) are signed and, optionally, encrypted tokens, widely used for authentication and authorization in web applications and APIs. A JWT consists of three parts, separated by dots (.):

  1. Header: contains the token type (JWT) and the signing algorithm (e.g. HS256, RS256).
  2. Payload: contains the information (claims) to be transmitted, such as the user’s identity, roles, expiration, etc.
  3. Signature: guarantees the integrity and authenticity of the token, calculated by signing the header and payload with a secret or private key.
1
2
3
4
5
6
7
base64UrlEncode(header) + "." +
base64UrlEncode(payload) + "." +
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret
)

Main characteristics โญ๏ธ

  • Stateless: the server doesn’t need to maintain sessions, all information is in the token.
  • Signed: the signature ensures the token hasn’t been tampered with.
  • Expiration: claims like exp allow setting a limited lifetime.
  • Warning: the data in the payload isn’t encrypted by default, so don’t put sensitive information in it without additional encryption.

PKI ๐Ÿ”‘

Public Key Infrastructure (PKI) is the foundation for secure management of digital identities and encrypted communications on a large scale. A well-designed PKI allows you to establish trust relationships between users, devices, and services in distributed environments like the Internet. In this section you’ll see how PKI works, its main components, and its role in the security of protocols like HTTPS, signed email, and VPN.

It manages public keys and digital certificates through a hierarchy of certificate authorities (CA). It allows reliably associating a public key with an identity, ensuring authenticity and integrity in secure communications.

Digital certificates and chain of trust ๐Ÿ›๏ธ

A digital certificate associates a public key with an identity (person, server, organization) and is signed by a CA. The main elements of a certificate are:

  • Subject: the identity to which the certificate is assigned
  • Issuer: the CA that issued and signed the certificate
  • Validity: the time period during which the certificate is valid
  • Public Key: the associated public key
  • Signature: the CA’s digital signature

The chain of trust starts from the Root CA (pre-installed in operating systems and browsers), passes through any intermediate CAs, and reaches the final certificate used by the application or service.

Types of authorities, formats, and certificate management ๐Ÿ“„

To fully understand how a PKI works, it’s important to know the different types of certificate authorities involved, the main formats used for certificates and requests, as well as the revocation and verification mechanisms. These elements are essential to ensure the security, scalability, and effective management of digital identities.

Types of certificate authorities (CA) ๐Ÿข

  • Root CA: the main, highly protected authority, on which the entire trust of the PKI depends.
  • Intermediate/Subordinate CA: intermediate authorities delegated by the Root CA to issue certificates, reducing the risk in case of compromise.
  • Issuing CA: the CA responsible for directly issuing certificates to end users or services.

Certificate formats and requests ๐Ÿ“ฆ

Digital certificates and their related keys can be exchanged and managed through different standard formats, each designed for specific usage scenarios:

  • PKCS #7: format for transmitting X.509 certificates.
  • PKCS #10: standard format for certificate signing requests (CSR).
  • PKCS #12: container format for private keys and certificates, often password-protected.

Certificate revocation and verification โŒ

Managing certificate validity is crucial for maintaining trust in the system. There are several methods for revoking and verifying certificate status:

  • CRL (Certificate Revocation List): a periodically updated list of certificates revoked by a CA.
  • OCSP (Online Certificate Status Protocol): a protocol for real-time verification of a certificate’s validity status.

Careful management of PKI and keys is essential to ensure the security of communications and trust in digital systems.

Attacks, risks, and common mistakes ๐Ÿšจ

Cryptography isn’t a magic wand: even the most robust systems can be compromised by implementation errors, wrong choices, or targeted attacks. Below you’ll find the main risks and traps to avoid in daily practice:

  • Brute force: tries every possible key. The longer the key, the more soundly you sleep.
  • Man in the middle: someone gets in between and listens. Always use TLS and verify certificates.
  • Side-channel: attacks that exploit timing, power consumption, or hardware errors. Not everything is solved with software!
  • Hash and passwords: never store passwords in plaintext. Always use robust hashes and salts.
  • Weak implementations: code errors, use of obsolete algorithms (MD5, SHA-1), keys that are too short, manual key management.
  • Random that isn’t random: weak random number generators compromise the security of the entire system.

Quantum computing and the future of cryptography ๐Ÿง‘โ€๐Ÿš€

Quantum computing promises to break many current algorithms, but for now you can sleep (reasonably) soundly. Follow official recommendations and get ready to switch algorithms when the time comes. Keep an eye on post-quantum solutions (lattice-based, hash-based, etc.).

Best practices and practical advice ๐Ÿ› ๏ธ

To help you avoid common mistakes and strengthen the security of your solutions, here are a few fundamental best practices to follow when implementing cryptography:

  • Use only standard, up-to-date algorithms and libraries (no, don’t write your own algorithm in one night of inspiration!).
  • Protect keys more than the data itself.
  • Automate key rotation and revocation.
  • Log access and critical operations.
  • Train the team: cryptography is powerful, but only if used well.
  • Document cryptographic choices and periodically update policies.

Cryptography and compliance ๐Ÿ“‹

Cryptography isn’t just a technical matter, but also one of regulatory compliance. Many standards and regulations require the adoption of adequate cryptographic measures. Here are a few useful references for navigating compliance and audits:

  • GDPR, PCI-DSS, ISO/IEC 27001: many regulations require encryption of sensitive data and secure key management.
  • Audit and logging: traceability of critical operations is essential to demonstrate compliance and identify incidents.

Conclusion ๐ŸŽฏ

Cryptography is the digital seatbelt: it doesn’t prevent accidents, but it saves your life when they happen. And remember: the weakest key is always the one you leave written on a sticky note! ๐Ÿ˜‰

Last updated on