Coder Social home page Coder Social logo

cryptools / xorcipher Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 5.0 23 KB

Additive Cipher related to binary

Home Page: https://en.wikipedia.org/wiki/XOR_cipher

License: MIT License

JavaScript 41.11% Python 39.02% Swift 19.87%
xor xor-cipher cipher frequency-analysis encoding cryptography cryptools

xorcipher's Introduction

XOR Cipher

A simple adaptive cipher based on the logical XOR operation.

How it works

Encoding

The XOR cipher will encrypt a message by using the Boolean XOR (exclusive or) operation. A XOR B returns 1 if and only if A and B are different.

A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0

To encrypt a message with a given key, we first convert the string into their ASCII equivalent, and we then XOR every character with the key. For example, if we want to encrypt XOR with 134 as a key, we would do:

    X        O        R
01011000 01001111 01010010    # String in ASCII
10000110 10000110 10000110    # Repeating key 134
--------------------------
11011110 11001001 11010100    # XOR result
    Þ        É        Ô       # Result converted back into plain text

When implemented in python, we get char ^ key.

As you might have noticed, in this cipher, a given character is always replaced by the same character. This makes frequency analysis easier. To avoid that, we can use a non-repeating key eg. 29, 62, 134, where we loop through the keys to encode each character.

 X  O  R
88  79  82
29  62 134
----------
69 113 212
 E  q   Ô

Decoding

The XOR operation has a couple of properties, including:

A XOR A = 0
A XOR 0 = A
(A XOR B) XOR C = A XOR (B XOR C)

So, from that, we can conclude that:

B XOR A XOR A = B XOR 0 = B

Therefore, to decrypt a message, we need to re-XOR it with the same key, aka. re-encode it.

    Þ        É        Ô       # Encoded string
11011110 11001001 11010100    # String in  ASCII
10000110 10000110 10000110    # Repeating key 134
--------------------------
01011000 01001111 01010010    # XOR result
    X        O        R       # Decoded string

Cons

  • Frequency analysis can be used to crack the code if the key is a single number.

Implementations

  Language             Encrypt                       Decrypt            
  Javascript    encrypt.js  decrypt.js  
  Python       encrypt.py decrypt.py
  Swift        lib.swift  lib.swift

Running the tests

Tests are automatically handled by Travis CI.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

Made with ❤️ at CrypTools.

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

xorcipher's People

Contributors

arguiot avatar lucasgruwez avatar lukalafaye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.