Sha256: 3fe6aadd62d224624f27ab7efe6f5bebaf4f1f2f6ff0f245073143de49af0927
Contents?: true
Size: 1.73 KB
Versions: 17
Compression:
Stored size: 1.73 KB
Contents
using System; using System.Linq; using System.Text; using System.Text.RegularExpressions; public class SimpleCipher { private const string Alphabet = "abcdefghijklmnopqrstuvwxyz"; private static readonly Random Rand = new Random(); public string Key { get; } public SimpleCipher() { Key = new string(Enumerable.Range(0, 100).Select(x => Alphabet[Rand.Next(Alphabet.Length)]).ToArray()); } public SimpleCipher(string key) { Key = IsValidKey(key) ? key : throw new ArgumentException("Invalid key"); } private static bool IsValidKey(string key) => Regex.IsMatch(key, "^[a-z]+$"); public string Encode(string plaintext) { var ciphertext = new StringBuilder(plaintext.Length); for (var i = 0; i < plaintext.Length; i++) ciphertext.Append(EncodeCharacter(plaintext, i)); return ciphertext.ToString(); } private char EncodeCharacter(string plaintext, int idx) { var alphabetIdx = Alphabet.IndexOf(plaintext[idx]) + Alphabet.IndexOf(Key[idx % Key.Length]); if (alphabetIdx >= Alphabet.Length) alphabetIdx -= Alphabet.Length; return Alphabet[alphabetIdx]; } public string Decode(string ciphertext) { var plaintext = new StringBuilder(ciphertext.Length); for (var i = 0; i < ciphertext.Length; i++) plaintext.Append(DecodeCharacter(ciphertext, i)); return plaintext.ToString(); } private char DecodeCharacter(string ciphertext, int idx) { var alphabetIdx = Alphabet.IndexOf(ciphertext[idx]) - Alphabet.IndexOf(Key[idx % Key.Length]); if (alphabetIdx < 0) alphabetIdx += Alphabet.Length; return Alphabet[alphabetIdx]; } }
Version data entries
17 entries across 17 versions & 1 rubygems