Created
May 14, 2024 04:10
-
-
Save DrunkenAlcoholic/ba6d7bec566fd3b16c4574e6d0d7ae96 to your computer and use it in GitHub Desktop.
Atbash Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils, unicode | |
const Alpha = "abcdefghijklmnopqrstuvwxyz".reversed | |
proc encode*(s: string, decode: bool = false): string = | |
var str = s.replace(" ") | |
for i in 0..<str.len: | |
if str[i].toLowerAscii in Alpha: result.add(Alpha[ord(str[i].toLowerAscii) - 97]) | |
elif str[i] in {'0'..'9'}: result.add(str[i].toLowerAscii) | |
else: discard | |
if not decode: | |
str = result | |
result = "" | |
for i in 0..<str.len: | |
if i > 0 and i mod 5 == 0: result.add(" ") | |
result.add(str[i]) | |
proc decode*(s: string): string = | |
encode(s, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment