Sha256: a1616149f319304a5bf76336025a5b83c38d342cde52265d9bfde7bb08499e2f
Contents?: true
Size: 1.24 KB
Versions: 271
Compression:
Stored size: 1.24 KB
Contents
using System; using System.Collections.Generic; using System.Linq; public static class AtbashCipher { private const int BlockSize = 5; private const string Plain = "abcdefghijklmnopqrstuvwxyz0123456789"; private const string Cipher = "zyxwvutsrqponmlkjihgfedcba0123456789"; public static string Encode(string plainValue) => string.Join(" ", EncodeInBlocks(GetEncodedCharacters(plainValue))); public static string Decode(string encodedValue) => new string(encodedValue.Replace(" ", "").Select(Decode).ToArray()); private static IEnumerable<char> GetEncodedCharacters(string words) => GetValidCharacters(words).Select(Encode); private static IEnumerable<char> GetValidCharacters(string words) => words.ToLowerInvariant().Where(char.IsLetterOrDigit); private static char Encode(char c) => Cipher[Plain.IndexOf(c)]; private static char Decode(char c) => Plain[Cipher.IndexOf(c)]; private static IEnumerable<string> EncodeInBlocks(IEnumerable<char> value) { var valueAsString = new string(value.ToArray()); for (var i = 0; i < valueAsString.Length; i += BlockSize) yield return valueAsString.Substring(i, Math.Min(BlockSize, valueAsString.Length - i)); } }
Version data entries
271 entries across 271 versions & 1 rubygems