{
  "id": "enwiki-001355319825",
  "slug": "xor-cipher",
  "title": "XOR cipher",
  "qid": "",
  "lead": "In cryptography, the simple XOR cipher is a type of additive cipher, an encryption algorithm that operates according to the principles: :A \\oplus 0 = A, :A \\oplus A = 0, :A \\oplus B = B \\oplus A, :(A \\oplus B) \\oplus C = A \\oplus (B \\oplus C), :(B \\oplus A) \\oplus A = B \\oplus 0 = B For example where \\oplus denotes the exclusive disjunction (XOR) operation. This operation is sometimes called modulus 2 addition (or subtraction, which is identical). With this logic, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.",
  "body": "In cryptography, the simple XOR cipher is a type of additive cipher, an encryption algorithm that operates according to the principles: A \\oplus 0 = A, A \\oplus A = 0, A \\oplus B = B \\oplus A, (A \\oplus B) \\oplus C = A \\oplus (B \\oplus C), (B \\oplus A) \\oplus A = B \\oplus 0 = B For example where \\oplus denotes the exclusive disjunction (XOR) operation. This operation is sometimes called modulus 2 addition (or subtraction, which is identical). With this logic, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher. ==Example== The string \"Wiki\" ( in 8-bit ASCII) can be encrypted with the repeating key as follows: And conversely, for decryption: The XOR operator in any of these ciphers is vulnerable to a known-plaintext attack, since plaintext \\oplus ciphertext = key. It is also trivial to flip arbitrary bits in the decrypted plaintext by manipulating the ciphertext. This is called malleability. ==Example implementation== Example using the JavaScript programming language. function xor_Encrypt(inputString, key) { let encrypted_Hex = \"\"; for (let i = 0; i \"05\") let hex = xor_Result.toString(16); if (hex.length 171) bytes.push(parseInt(hexa_Byte, 16)); } // Step 2: XOR each byte with the key and convert back to character for (let i = 0; i Another example using the Python programming language. from os import urandom def generate_key(length: int) -> bytes: \"\"\"Generate encryption key.\"\"\" return urandom(length) def xor_strings(s, t) -> bytes: \"\"\"Concatenate xor two strings together.\"\"\" if isinstance(s, str): # Text strings contain single characters return \"\".join(chr(ord(a) ^ b) for a, b in zip(s, t)).encode(\"utf8\") else: # Bytes objects contain integer values in the range 0-255 return bytes([a ^ b for a, b in zip(s, t)]) message = \"This is a secret message\" print(\"Message:\", message) key = generate_key(len(message)) print(\"Key:\", key) cipherText = xor_strings(message.encode(\"utf8\"), key) print(\"cipherText:\", cipherText) print(\"decrypted:\", xor_strings(cipherText, key).decode(\"utf8\")) Verify if xor_strings(cipherText, key).decode(\"utf8\") == message: print(\"Unit test passed\") else: print(\"Unit test failed\") A shorter example using the R programming language, based on a puzzle posted on Instagram by GCHQ. secret_key as.raw() secret_message charToRaw() |> xor(secret_key) |> base64enc::base64encode() secret_message_bytes base64enc::base64decode() xor(secret_message_bytes, secret_key) |> rawToChar() ==References== ===Citations=== ===Sources=== Transcript of a lecture given by Prof. Tutte at the University of Waterloo",
  "categories": [
    "23118434",
    "23603337",
    "27106044",
    "48128016",
    "63697623"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1355319825",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1779332961.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001355319825",
    "type": "webpage",
    "title": "XOR cipher",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/xor-cipher",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          20
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001355319825",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1355319825",
    "note": "Immutable snapshot enwiki-001355319825"
  }
}