├── .travis.yml ├── LICENSE ├── README.md ├── __test__ └── test.js ├── js ├── decrypt.js └── encrypt.js ├── py ├── decrypt-v2.py ├── decrypt.py ├── encrypt-v2.py └── encrypt.py └── swift └── lib.swift /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | env: 4 | - SWIFT_VERSION=4.0.3 5 | language: node_js 6 | node_js: 7 | - "node" 8 | - "lts/*" 9 | - "8" 10 | before_install: 11 | - npm install -g npm@latest 12 | - npm install -g eye.js 13 | install: 14 | - eval "$(curl -sL https://swiftenv.fuller.li/install.sh)" # Install swift 15 | script: 16 | - eye # JS 17 | - swiftc swift/lib.swift -o out # Swift 18 | notifications: 19 | email: false 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 CrypTools 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XOR Cipher 2 |

3 | 4 | 5 |

6 |

7 | 8 | 9 | 10 | 11 |

12 | 13 | A simple adaptive cipher based on the logical XOR operation. 14 | 15 | ## How it works 16 | 17 | ### Encoding 18 | 19 | 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. 20 | 21 | | A | B | A XOR B | 22 | |---|---|---------| 23 | | 0 | 0 | 0 | 24 | | 0 | 1 | 1 | 25 | | 1 | 0 | 1 | 26 | | 1 | 1 | 0 | 27 | 28 | 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: 29 | 30 | ```txt 31 | X O R 32 | 01011000 01001111 01010010 # String in ASCII 33 | 10000110 10000110 10000110 # Repeating key 134 34 | -------------------------- 35 | 11011110 11001001 11010100 # XOR result 36 | Þ É Ô # Result converted back into plain text 37 | ``` 38 | 39 | When implemented in python, we get `char ^ key`. 40 | 41 | 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. 42 | 43 | ```txt 44 | X O R 45 | 88 79 82 46 | 29 62 134 47 | ---------- 48 | 69 113 212 49 | E q Ô 50 | ``` 51 | 52 | 53 | ### Decoding 54 | 55 | The XOR operation has a couple of properties, including: 56 | 57 | ```txt 58 | A XOR A = 0 59 | A XOR 0 = A 60 | (A XOR B) XOR C = A XOR (B XOR C) 61 | ``` 62 | 63 | So, from that, we can conclude that: 64 | 65 | ```txt 66 | B XOR A XOR A = B XOR 0 = B 67 | ``` 68 | 69 | Therefore, to decrypt a message, we need to re-XOR it with the same key, aka. re-encode it. 70 | 71 | ```txt 72 | Þ É Ô # Encoded string 73 | 11011110 11001001 11010100 # String in ASCII 74 | 10000110 10000110 10000110 # Repeating key 134 75 | -------------------------- 76 | 01011000 01001111 01010010 # XOR result 77 | X O R # Decoded string 78 | ``` 79 | 80 | ## Cons 81 | * Frequency analysis can be used to crack the code if the key is a single number. 82 | 83 | ## Implementations 84 | 85 | |   Language   |           Encrypt             |           Decrypt             | 86 | |----------------|--------------------------------|--------------------------------| 87 | |   Javascript   |  [encrypt.js](js/encrypt.js) |  [decrypt.js](js/decrypt.js)   | 88 | |   Python       | [encrypt.py](py/encrypt-v2.py) | [decrypt.py](py/decrypt-v2.py) | 89 | |   Swift       |  [lib.swift](swift/lib.swift) |  [lib.swift](swift/lib.swift) | 90 | 91 | ## Running the tests 92 | 93 | Tests are automatically handled by [Travis CI](https://travis-ci.org/CrypTools/XORCipher/). 94 | 95 | ## Contributing 96 | 97 | Please read [CONTRIBUTING.md](https://github.com/CrypTools/cryptools.github.io/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. 98 | 99 | ## Versioning 100 | 101 | We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/CrypTools/XORCipher/tags). 102 | 103 | ## Authors 104 | 105 | Made with ❤️ at CrypTools. 106 | 107 | See also the list of [contributors](https://github.com/CrypTools/XORCipher/contributors) who participated in this project. 108 | 109 | ## License 110 | 111 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details 112 | -------------------------------------------------------------------------------- /__test__/test.js: -------------------------------------------------------------------------------- 1 | // Test made using EyeJS - https://eye.js.org 2 | 3 | const path = require('path').normalize(__testDir + "/../js/") 4 | 5 | const encrypt = require(path + "encrypt.js") 6 | const decrypt = require(path + "decrypt.js") 7 | 8 | 9 | eye.test("Encryption", "node", 10 | $ => $(encrypt("Hello World!", 134)).Equal("Îãêêé¦Ñéôêâ§"), 11 | $ => $(encrypt("Hello World!", [29, 62, 134])).Equal("U[êqQ¦JQôqZ§") 12 | ) 13 | eye.test("Decryption", "node", 14 | $ => $(decrypt("Îãêêé¦Ñéôêâ§", 134)).Equal("Hello World!"), 15 | $ => $(decrypt("U[êqQ¦JQôqZ§", [29, 62, 134])).Equal("Hello World!") 16 | ) 17 | -------------------------------------------------------------------------------- /js/decrypt.js: -------------------------------------------------------------------------------- 1 | /* ========================================================================== 2 | * Use: 3 | * "Îãêêé¦Ñéôêâ§".decrypt(134) 4 | * => 'Hello World!' 5 | * 6 | * Or in the case of non-repeating keys: 7 | * 8 | * Use: 9 | * "U[êqQ¦JQôqZ§".decrypt([29, 62, 134]) 10 | * => 'Hello World!' 11 | * 12 | * ========================================================================== */ 13 | 14 | String.prototype.decrypt = function(key) { 15 | 16 | if (typeof key == 'number') key = [key] 17 | 18 | let output = ''; 19 | for (var i = 0; i < this.length; i++) { 20 | const c = this.charCodeAt(i) 21 | const k = key[i % key.length] 22 | output += String.fromCharCode(c ^ k); 23 | } 24 | 25 | return output 26 | } 27 | 28 | module.exports = (text, key) => text.decrypt(key) 29 | -------------------------------------------------------------------------------- /js/encrypt.js: -------------------------------------------------------------------------------- 1 | /* ========================================================================== 2 | * 3 | * Use: 4 | * "Hello World!".encrypt(134) 5 | * => 'Îãêêé¦Ñéôêâ§' 6 | * 7 | * Or in the case of non-repeating keys: 8 | * 9 | * Use: 10 | * "Hello World!".encrypt([29, 62, 134]) 11 | * => 'U[êqQ¦JQôqZ§' 12 | * 13 | * ========================================================================== */ 14 | 15 | String.prototype.encrypt = function(key) { 16 | 17 | if (typeof key == 'number') key = [key] 18 | 19 | let output = ''; 20 | for (var i = 0; i < this.length; i++) { 21 | const c = this.charCodeAt(i) 22 | const k = key[i % key.length] 23 | output += String.fromCharCode(c ^ k); 24 | } 25 | 26 | return output 27 | } 28 | 29 | module.exports = (text, key) => text.encrypt(key) 30 | -------------------------------------------------------------------------------- /py/decrypt-v2.py: -------------------------------------------------------------------------------- 1 | # ============================================================================== 2 | # 3 | # Use: 4 | # decrypt("Îãêêé¦Ñéôêâ§", 134) 5 | # => 'Hello World!' 6 | # 7 | # Or in the case of non-repeating keys: 8 | # 9 | # Use: 10 | # decrypt("U[êqQ¦JQôqZ§", [29, 62, 134]) 11 | # => Hello World!' 12 | # 13 | # ============================================================================== 14 | 15 | def decrypt(text, key): 16 | if type(key) is int: key = [key] 17 | 18 | output = '' 19 | 20 | for i in range(len(text)): 21 | c = ord(text[i]) 22 | k = key[i % len(key)] 23 | output += chr(c ^ k) 24 | 25 | return output 26 | -------------------------------------------------------------------------------- /py/decrypt.py: -------------------------------------------------------------------------------- 1 | def decrypt(initial, key): 2 | """ Use (key is an 8 digits binary number) : decrypt("ÉÁ××ÅÃÁ", "10100100") 3 | => 'message' 4 | """ 5 | key = int(''.join(('0b', key)), 2) 6 | mylist = [] 7 | for i in range (len(initial)): 8 | mylist.append(int(''.join(('0b', str(format(ord(initial[i]), '08b')))), 2)) 9 | output = "" 10 | 11 | for i in range(0, len(mylist)): 12 | output += str(chr(mylist[i] ^ key)) 13 | return output 14 | -------------------------------------------------------------------------------- /py/encrypt-v2.py: -------------------------------------------------------------------------------- 1 | # ============================================================================== 2 | # 3 | # Use: 4 | # encrypt("Hello World!", 134) 5 | # => 'Îãêêé¦Ñéôêâ§' 6 | # 7 | # Or in the case of non-repeating keys: 8 | # 9 | # Use: 10 | # encrypt("Hello World!", [29, 62, 134]) 11 | # => 'U[êqQ¦JQôqZ§' 12 | # 13 | # ============================================================================== 14 | 15 | 16 | def encrypt(text, key): 17 | if type(key) is int: key = [key] 18 | 19 | output = '' 20 | 21 | for i in range(len(text)): 22 | c = ord(text[i]) 23 | k = key[i % len(key)] 24 | output += chr(c ^ k) 25 | 26 | return output 27 | -------------------------------------------------------------------------------- /py/encrypt.py: -------------------------------------------------------------------------------- 1 | def encrypt(initial, key): 2 | """ Use (key is an 8 digits binary number) : encrypt("message", "10100100") 3 | => 'ÉÁ××ÅÃÁ' 4 | """ 5 | key = int(''.join(('0b', key)), 2) 6 | mylist = [] 7 | for i in range (len(initial)): 8 | mylist.append(int(''.join(('0b', str(format(ord(initial[i]), '08b')))), 2)) 9 | output = "" 10 | 11 | for i in range(0, len(mylist)): 12 | output += str(chr(mylist[i] ^ key)) 13 | return output 14 | -------------------------------------------------------------------------------- /swift/lib.swift: -------------------------------------------------------------------------------- 1 | /********************************* 2 | * 3 | * Use: "Hello World!".encrypt([134]) 4 | * => "Îãêêé¦Ñéôêâ§" 5 | * "U[êqQ¦JQôqZ§".decrypt([134]) 6 | * => "Hello World!" 7 | * 8 | *********************************/ 9 | import Foundation 10 | 11 | //ascii code 12 | extension Character 13 | { 14 | func unicodeValue() -> UInt32 15 | { 16 | let characterString = String(self) 17 | let scalars = characterString.unicodeScalars 18 | 19 | return scalars[scalars.startIndex].value 20 | } 21 | } 22 | // main 23 | extension String { 24 | func encrypt(_ keyValue: Array) -> String { 25 | var out = ""; 26 | let inArray = Array(self); 27 | let keyArray = keyValue; 28 | for i in 0...self.count - 1 { 29 | let c = Int(inArray[i].unicodeValue()) 30 | let k = Int(keyArray[i % keyValue.count]) 31 | out += String(Character(UnicodeScalar(Int(c ^ k))!)) 32 | } 33 | return out 34 | } 35 | func decrypt(_ keyValue: Array) -> String { 36 | return self.encrypt(keyValue) 37 | } 38 | } 39 | --------------------------------------------------------------------------------