Vigenère cipher: Difference between revisions

Improved D code, removed undefined code (See C codepoints)
(Improved D code, removed undefined code (See C codepoints))
Line 316:
=={{header|D}}==
{{trans|C++}}
ASCII only:
<lang d>import std.stdio, std.string, std.ctype;
 
string encrypt(string text, in string key) {
string res;
int j;
 
foreach (c; text.toupper) {
if (!isupper(c < 'A' || c > 'Z') continue;)
res ~= (c + key[j] - 2 * 'A') % 26 + 'A'continue;
jres ~= (c ++ key[j] - 2 * 'A') % key.length26 + 'A';
j++;
j %= key.length;
}
 
return res;
}
 
string decrypt(string text, in string key) {
string res;
int j;
 
foreach (c; text.toupper) {
if (!isupper(c < 'A' || c > 'Z') continue;)
res ~= (c - key[j] + 26) % 26 + 'A'continue;
jres ~= ++(c - key[j] + 26) % key.length26 + 'A';
j++;
j %= key.length;
}
 
return res;
}
 
void main() {
auto key = "VIGENERECIPHER";
auto ori = "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);
writeln(enc);
Anonymous user