1. Home
  2. Explore
  3. Computer Science
  4. Secret-Key Encryption

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Secret-Key Encryption

Secret-Key Encryption

Encryption and decryption

Encryption transforms readable information into a form intended to prevent unauthorized people from understanding it. The original readable information is called plaintext; the transformed information is called ciphertext.

Decryption reverses the transformation, recovering the plaintext from the ciphertext. Both processes use a key:

  • Encryption combines the plaintext with a key to produce ciphertext.

  • Decryption uses a key to recover the plaintext.

The method used to transform the data is the encryption algorithm. The key is a value supplied to that algorithm; changing the key generally changes the resulting ciphertext.


Secret-key and public-key encryption

Encryption methods are commonly grouped according to how their keys are used.

Secret-key encryption

In secret-key encryption, the same key is used for encryption and decryption. It is therefore also called symmetric-key cryptography.

The communicating parties must both have the shared key. The key must remain secret from anyone who should not be able to decrypt the ciphertext.

Public-key encryption

In public-key encryption, encryption and decryption use different keys. It is therefore also called asymmetric-key cryptography.

The example below focuses on secret-key encryption.


XOR as a simple encryption operation

The example uses the bitwise exclusive OR operation, written as XOR or ⊕\oplus⊕. XOR compares two bits:

  • The result is 000 when the bits are the same.

  • The result is 111 when the bits are different.

Its four possible inputs are:

  • 0⊕0=00 \oplus 0 = 00⊕0=0

  • 0⊕1=10 \oplus 1 = 10⊕1=1

  • 1⊕0=11 \oplus 0 = 11⊕0=1

  • 1⊕1=01 \oplus 1 = 01⊕1=0

XOR has a useful property for this demonstration: applying the same value twice restores the original value.

(P⊕K)⊕K=P(P \oplus K) \oplus K = P(P⊕K)⊕K=P

Here, PPP represents a plaintext bit and KKK represents a key bit. This property allows the same XOR operation and key to encrypt and decrypt.


Example: encrypting “ATTACK”

1. Represent the plaintext as bytes

Computers represent text using numerical character codes. In this example, the letters are represented using ASCII, with each character taking one byte (eight bits).

Character

ASCII hexadecimal

Binary

A

0x41

010000010100000101000001

T

0x54

010101000101010001010100

T

0x54

010101000101010001010100

A

0x41

010000010100000101000001

C

0x43

010000110100001101000011

K

0x4B

010010110100101101001011

Thus, the plaintext “ATTACK” is the byte sequence:

0x41 0x54 0x54 0x41 0x43 0x4B

2. Choose the key

The example uses the one-byte key 0x73, which is:

0x73=011100110x73 = 011100110x73=01110011

Because the plaintext has six bytes and the example uses a one-byte key, the same key byte is applied to every plaintext byte.

3. Encrypt each byte

Each plaintext byte is XORed bit by bit with the key byte:

ciphertext byte=plaintext byte⊕key byte\text{ciphertext byte} = \text{plaintext byte} \oplus \text{key byte}ciphertext byte=plaintext byte⊕key byte

For the first character, A:

01000001⊕01110011=0011001001000001 \oplus 01110011 = 0011001001000001⊕01110011=00110010

The result, 001100100011001000110010, is 0x32, which is the ASCII code for the character “2.” Applying the same operation to each byte gives:

  • 0x41 ⊕ 0x73 = 0x32

  • 0x54 ⊕ 0x73 = 0x27

  • 0x54 ⊕ 0x73 = 0x27

  • 0x41 ⊕ 0x73 = 0x32

  • 0x43 ⊕ 0x73 = 0x30

  • 0x4B ⊕ 0x73 = 0x38

The resulting ciphertext bytes are:

0x32 0x27 0x27 0x32 0x30 0x38

These bytes happen to correspond to the ASCII characters 2''208. This readable appearance is incidental: ciphertext is fundamentally a sequence of encoded bytes, not necessarily text that can be interpreted as characters.


Decrypting the ciphertext

To decrypt, the receiver XORs each ciphertext byte with the same key byte:

plaintext byte=ciphertext byte⊕key byte\text{plaintext byte} = \text{ciphertext byte} \oplus \text{key byte}plaintext byte=ciphertext byte⊕key byte

For the first byte:

00110010⊕01110011=0100000100110010 \oplus 01110011 = 0100000100110010⊕01110011=01000001

The result, 010000010100000101000001, is 0x41, the ASCII code for A. Applying this operation to the entire ciphertext recovers:

0x41 0x54 0x54 0x41 0x43 0x4B

This is the original plaintext, “ATTACK.” The same key and XOR operation work for both encryption and decryption because XORing with the key a second time cancels the first XOR.


Why this example is insecure

The XOR example demonstrates the mechanics of secret-key encryption, but it is not a secure practical encryption method. The key is very short and is repeated for every byte, making it vulnerable to analysis.

If an attacker knows a plaintext byte and its corresponding ciphertext byte, the key byte can be found by XORing them:

plaintext byte⊕ciphertext byte=key byte\text{plaintext byte} \oplus \text{ciphertext byte} = \text{key byte}plaintext byte⊕ciphertext byte=key byte

For example:

0x41⊕0x32=0x730x41 \oplus 0x32 = 0x730x41⊕0x32=0x73

The known plaintext byte A and its ciphertext byte 0x32 reveal the key 0x73. Since the same one-byte key is reused, learning that key byte exposes the encryption of every byte in this example.

A simple XOR demonstration can explain how a shared key is used, but a short, repeated key does not provide strong security.

Practical encryption algorithms use more complex processes and substantially longer keys. Some algorithms operate on data one unit at a time in a way associated with stream ciphers; others process data in fixed-size groups called blocks and are known as block ciphers. The example’s repeated one-byte XOR operation is only a simplified illustration of the broader secret-key encryption idea.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent