Vigenère cipher: Difference between revisions
Content added Content deleted
(Improved D code, removed undefined code (See C codepoints)) |
|||
Line 316: | Line 316: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
ASCII only: |
|||
<lang d>import std.stdio, std.string; |
<lang d>import std.stdio, std.string, std.ctype; |
||
string encrypt(string text, in string key) { |
string encrypt(string text, in string key) { |
||
string res; |
string res; |
||
int j; |
int j; |
||
foreach (c; text.toupper) { |
foreach (c; text.toupper) { |
||
if (c |
if (!isupper(c)) |
||
continue; |
|||
res ~= (c + key[j] - 2 * 'A') % 26 + 'A'; |
|||
j++; |
|||
j %= key.length; |
|||
} |
} |
||
return res; |
return res; |
||
} |
} |
||
string decrypt(string text, in string key) { |
string decrypt(string text, in string key) { |
||
string res; |
string res; |
||
int j; |
int j; |
||
foreach (c; text.toupper) { |
foreach (c; text.toupper) { |
||
if (c |
if (!isupper(c)) |
||
continue; |
|||
res ~= (c - key[j] + 26) % 26 + 'A'; |
|||
j++; |
|||
j %= key.length; |
|||
} |
} |
||
return res; |
return res; |
||
} |
} |
||
void main() { |
void main() { |
||
auto key = "VIGENERECIPHER"; |
auto key = "VIGENERECIPHER"; |
||
auto ori = "Beware the Jabberwock, my son! |
auto ori = "Beware the Jabberwock, my son!" ~ |
||
" The jaws that bite, the claws that catch!"; |
|||
auto enc = encrypt(ori, key); |
auto enc = encrypt(ori, key); |
||
writeln(enc); |
writeln(enc); |