X Tutup
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Ciphers/XORCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* The XOR cipher is a type of additive cipher.
* Each character is bitwise XORed with the key.
* We loop through the input string, XORing each
* character with the key.
*/

/**
* Encrypt using an XOR cipher
* @param {String} str - String to be encrypted
* @param {Number} key - key for encryption
* @return {String} encrypted string
*/

function XOR (str, key) {
let result = ''
for (const elem of str) {
result += String.fromCharCode(elem.charCodeAt(0) ^ key)
}
return result
}

const encryptedString = XOR('test string', 32)
console.log('Encrypted: ', encryptedString)
const decryptedString = XOR(encryptedString, 32)
console.log('Decrypted: ', decryptedString)
X Tutup