Vigenère cipher: Difference between revisions

Content added Content deleted
(→‎{{header|PicoLisp}}: Added PureBasic)
(Improved D code)
Line 317:
{{trans|C++}}
ASCII only:
<lang d>import std.stdio, std.string, std.ctype;
 
string encrypt(in string text, in string key) {
string resencoded;
foreach (i, c; text.toupper().removechars("^A-Z")) {
int j;
resencoded ~= (c + key[ji % $] - 2 * 'A') % 26 + 'A';
 
return resencoded;
foreach (c; text.toupper) {
if (!isupper(c))
continue;
res ~= (c + key[j] - 2 * 'A') % 26 + 'A';
j++;
j %= key.length;
}
 
return res;
}
 
string decrypt(in string textencoded, in string key) {
string resdecoded;
foreach (i, c; textencoded.toupper().removechars("^A-Z")) {
int j;
resdecoded ~= (c - key[ji % $] + 26) % 26 + 'A';
 
return resdecoded;
foreach (c; text.toupper) {
if (!isupper(c))
continue;
res ~= (c - key[j] + 26) % 26 + 'A';
j++;
j %= key.length;
}
 
return res;
}
 
void main() {
auto key = "VIGENERECIPHER";
Line 354 ⟶ 338:
" The jaws that bite, the claws that catch!";
auto enc = encrypt(ori, key);
writeln(enc, "\n", decrypt(enc, key));
writeln(decrypt(enc, key));
}</lang>
Output:
 
<pre>WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>