Atbash
# 实现
# JavaScript
/**
* @function Atbash - Decrypt a Atbash cipher
* @description - The Atbash cipher is a particular type of monoalphabetic cipher formed by taking the alphabet and mapping it to its reverse, so that the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.
* @param {string} str - string to be decrypted/encrypt
* @return {string} decrypted/encrypted string
* @see - [wiki](https://en.wikipedia.org/wiki/Atbash)
*/
const Atbash = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
return str.replace(/[a-z]/gi, (char) => {
const charCode = char.charCodeAt()
if (/[A-Z]/.test(char)) {
return String.fromCharCode(90 + 65 - charCode)
}
return String.fromCharCode(122 + 97 - charCode)
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编辑 (opens new window)
上次更新: 2022/10/28, 17:30:16