Vigenère cipher: Difference between revisions
Content added Content deleted
(Improved D code) |
(Modified D version) |
||
Line 315: | Line 315: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
|||
ASCII only: |
ASCII only: |
||
<lang d>import std.stdio, std.string; |
<lang d>import std.stdio, std.string; |
||
string encrypt( |
string encrypt(string text, in string key) { |
||
string encoded; |
string encoded; |
||
foreach (i, c; text.toupper().removechars("^A-Z")) |
foreach (i, c; text.toupper().removechars("^A-Z")) |
||
Line 325: | Line 324: | ||
return encoded; |
return encoded; |
||
} |
} |
||
string decrypt( |
string decrypt(string encoded, in string key) { |
||
string decoded; |
string decoded; |
||
foreach (i, c; encoded.toupper().removechars("^A-Z")) |
foreach (i, c; encoded.toupper().removechars("^A-Z")) |
||
Line 332: | Line 331: | ||
return decoded; |
return decoded; |
||
} |
} |
||
void main() { |
void main() { |
||
auto key = "VIGENERECIPHER"; |
auto key = "VIGENERECIPHER"; |
||
auto |
auto original = "Beware the Jabberwock, my son!" ~ |
||
" The jaws that bite, the claws that catch!"; |
" The jaws that bite, the claws that catch!"; |
||
auto |
auto encoded = encrypt(original, key); |
||
writeln( |
writeln(encoded, "\n", decrypt(encoded, key)); |
||
}</lang> |
}</lang> |
||
Output: |
Output: |