Skip to content

About

Message encryptor/decryptor using XOR cypher.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Encryption.js

XOR Encryption

The best form of encryption is the one where finding the key is equally difficult as finding the message. The use of the exclusive or (XOR) logical operation allows us to encrypt and decrypt a message using a key of the same length as the message.

The XOR (⊕) operation is only true when only one of the conditions is true.

A B A ⊕ B
false false false
false true true
true false true
true true false

Let's take a look at this example, we are going to encrypt 8 bits with an 8-bit key.

message key encrypted
0 1 1
1 0 1
0 1 1
0 0 0
0 0 0
1 0 1
0 1 1
1 1 0

Again using the XOR operation, we are going to decrypt the encrypted message with the same key.

encrypted key message
1 1 0
1 0 1
1 1 0
0 0 0
0 0 0
1 0 1
1 1 0
0 1 1

We obtain the same initial message we encrypted.

Functions

mulberry32(number) 🡆 number
Takes seed and returns pseudo-random number between 0 and 1.

$ mulberry32(69420) 
▶ 0.5890528310555965

numberArrayToString(array<number>) 🡆 string
Takes array of numbers and returns a string using the following characters.
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

$ numberArrayToString([0.69, 0.420, 0.4815162342, 0.5890528310555965])
▶ 'qadk'

seedToNumberArray(seed:number, lenght:number) 🡆 array<number>
Takes a seed and a lenght and returns a number array of that lenght.

$ seedToNumberArray(69, 6)
▶ [0.7964120346587151, 0.9098359290510416, 0.12758133537136018, 0.7879517334513366, 0.7471530991606414, 0.7873547316994518]

passwordToSeed(string) 🡆 number
Takes a string and returns a number seed.

$ passwordToSeed('hey mom')
▶ 30803286948406630

randomString(number) 🡆 string
Takes lenght and returns string of that length with the following characters:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'

$ randomString(69)
▶ 'M0zlj7PFFQncz24zZpg4ABAkKIMkIagYDIcw1BXMg7R2Trh4Lm2F2Up7NvgP9LHA5z0MG'

utf8toByteArray(string) 🡆 byte array
Takes UTF-8 encoded string and returns its byte array.

$ utf8toByteArray('hippity dippity')
▶ Uint8Array(15) [104, 105, 112, 112, 105, 116, 121, 32, 100, 105, 112, 112, 105, 116, 121, buffer: ArrayBuffer(15), byteLength: 15, byteOffset: 0, length: 15, Symbol(Symbol.toStringTag): 'Uint8Array']

byteArrayToUtf8(byte array) 🡆 string
Takes bytes array and encodes it to Unicode (UTF-8) string.

$ byteArrayToUtf8([104, 105, 112, 112, 105, 116, 121, 32, 100, 105, 112, 112, 105, 116, 121])
▶ 'hippity dippity'

xorEncrypt(byte array, byte array) 🡆 byte array
Encrypts a byte array using XOR logical operation and key of the same length.

$ xorEncrypt([4,8,15,16,23,42],[30,60,90,120,150,180])
▶ [26, 52, 85, 104, 129, 158]

utf8ToBase64(unicode string) 🡆 B64 string
Converts a UTF-8 encoded string to base 64.

$ utf8ToBase64('i am iron man')
▶ 'aSBhbSBpcm9uIG1hbg=='

base64ToUtf8(B64 string) 🡆 UTF-8 string
Converts a base 64 encoded string to Unicode (UTF-8).

$ base64ToUtf8('aSBhbSBpcm9uIG1hbg==')
▶ 'i am iron man'

Stolen borrowed code:

mulberry32 => Tommy Ettinger + bryc for javascript implementation
utf8ToBase64, base64ToUtf8 => brandonscript

About

Message encryptor/decryptor using XOR cypher.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages