Sha256: f7d8ac91d5eb423170922f34aa191fce7f1587bf04cec25c77916677603aaba5
Contents?: true
Size: 1.54 KB
Versions: 208
Compression:
Stored size: 1.54 KB
Contents
using System; using System.Collections.Generic; using System.Linq; public class Crypto { public Crypto(string input) { this.NormalizePlaintext = GetNormalizedPlaintext(input); this.Size = this.CalculateSize(); } public int Size { get; private set; } public string NormalizePlaintext { get; private set; } public string NormalizeCiphertext() { return string.Join(" ", Transpose(this.PlaintextSegments())); } public string Ciphertext() { return string.Join("", Transpose(this.PlaintextSegments())); } public IEnumerable<string> PlaintextSegments() { return Chunks(this.NormalizePlaintext, this.Size); } private int CalculateSize() { return (int) Math.Ceiling(Math.Sqrt(this.NormalizePlaintext.Length)); } private static string GetNormalizedPlaintext(string input) { return new string(input.ToLowerInvariant().Where(char.IsLetterOrDigit).ToArray()); } private static IEnumerable<string> Chunks(string str, int chunkSize) { return Enumerable.Range(0, (int)Math.Ceiling(str.Length / (double)chunkSize)) .Select(i => str.Substring(i * chunkSize, Math.Min(str.Length - i * chunkSize, chunkSize))); } private static IEnumerable<string> Transpose(IEnumerable<string> input) { return input.SelectMany(s => s.Select((c, i) => Tuple.Create(i, c))) .GroupBy(x => x.Item1) .Select(g => new string(g.Select(t => t.Item2).ToArray())); } }
Version data entries
208 entries across 208 versions & 1 rubygems