Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article explores symmetric-key cryptography, a subject I encountered while learning about blockchains.

While reading Practical Introduction to Blockchain: From Bitcoin to Ethereum and DApp Development, I looked up the concepts I did not understand and collected what I learned here.

The subject is symmetric-key cryptography. I have divided the material into two articles because of its length.

Practical Introduction to Blockchain: From Bitcoin to Ethereum and DApp Development

Symmetric-key cryptography

How it works

In one sentence:

The same key is used to encrypt data and to decrypt it (recover the original message).

The following diagram illustrates the process.

Encryption and decryption with a shared key

The message "Cardene" (かるでね in the diagram) is encrypted using a key. Decrypting the ciphertext with that same key recovers the original message.

This makes it possible to send a message over the Internet without revealing its contents to anyone other than the intended recipient, provided the key remains secret and the encryption is secure.

Sending encrypted data to the intended recipient

Using the same key for encryption and decryption generally allows these operations to be relatively fast.

There are, however, some key-management challenges.

More recipients mean more keys

The number of keys grows with the number of communication partners

When each communication partner requires a separate key, managing those keys becomes increasingly difficult.

Keys can be leaked

Risk of key leakage during distribution

If a key is sent over the Internet without adequate protection, a third party may obtain it.

If the key held by either the sender or the recipient is compromised, an attacker may be able to read encrypted messages or encrypt a different message and deliver it to the recipient.

References

Kerckhoffs's principle

Explanation

A cryptosystem should remain secure even when its encryption method is known.

The secrecy of the key, rather than secrecy about the encryption method, should protect the information.

Plaintext (the original, unencrypted message) may be intercepted while traveling across a network. Encrypting it with a key is intended to prevent an eavesdropper from understanding the message.

References

XOR operation

Explanation

Exclusive OR (XOR) returns true when exactly one of its two inputs is true. It returns false when both inputs are true or both are false.

The following truth table and Venn diagram illustrate this behavior.

XOR truth table and Venn diagram

In the table on the left, 1 represents true and 0 represents false. The orange region in the Venn diagram on the right represents true.

It is also useful to remember properties such as commutativity and associativity before moving on.

Commutative property of XOR

Associative property of XOR

References

Practical Introduction to Blockchain: From Bitcoin to Ethereum and DApp Development

Stream ciphers

Explanation

A stream cipher encrypts plaintext by applying XOR with a keystream, one bit or one byte at a time. Decryption applies XOR to the ciphertext with the same keystream to recover the plaintext.

Different keystream values are generated as encryption proceeds.

In the approach illustrated here, a pseudorandom number generator uses a seed to produce the sequence used to transform the characters. The sender and receiver share the seed so that they can generate the same sequence.

Stream cipher encryption and decryption

Advantages

  • Limited error propagation can make synchronization easier.
  • Data can be decrypted as it arrives, reducing processing delay.

There are two approaches to synchronization.

Synchronous stream ciphers

In a synchronous stream cipher, the sender and receiver maintain synchronization. Deletion, insertion, or repetition of data can disrupt that synchronization, making the disruption apparent.

Self-synchronizing (asynchronous) stream ciphers

A self-synchronizing stream cipher can recover synchronization automatically after a disruption, although an error in one bit may propagate to other bits.

Disadvantages

  • Some historical pseudorandom generators and stream-cipher constructions have been attacked and are no longer recommended.

The original article lists the following protocols and cipher in connection with historical stream-cipher weaknesses.

Key points

  • SSL
  • TLS
  • WEP/WPA
  • RC4

SSL, TLS, and WEP/WPA are protocols or protocol families, not pseudorandom generators; RC4 is a historical stream cipher. The security of a protocol depends on its version and configuration.

Readers interested in these examples can investigate them further.

The one-to-one XOR operation also has no diffusion by itself: changing one plaintext bit changes the corresponding ciphertext bit. This is a property of the operation, rather than a general proof that stream ciphers are insecure.

References

Block ciphers

Explanation

A block cipher encrypts fixed-length blocks of plaintext under the same key. Depending on the cipher, a block may contain 64, 128, or another supported number of bits (the original example also lists 256 bits).

A ciphertext block has the same length as its corresponding plaintext block.

Encrypting identical plaintext blocks directly under the same key produces identical ciphertext blocks. This can reveal patterns in the original data.

Block cipher encryption

Modes of operation

A mode of operation specifies how to apply a block cipher repeatedly to a message.

When plaintext is shorter than a full block, some modes use padding to fill the remaining space.

The following are common modes.

ECB mode

  • Each plaintext block is encrypted directly into a ciphertext block.
  • Identical plaintext blocks produce identical ciphertext blocks under the same key, which exposes patterns and weakens confidentiality.

ECB mode

CBC mode

  • Each plaintext block is XORed with the previous ciphertext block before encryption.

CBC mode

CFB mode

  • The previous ciphertext block is encrypted, and its output is XORed with the plaintext to produce the ciphertext.

CFB mode

OFB mode

  • The output of the cipher (the keystream) is XORed with the plaintext block to produce the ciphertext block.
  • The keystream is generated by encrypting the previous cipher output, rather than the ciphertext itself.

OFB mode

CTR mode

  • A counter that increases by one is encrypted to generate the keystream, which is XORed with the plaintext to produce ciphertext.
  • In the illustrated layout, the first eight bytes remain fixed and the last eight bytes form the incrementing counter.

CTR mode

Advantages

  • Block ciphers provide diffusion within a block. Depending on the mode, changes may also affect other ciphertext blocks.

Disadvantages

  • Encryption and decryption may take longer than with stream ciphers.
  • Depending on the mode, a single-bit error may corrupt a whole block.

References

Pseudorandom number generators

Explanation

A pseudorandom number generator is an algorithm that produces numbers that appear random and unpredictable. Its output is not genuinely random.

For cryptographic use, it is important to distinguish ordinary pseudorandom generators from cryptographically secure ones.

The original discussion highlights the following properties.

Key points

  • Zeros and ones occur with approximately equal frequency.
  • Randomness: no readily discernible pattern.
  • Unpredictability: an observer cannot feasibly predict future output from prior output.
  • Resistance to deliberately obtaining a chosen output without the necessary internal information.

References

One-time pads

Explanation

A one-time pad is a form of symmetric-key encryption that assumes a secret key has been shared in advance.

It has several practical limitations.

Key points

  • For a one-time pad, the key must be as long as the plaintext.
    • Encrypting a 1 GB file requires 1 GB of key material, effectively doubling the total amount of data that must be handled.
  • How can the sender and recipient share the secret key securely? (The key distribution problem.)
    • If secure distribution is already possible, why is encryption needed?

References

Practical Introduction to Blockchain: From Bitcoin to Ethereum and DApp Development

Conclusion

I hope this article has helped you understand symmetric-key cryptography a little better.

I put this article together while working through the material myself, so there may still be gaps in my understanding. If you notice an error, I would appreciate a correction in the comments or by direct message.

I mainly write about Python, and I would also appreciate a follow on Twitter.