Sha256: 0c4dece258240eff9e8cb4b9c156467a7e056fd9bee49738cb12ab9dee97c75a

Contents?: true

Size: 1.75 KB

Versions: 379

Compression:

Stored size: 1.75 KB

Contents

using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

public class Cipher
{
    private const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    private static readonly Random Rand = new Random();

    public string Key { get; private set; }

    public Cipher()
    {
        Key = new string(Enumerable.Range(0, 100).Select(x => ALPHABET[Rand.Next(ALPHABET.Length)]).ToArray());
    }

    public Cipher(string key)
    {
        if (!IsValidKey(key)) throw new ArgumentException("Invalid key");
        Key = key;
    }

    private static bool IsValidKey(string key)
    {
        return Regex.IsMatch(key, "^[a-z]+$");
    }

    public string Encode(string plaintext)
    {
        var ciphertext = new StringBuilder(plaintext.Length);

        for (int i = 0; i < Math.Min(plaintext.Length, Key.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]);
        if (alphabetIdx >= ALPHABET.Length)
            alphabetIdx -= ALPHABET.Length;
        return ALPHABET[alphabetIdx];
    }

    public string Decode(string ciphertext)
    {
        var plaintext = new StringBuilder(ciphertext.Length);

        for (int 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]);
        if (alphabetIdx < 0)
            alphabetIdx += ALPHABET.Length;
        return ALPHABET[alphabetIdx];
    }
}

Version data entries

379 entries across 379 versions & 1 rubygems

Version Path
trackler-2.2.1.162 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.159 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.158 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.157 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.156 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.155 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.154 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.153 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.152 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.151 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.150 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.149 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.148 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.147 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.146 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.145 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.144 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.143 tracks/csharp/exercises/simple-cipher/Example.cs