Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/plugfox/xor_cipher

The XOR Encryption algorithm is an effective and easy-to-implement method of symmetric encryption.
https://github.com/plugfox/xor_cipher

cipher cryptography dart dartlang symmetric-cryptography xor xor-cipher

Last synced: about 1 month ago
JSON representation

The XOR Encryption algorithm is an effective and easy-to-implement method of symmetric encryption.

Awesome Lists containing this project

README

        

# Symmetric XOR cipher library

[![platform_info](https://img.shields.io/pub/v/xor_cipher.svg)](https://pub.dev/packages/xor_cipher)
[![Actions Status](https://github.com/PlugFox/xor_cipher/actions/workflows/checkout.yml/badge.svg)](https://github.com/PlugFox/xor_cipher/actions/workflows/checkout.yml)
[![Coverage](https://codecov.io/gh/PlugFox/xor_cipher/branch/master/graph/badge.svg)](https://codecov.io/gh/PlugFox/xor_cipher)
[![License: MIT](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT)
[![Linter](https://img.shields.io/badge/style-linter-40c4ff.svg)](https://dart-lang.github.io/linter/lints/)


## About XOR cipher

XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating random encryption keys to match with the correct one.
The XOR Encryption algorithm is a very effective yet easy to implement method of symmetric encryption. Due to its effectiveness and simplicity, the XOR Encryption is an extremely common component used in more complex encryption algorithms used nowadays.
In [cryptography](https://en.wikipedia.org/wiki/Cryptography "Cryptography"), the **simple XOR cipher** is a type of _additive cipher_, an [encryption algorithm](https://en.wikipedia.org/wiki/Encryption_algorithm "Encryption algorithm") that operates according to the principles:
```
A XOR 0 = A,
A XOR A = 0,
A XOR B = B XOR A,
(A XOR B) XOR C = A XOR (B XOR C),
(B XOR A) XOR A = B XOR 0 = B
```
where `XOR` denotes the [exclusive disjunction](https://en.wikipedia.org/wiki/Exclusive_disjunction "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.

The XOR operator is extremely common as a component in more complex ciphers. By itself, using a constant repeating key, a simple XOR cipher can trivially be broken using [frequency analysis](https://en.wikipedia.org/wiki/Frequency_analysis "Frequency analysis"). If the content of any message can be guessed or otherwise known then the key can be revealed. Its primary merit is that it is simple to implement, and that the XOR operation is computationally inexpensive. A simple repeating XOR (i.e. using the same key for xor operation on the whole data) cipher is therefore sometimes used for hiding information in cases where no particular security is required. The XOR cipher is often used in computer malware to make reverse engineering more difficult.

If the key is random and is at least as long as the message, the XOR cipher is much more secure than when there is key repetition within a message. When the keystream is generated by a [pseudo-random number generator](https://en.wikipedia.org/wiki/Pseudo-random_number_generator "Pseudo-random number generator"), the result is a [stream cipher](https://en.wikipedia.org/wiki/Stream_cipher "Stream cipher"). With a key that is [truly random](https://en.wikipedia.org/wiki/Hardware_random_number_generator "Hardware random number generator"), the result is a [one-time pad](https://en.wikipedia.org/wiki/One-time_pad "One-time pad"), which is [unbreakable in theory](https://en.wikipedia.org/wiki/Information-theoretic_security "Information-theoretic security").

The XOR operator in any of these ciphers is vulnerable to a [known-plaintext attack](https://en.wikipedia.org/wiki/Known-plaintext_attack "Known-plaintext attack"), since _plaintext_ ^ _ciphertext_ = _key_. It is also trivial to flip arbitrary bits in the decrypted plaintext by manipulating the ciphertext. This is called [malleability](https://en.wikipedia.org/wiki/Malleability_(cryptography) "Malleability (cryptography)").

## Usage example

```dart
import 'package:xor_cipher/xor_cipher.dart';

void main() {
const source = 'Hello 🦊 world!!!';
const secret = 'Top 😺 secret';
print(
'Source: $source\n'
'Secret: $secret',
);
final encrypted = XOR.encrypt(source, secret, urlEncode: true);
print('Encrypted: $encrypted');
final decrypted = XOR.decrypt(encrypted, secret, urlDecode: true);
print(
'Decrypted: $decrypted\n'
'Identical: ${identical(source, decrypted)}',
);
}
```


## Coverage

[![](https://codecov.io/gh/PlugFox/xor_cipher/branch/master/graphs/sunburst.svg)](https://codecov.io/gh/PlugFox/xor_cipher/branch/master)


## Changelog

Refer to the [Changelog](https://github.com/plugfox/xor_cipher/blob/master/CHANGELOG.md) to get all release notes.


## Maintainers

[Plague Fox](https://plugfox.dev)


## License

[MIT](https://github.com/plugfox/xor_cipher/blob/master/LICENSE)