šŸ³ļø
The CTF Recipes
  • Introduction
  • Cryptography
    • Introduction
    • General knowledge
      • Encoding
        • Character encoding
          • ASCII
          • Unicode
          • UTF-8
        • Data encoding
          • Base16
          • Base32
          • Base64
      • Maths
        • Modular arithmetic
          • Greatest Common Divisor
          • Fermat's little theorem
          • Quadratic residues
          • Tonelli-Shanks
          • Chinese Remainder Theorem
          • Modular binomial
      • Padding
        • PKCS#7
    • Misc
      • XOR
    • Mono-alphabetic substitution
      • Index of coincidence
      • frequency analysis
      • Well known algorithms
        • šŸ”“Scytale
        • šŸ”“ROT
        • šŸ”“Polybe
        • šŸ”“Vigenere
        • šŸ”“Pigpen cipher
        • šŸ”“Affine cipher
    • Symmetric Cryptography
      • AES
        • Block Encryption procedure
          • Byte Substitution
          • Shift Row
          • Mix Column
          • Add Key
          • Key Expansion / Key Schedule
        • Mode of Operation
          • ECB
            • Block shuffling
              • Challenge example
            • ECB Oracle
              • Challenge example
          • CBC
            • Bit flipping
              • Challenge example
            • Padding oracle
              • Challenge example
          • OFB
            • Key stream reconstruction
            • Encrypt to Uncrypt
  • šŸ› ļøPwn
    • General knowledge
      • STACK
        • Variables storage
        • Stack frame
      • PLT and GOT
      • HEAP
        • HEAP operations
        • Chunk
        • Bins
        • Chunk allocation and reallocation
      • Syscall
    • Architectures
      • aarch32
        • Registers
        • Instruction set
        • Calling convention
      • aarch64
        • Registers
        • Instruction set
        • Calling convention
      • mips32
        • Registers
        • Instruction set
        • Calling convention
      • mips64
        • Registers
        • Instruction set
        • Calling convention
      • x86 / x64
        • Registers
        • Instruction set
        • Calling convention
    • Stack exploitation
      • Stack Buffer Overflow
        • Dangerous functions
          • gets
          • memcpy
          • sprintf
          • strcat
          • strcpy
        • Basics
          • Challenge example
        • Instruction pointer Overwrite
          • Challenge example
        • De Bruijn Sequences
        • Stack reading
          • Challenge example
      • Format string
        • Dangerous functions
          • printf
          • fprintf
        • Placeholder
        • Data Leak
          • Challenge example
        • Data modification
          • Challenge example
      • Arbitrary code execution
        • Shellcode
        • ret2reg
        • Code reuse attack
          • Ret2plt
          • Ret2dlresolve
          • GOT Overwrite
          • Ret2LibC
          • Leaking LibC
          • Ret2csu
          • Return Oriented Programming - ROP
          • Sigreturn Oriented Programming - SROP
          • Blind Return Oriented Programming - BROP
            • Challenge example
          • šŸ”“Call Oriented Programming - COP
          • šŸ”“Jump Oriented Programming - JOP
          • One gadget
        • Stack pivoting
    • šŸ› ļøHeap exploitation
      • Heap overflow
        • Challenge example
      • Use after free
        • Challenge example
      • šŸ› ļøDouble free
      • šŸ”“Unlink exploit
    • Protections
      • Stack Canaries
      • No eXecute
      • PIE
      • ASLR
      • RELRO
    • Integer overflow
Powered by GitBook
On this page
  • How it works ?
  • Properties
  • Python
  • Resources
  1. Cryptography
  2. Misc

XOR

The XOR (Exclusive OR) operator is a logical operation that takes two binary inputs and returns a single binary output.

In cryptography, XOR is often used as a simple method for combining two values into a single value or for separating a single value into two parts.

How it works ?

XOR is a bitwise operator which returns 0 if the bits are the same, and 1 otherwise. In textbooks the XOR operator is denoted by āŠ•, but in most programming languages you will see the char^ used instead.

A
B
Result

0

0

0

0

1

1

1

1

0

1

0

1

For longer binary inputs, the XOR operation is done bit by bit : 1010 ^ 0111 = 1101 . If the two operand haven't the same size, then the shorter padded with non-significant 0 :

1010 ^ 01 = 1010 ^ 0001 = 1011

Properties

Like other standard mathematical objects, xor has some specific properties :

Commutative  : A āŠ• B = B āŠ• A
Associative  : A āŠ• B āŠ• C = A āŠ• (B āŠ•  C) = (A āŠ• B) āŠ• C
Identity     : A āŠ• 0 = A
Self-Inverse : A āŠ• A = 0

These properties are very interesting cause it permit the following :

D=AāŠ•BAāŠ•D=AāŠ•(AāŠ•B)=AāŠ•AāŠ•B=(AāŠ•A)āŠ•B=0āŠ•B=BD = A \oplus B \\ A \oplus D = A \oplus ( A \oplus B) = A \oplus A \oplus B = (A \oplus A) \oplus B = 0 \oplus B = BD=AāŠ•BAāŠ•D=AāŠ•(AāŠ•B)=AāŠ•AāŠ•B=(AāŠ•A)āŠ•B=0āŠ•B=B

Python

from pwn import *

A = "someData"
B = "OtherData"

xored = xor(A,B)

Resources

PreviousMiscNextMono-alphabetic substitution

Last updated 2 years ago

CyberChef
Logo