forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXORCipher.js
More file actions
Latest commit
22 lines (20 loc) · 650 Bytes
/
Copy pathXORCipher.js
File metadata and controls
22 lines (20 loc) · 650 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @function XORCipher
* @description - Encrypt using an XOR cipher
* 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.
* @param {string} str - string to be encrypted
* @param {number} key - key for encryption
* @return {string} encrypted string
*/
constXORCipher=(str,key)=>{
if(typeofstr!=='string'||!Number.isInteger(key)){
thrownewTypeError('Arguments type are invalid')
}
returnstr.replace(
/./g,(char)=>String.fromCharCode(char.charCodeAt()^key)
)
}
exportdefaultXORCipher