Vigenère cipher/Cryptanalysis: Difference between revisions

Content deleted Content added
Longer but more structured D code
Updated D code
Line 470:
 
=={{header|D}}==
{{works with|DMD 2.057058}}
{{trans|C++}}
<lang d>import std.stdio, std.algorithm, std.typecons, std.string,
Line 483:
foreach (c; txt)
charCounts[c - 'A']++;
return dotProduct(sort(charCounts[]).release(), sTargets);
}
 
static frequency(in string txt) pure nothrow {
auto freqs = new Tuple!(char,"c", uint,"d")[nalpha];
foreach (i, const char c; uppercase)
freqs[i] = tuple(c, 0);
foreach (c; txt)
Line 499:
assert(key.length > 0);
string decoded;
foreach (i, const char c; cleaned)
decoded ~= (c - key[i % $] + nalpha) % nalpha + 'A';
return [key, decoded];
Line 535:
in double[] targetFreqs) /*pure nothrow*/ {
auto pieces = new string[bestLength];
foreach (i, const char c; cleaned)
pieces[i % bestLength] ~= c;
auto freqs = array(map!frequency(pieces));
Line 545:
size_t m;
double maxCorr = 0.0;
foreach (j, const char c; uppercase) {
double corr = 0.0;
foreach (k; 0 .. nalpha) {
Line 564:
}
 
//immutable cleaned = input.toUpper().removechars("^A-Z");
immutable cleaned = input.toUpper()[].removechars("^A-Z");
 
//immutable sortedTargets = sorted(targetFreqs);
Line 606 ⟶ 605:
writefln("Key: %s\n\nText: %s", key_dec[0], key_dec[1]);
}</lang>
Output (cut):
<pre>Key: THECHESHIRECAT
 
Text: THISWASTHEPOEMTHATALICEREADJABBERWOCKYTWASBRILLIGANDTHESLITHY...</pre>
 
=={{header|Python}}==