Cryptography uses math operations to make data harder to understand without a key. Some operations are simple but powerful tools for mixing and transforming data. Even modern encryption algorithms depend on them.
XOR (Exclusive OR)
XOR is a bitwise operation used to compare two binary values. It returns 1
if the bits are different, and 0
if they are the same.
- Common in symmetric encryption (e.g., AES, stream ciphers)
- Reversible:
A ⊕ B ⊕ B = A
(can get the original value back) - Also used in hash functions and error detection
XOR Truth Table:
A | B | A ⊕ B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example:
A: 1101
B: 1011
-------------
A⊕B: 0110
Modulo Operation
Modulo gives the remainder when one number is divided by another. It’s written as a mod n
.
- Modular arithmetic is used in RSA, Diffie-Hellman, and ECC
- Helps with looping values inside a fixed range
- Keeps numbers within specific limits in math-based encryption
Example:
17 mod 5 = 2 (because 17 ÷ 5 = 3 with remainder 2)
AND, OR, NOT (Bitwise Operations)
These logical operations work on individual bits. They are often used in masking, shifting, and low-level data handling.
Bitwise AND:
Returns 1
only if both bits are 1.
A: 1101
B: 1011
A & B: 1001
Bitwise OR:
Returns 1
if either bit is 1.
A: 1101
B: 1011
A | B: 1111
Bitwise NOT:
Flips each bit (1 becomes 0, and 0 becomes 1).
A: 1101
~A: 0010