Skip to content

Latest commit

 

History

History
68 lines (55 loc) · 1.89 KB

File metadata and controls

68 lines (55 loc) · 1.89 KB

Solve a cryptogram

Cryptogram Solver

Build a solver

A CryptogramSolver must be configured before use, using a CryptogramSolverBuilder.

First of all, the an ISpeculativePlaintextScorer must be set to score each candidate:

var scorer = new SubstringSpeculativePlaintextScorer("solution");
var builder = new CryptogramSolverBuilder(scorer);

For more information on scoring, see Scoring.

Then, you have to add ciphers which are going to be tested by the solver:

builder.AddCihper(new BaconCipher());

Ciphers can be added multiple times with different configurations as well:

builder.AddCihper(new BaconCipher(new BaconOptions(A: 'B', B: 'A')));

For keyed ciphers, a key space must be configured:

builder.AddCipher(new ShiftCipher(), new IntKeySpace(1, 25));

The CryptogramSolver supports all kinds of key spaces.

Optionally, you can also provide your own ILogger instance to get logs:

builder.SetLogger(logger); // ILogger

Run

Full example:

var ciphertext = "aol zvsbapvu pz jyfwavnyht";
var scorer = new SubstringSpeculativePlaintextScorer("solution");
var promoter = new ProgressivelyBetterCandidatePromoter();

var solver = new CryptogramSolverBuilder(scorer)
	.AddCipher(new BaconCipher())
	.AddCipher(new TapCode())
	.AddCipher(new ShiftCipher(), new ShiftKeySpace())
	// ...
	.Build();

await foreach (var candidate in solver.SolveAsync(ciphertext, promoter))
{
	var cipher = candidate.Cipher.Cipher;
	if (candidate.Cipher.HasKey)
	{
		var key = candidate.Key;
		// ...
	}
	// ...
	var plaintext = candidate.SpeculativePlaintext.Plaintext;
	var score = candidate.SpeculativePlaintext.Score;
	// ...
}

Depth

CryptogramSolver supports only a single depth of encryption.