File tree Expand file tree Collapse file tree 2 files changed +34
-26
lines changed
Expand file tree Collapse file tree 2 files changed +34
-26
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ /*
2+ The Atbash cipher is a particular type of monoalphabetic cipher
3+ formed by taking the alphabet and mapping it to its reverse,
4+ so that the first letter becomes the last letter,
5+ the second letter becomes the second to last letter, and so on.
6+ */
7+
8+
9+ /**
10+ * Decrypt a Atbash cipher
11+ * @param {String } str - string to be decrypted/encrypt
12+ * @return {String } decrypted/encrypted string
13+ */
14+
15+ const Atbash = ( message ) => {
16+ let decodedString = ''
17+
18+ for ( let i = 0 ; i < message . length ; i ++ ) {
19+ if ( / [ ^ a - z A - Z ] / . test ( message [ i ] ) ) {
20+ decodedString += message [ i ]
21+ } else if ( message [ i ] === message [ i ] . toUpperCase ( ) ) {
22+ decodedString += String . fromCharCode ( 90 + 65 - message . charCodeAt ( i ) )
23+ } else {
24+ decodedString += String . fromCharCode ( 122 + 97 - message . charCodeAt ( i ) )
25+ }
26+ }
27+ return decodedString
28+ }
29+
30+ // Atbash Example
31+ const encryptedString = 'HELLO WORLD'
32+ const decryptedString = Atbash ( encryptedString )
33+
34+ console . log ( decryptedString ) // SVOOL DLIOW
You can’t perform that action at this time.
0 commit comments