Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3874ec3 commit 4be7a85Copy full SHA for 4be7a85
1 file changed
Ciphers/XORCipher.js
@@ -0,0 +1,26 @@
1
+/**
2
+ * The XOR cipher is a type of additive cipher.
3
+ * Each character is bitwise XORed with the key.
4
+ * We loop through the input string, XORing each
5
+ * character with the key.
6
+ */
7
+
8
9
+ * Encrypt using an XOR cipher
10
+ * @param {String} str - String to be encrypted
11
+ * @param {Number} key - key for encryption
12
+ * @return {String} encrypted string
13
14
15
+function XOR (str, key) {
16
+ let result = ''
17
+ for (const elem of str) {
18
+ result += String.fromCharCode(elem.charCodeAt(0) ^ key)
19
+ }
20
+ return result
21
+}
22
23
+const encryptedString = XOR('test string', 32)
24
+console.log('Encrypted: ', encryptedString)
25
+const decryptedString = XOR(encryptedString, 32)
26
+console.log('Decrypted: ', decryptedString)
0 commit comments