Vigenère cipher: Difference between revisions
Content added Content deleted
(→{{header|D}}: less compact but faster) |
(→{{header|D}}: handle multibyte chars) |
||
Line 320: | Line 320: | ||
string encrypt(string txt, in string key) { |
string encrypt(string txt, in string key) { |
||
string res; |
string res; |
||
foreach (c; txt.toupper) { |
foreach (dchar c; txt.toupper) { |
||
if (c < 'A' || c > 'Z') continue; |
if (c < 'A' || c > 'Z') continue; |
||
res ~= (c + key[res.length % $] - 2 * 'A') % 26 + 'A'; |
res ~= (c + key[res.length % $] - 2 * 'A') % 26 + 'A'; |
||
Line 329: | Line 329: | ||
string decrypt(string txt, in string key) { |
string decrypt(string txt, in string key) { |
||
string res; |
string res; |
||
foreach (c; txt.toupper) { |
foreach (dchar c; txt.toupper) { |
||
if (c < 'A' || c > 'Z') continue; |
if (c < 'A' || c > 'Z') continue; |
||
res ~= (c - key[res.length % $] + 26) % 26 + 'A'; |
res ~= (c - key[res.length % $] + 26) % 26 + 'A'; |