シーザー暗号/C Sharp
記述例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);
}
}
関連項目編集
参考文献編集