26 April 2022

Checksums and early encryption

ISBN-13

The ISBN checksum, or indeed any checksum, is there to provide an easy check for the plausibility of a multi-digit sequence.  The same sort of technique is used for credit card numbers.

The idea is that the commonest errors are (1) the alteration of a single digit or (2) the swapping of 2 consecutive digits, and both of these will usually change the check digit. 

The check digit can obviously take only one of 10 different values and thus there is a 10% chance that a wrong set of digits will give the same checksum. However, by choosing different multipliers for consecutive digits the probability that swapping two consecutive digits yields the same checksum is much reduced - at the expense, of course, of slightly increasing the chance that a random error will result in an unchanged check digit.

The algorithm is pretty straightforward to code, and even in my rather wordy style is only 5 lines long.

Wheatstone-Playfair encryption

This is an interesting example of something which is relatively easy to describe, but requires rather more code than might at first appear.

My solution builds the 5x5 matrix as a 2D array, and simultaneously builds an index to it such that $locate{$letter}->[0] and $locate{$letter}->[1] give the coordinates of the given letter. Once that is done, the encrypt and decrypt functions are easily coded.

As one of the examples I used 'the quick brown fox ...' as the key to show that my technique works even if all the letters of the alphabet occur in the key, which is required if the bottom right cell of the matrix is not to be 'z'.

No comments:

Post a Comment