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.122 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.121 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.120 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.119 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.118 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.117 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.116 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.115 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.114 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.113 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.111 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.110 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.109 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.108 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.107 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.106 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.105 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.104 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.103 tracks/csharp/exercises/simple-cipher/Example.cs
trackler-2.2.1.102 tracks/csharp/exercises/simple-cipher/Example.cs