The Laws of Cryptography:
The ISBN Scheme

by Neal R. Wagner

Copyright © 2001 by Neal R. Wagner. All rights reserved.

NOTE: This site is obsolete. See book draft (in PDF):

Here is the ISBN mod 11 scheme used for book publishing numbers.


// ErrorDetection.java: base class for single-digit error detection
public class ErrorDetection {

   public static void printArray(int[] a) {
      for (int i = 0; i < a.length; i++) {
      	 if (a[i] == 10)
      	    System.out.print("X ");
      	 else
      	    System.out.print(a[i]);
      	 if (i%5 == 0) System.out.print(" ");
      }
      System.out.println();
   }
   
   public static void printUnchecked(int[] a) {
   	  System.out.print("? ");
      for (int i = 1; i < a.length; i++) {
      	 System.out.print(a[i]);
      	 if (i%5 == 0) System.out.print(" ");
      }
      System.out.println();
   }
}

// ISBNErrorDetection.java: Implement the mod 11 check used by ISBN numbers, public class ISBNErrorDetection extends ErrorDetection { public static int insertCheck(int[] a) { int check = 0; for (int i = 1; i < a.length; i++) check = (check + (i%10 + 1)*a[i])%11; if (check == 0) a[0] = 0; else a[0] = -check + 11; return a[0]; } public static boolean doCheck(int[] a) { int check = 0; for (int i = 0; i < a.length; i++) check = (check + (i%10 + 1)*a[i])%11; if (check != 0) return false; else return true; } // main function public static void main (String[] args) { int[] a = new int[9]; boolean checkFlag = false; for (int i = 1; i < a.length; i++) a[i] = (int)(Math.random() * 10.0); ISBNErrorDetection.printUnchecked(a); ISBNErrorDetection.insertCheck(a); ISBNErrorDetection.printArray(a); System.out.println(ISBNErrorDetection.doCheck(a)); a[4] = (a[4] + 3)%10; ISBNErrorDetection.printArray(a); System.out.println(ISBNErrorDetection.doCheck(a)); // test all adjacent transpositions System.out.println("\nISBN error detection scheme"); System.out.println("\nTest all adjacent transpositions ..."); for (int pos = 4; pos < 7; pos++) for (int p1 = 0; p1 < 10; p1++) for (int p2 = 0; p2 < 10; p2++) { if (p1 != p2) { a[pos] = p1; a[pos+1] = p2; ISBNErrorDetection.insertCheck(a); // interchange a[pos] ^= a[pos+1]; a[pos+1] ^= a[pos]; a[pos] ^= a[pos+1]; if (ISBNErrorDetection.doCheck(a)) { System.out.println("Warning: Interchange of " + p1 + " and " + p2 + " not detected"); checkFlag = true; } } } if (checkFlag) System.out.println("At least one transposition undetected"); else System.out.println("All transpositions detected"); } // end of main }

Here is the output, first showing a simple test. I tweaked the test until the check "digit" was an "X". Next is a test of all adjacent interchanges. Here all interchanges are caught.
? 11696 554
X  11696 554
true
X  11626 554
false

ISBN error detection scheme

Test all adjacent transpositions ...
All transpositions detected