メインメニューを開く

差分

シーザー暗号/C Sharp

1,392 バイト追加, 2012年5月23日 (水) 02:44
ページの作成:「C#によるシーザー暗号の記述例。 == 記述例1 == The Zen of Pythonソースコードを見ながら、The Zen of Pythonが...」
[[C Sharp|C#]]による[[シーザー暗号]]の記述例。

== 記述例1 ==
[[The Zen of Python]]の[[ソースコード]]を見ながら、[[The Zen of Python]]がデコードできるかで動作確認した。それ以外では試していない即席コードなので何かしらの[[バグ]]があるかもしれない。
<source lang="csharp">
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);
}
}
</source>

== 関連項目 ==
* [[C Sharp|C#]]
* [[シーザー暗号]]

== 参考文献 ==
<references/>

== 外部リンク ==

{{stub}}
匿名利用者