Skip to content

Instantly share code, notes, and snippets.

@DrunkenAlcoholic
Created May 14, 2024 04:10
Show Gist options
  • Save DrunkenAlcoholic/ba6d7bec566fd3b16c4574e6d0d7ae96 to your computer and use it in GitHub Desktop.
Save DrunkenAlcoholic/ba6d7bec566fd3b16c4574e6d0d7ae96 to your computer and use it in GitHub Desktop.
Atbash Cipher
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