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 ⊕. XOR compares two bits:
The result is 0 when the bits are the same.
The result is 1 when the bits are different.
Its four possible inputs are:
0⊕0=0
0⊕1=1
1⊕0=1
1⊕1=0
XOR has a useful property for this demonstration: applying the same value twice restores the original value.
Here, P represents a plaintext bit and K 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 |
| 01000001 |
T |
| 01010100 |
T |
| 01010100 |
A |
| 01000001 |
C |
| 01000011 |
K |
| 01001011 |
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:
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:
For the first character, A:
The result, 00110010, is 0x32, which is the ASCII code for the character “2.” Applying the same operation to each byte gives:
0x41 ⊕ 0x73 = 0x320x54 ⊕ 0x73 = 0x270x54 ⊕ 0x73 = 0x270x41 ⊕ 0x73 = 0x320x43 ⊕ 0x73 = 0x300x4B ⊕ 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:
For the first byte:
The result, 01000001, 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:
For example:
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.