File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments