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(in string text, in string key) {
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(in string encoded, in string key) {
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 ori = "Beware the Jabberwock, my son!" ~
auto original = "Beware the Jabberwock, my son!" ~
" The jaws that bite, the claws that catch!";
" The jaws that bite, the claws that catch!";
auto enc = encrypt(ori, key);
auto encoded = encrypt(original, key);
writeln(enc, "\n", decrypt(enc, key));
writeln(encoded, "\n", decrypt(encoded, key));
}</lang>
}</lang>
Output:
Output: