シーザー暗号/C Sharp

提供: MonoBook
ナビゲーションに移動 検索に移動

C#によるシーザー暗号の記述例。

記述例1[編集 | ソースを編集]

The Zen of Pythonソースコードを見ながら、The Zen of Pythonがデコードできるかで動作確認した。それ以外では試していない即席コードなので何かしらのバグがあるかもしれない。

public static class CaesarCipher
{
    public static string Decode(string enc, int rot = 13)
    {
        int idx;
        int pos;
        var lut1 = new char[26];
        var lut2 = new char[26];
        for (idx = 0; idx < 26; idx++)
        {
            pos = (idx + rot) % 26;
            lut1[idx] = (char)('A' + pos);
            lut2[idx] = (char)('a' + pos);
        }

        int len = enc.Length;
        var dec = new char[len];
        char one;
        for (idx = 0; idx < len; idx++)
        {
            one = enc[idx];
            if ('A' <= one && one <= 'Z')
            {
                dec[idx] = lut1[one - 'A'];
            }
            else if ('a' <= one && one <= 'z')
            {
                dec[idx] = lut2[one - 'a'];
            }
            else
            {
                dec[idx] = one;
            }
        }
        return new string(dec);
    }
}

関連項目[編集 | ソースを編集]

参考文献[編集 | ソースを編集]


外部リンク[編集 | ソースを編集]