The Laws of Cryptography:
The U.S. Banking 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 simple scheme used by U.S. banks, involving successive weights of 3, 7 and 1, repeated.


// 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();
   }
}

// BanksErrorDetection.java: Implement the scheme used by US banks for checks public class BanksErrorDetection extends ErrorDetection { public static int insertCheck(int[] a) { int check = 0; for (int i = 1; i < a.length; i++) if (i%3 == 1) check = (check + 3*a[i])%10; else if (i%3 == 2) check = (check + 7*a[i])%10; else check = (check + a[i])%10; if (check == 0) a[0] = 0; else a[0] = -check + 10; return a[0]; } public static boolean doCheck(int[] a) { int check = 0; for (int i = 0; i < a.length; i++) if (i%3 == 1) check = (check + 3*a[i])%10; else if (i%3 == 2) check = (check + 7*a[i])%10; else check = (check + a[i])%10; 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); printUnchecked(a); BanksErrorDetection.insertCheck(a); printArray(a); System.out.println(BanksErrorDetection.doCheck(a)); a[4] = (a[4] + 1)%10; BanksErrorDetection.printArray(a); System.out.println(BanksErrorDetection.doCheck(a)); // test all adjacent transpositions System.out.println("\nUS Banks, 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; BanksErrorDetection.insertCheck(a); // interchange a[pos] ^= a[pos+1]; a[pos+1] ^= a[pos]; a[pos] ^= a[pos+1]; if (BanksErrorDetection.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, showing a simple test, and a test of all adjacent interchanges. Here if digits differing by 5 are interchanged, the error goes undetected. I have tested interchanges for each pair of the three weights, so the 10 missed transpositions are repeated 3 times below.
? 95967 315
1 95967 315
true
1 95977 315
false

US Banks, error detection scheme

Test all adjacent transpositions ...
Warning: Interchange of 0 and 5 not detected
Warning: Interchange of 1 and 6 not detected
Warning: Interchange of 2 and 7 not detected
Warning: Interchange of 3 and 8 not detected
Warning: Interchange of 4 and 9 not detected
Warning: Interchange of 5 and 0 not detected
Warning: Interchange of 6 and 1 not detected
Warning: Interchange of 7 and 2 not detected
Warning: Interchange of 8 and 3 not detected
Warning: Interchange of 9 and 4 not detected
Warning: Interchange of 0 and 5 not detected
Warning: Interchange of 1 and 6 not detected
Warning: Interchange of 2 and 7 not detected
Warning: Interchange of 3 and 8 not detected
Warning: Interchange of 4 and 9 not detected
Warning: Interchange of 5 and 0 not detected
Warning: Interchange of 6 and 1 not detected
Warning: Interchange of 7 and 2 not detected
Warning: Interchange of 8 and 3 not detected
Warning: Interchange of 9 and 4 not detected
Warning: Interchange of 0 and 5 not detected
Warning: Interchange of 1 and 6 not detected
Warning: Interchange of 2 and 7 not detected
Warning: Interchange of 3 and 8 not detected
Warning: Interchange of 4 and 9 not detected
Warning: Interchange of 5 and 0 not detected
Warning: Interchange of 6 and 1 not detected
Warning: Interchange of 7 and 2 not detected
Warning: Interchange of 8 and 3 not detected
Warning: Interchange of 9 and 4 not detected
At least one transposition undetected