Vigenère cipher: Difference between revisions
Content added Content deleted
(Modified D version) |
(→{{header|D}}: less compact but faster) |
||
Line 318: | Line 318: | ||
<lang d>import std.stdio, std.string; |
<lang d>import std.stdio, std.string; |
||
string encrypt(string |
string encrypt(string txt, in string key) { |
||
string |
string res; |
||
foreach ( |
foreach (c; txt.toupper) { |
||
if (c < 'A' || c > 'Z') continue; |
|||
res ~= (c + key[res.length % $] - 2 * 'A') % 26 + 'A'; |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
string decrypt(string |
string decrypt(string txt, in string key) { |
||
string |
string res; |
||
foreach ( |
foreach (c; txt.toupper) { |
||
if (c < 'A' || c > 'Z') continue; |
|||
res ~= (c - key[res.length % $] + 26) % 26 + 'A'; |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||