📄 kat.java
字号:
} /** main action. */ void run() { long time = System.currentTimeMillis(); if (varKey) vkKAT(vkFileName); if (varText) vtKAT(vtFileName); notify("Java interpreter used: Version "+System.getProperty("java.version")); notify("Java Just-In-Time (JIT) compiler: "+System.getProperty("java.compiler")); // print timing and stats info notify("Total execution time (ms): "+(System.currentTimeMillis() - time)); notify("During this time, "+cipherName+":"); notify(" Encrypted "+encBlocks+" blocks"); notify(" Decrypted "+ decBlocks+" blocks"); notify(" Created "+keyCount+" session keys"); }// Variable Key KAT methods//........................................................................... void vkKAT (String fileName) { File f = new File(destination, fileName); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(f) , true); } catch (IOException x) { halt("Unable to initialize <" + fileName + "> as a Writer:\n" + x.getMessage()); } out.println(); out.println("========================="); out.println(); out.println("FILENAME: \"" + fileName + "\""); out.println(); out.println("Electronic Codebook (ECB) Mode"); out.println("Variable Key Known Answer Tests"); out.println(); out.println("Algorithm Name: " + cipherName); out.println("Principal Submitter: " + SUBMITTER); out.println(); try { for (int k = 0; k < keys.length; k++) vkForKey(keys[k], out); } catch (Exception x) { halt("Exception encountered in a " + cipherName + "_Algorithm method:\n" + x.getMessage()); } out.println("=========="); out.close(); } void vkForKey (int keysize, PrintWriter out) throws IllegalAccessException, InvocationTargetException { notify("Generating and testing Variable Key KAT (short); key size: " + keysize); Object[] args = {}; //actual arguments int count = keysize / 8; // number of bytes in key material byte[] keyMaterial = new byte[count]; byte[] pt = new byte[16]; // plaintext (16=default AES block size) byte[] cpt; // computed plaintext byte[] ct; // ciphertext int round = 0; // current round ord. number Object skeys; // algorithm secret key out.println("=========="); out.println(); out.println("KEYSIZE=" + keysize); out.println(); out.println("PT=" + toString(pt)); out.println(); // The key bytes are organised and numbered as follows: // // |<- byte 0 ->|<- byte 1 ->|<- ... ->|<- byte n ->| // |<------------- bit_(n-1) to bit_0 ------------->| // for (int i = 0; i < count; i++) { for (int j = 0; j < 8; j++) { round++; out.println("I=" + round); keyMaterial[count-i-1] = (byte)(1 << (7-j)); out.println("KEY=" + toString(keyMaterial)); args = new Object[] { keyMaterial }; skeys = makeKey.invoke(null, args); keyCount++; args = new Object[] {pt, new Integer(0), skeys}; ct = (byte[]) encrypt.invoke(null, args); encBlocks++; out.print("CT=" + toString(ct)); args[0] = ct; cpt = (byte[]) decrypt.invoke(null, args); decBlocks++; if (! areEqual(pt, cpt)) // check if results match out.print(" *** ERROR ***"); out.println(); out.println(); } keyMaterial[count-i-1] = 0x00; } }// Variable Text KAT methods//........................................................................... void vtKAT (String fileName) { File f = new File(destination, fileName); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(f) , true); } catch (IOException ex1) { halt("Unable to initialize <" + fileName + "> as a Writer:\n" + ex1.getMessage()); } out.println(); out.println("========================="); out.println(); out.println("FILENAME: \"" + fileName + "\""); out.println(); out.println("Electronic Codebook (ECB) Mode"); out.println("Variable Text Known Answer Tests"); out.println(); out.println("Algorithm Name: " + cipherName); out.println("Principal Submitter: " + SUBMITTER); out.println(); try { for (int k = 0; k < keys.length; k++) vtForKey(keys[k], out); } catch (Exception x) { halt("Exception encountered in a " + cipherName + "_Algorithm method:\n" + x.getMessage()); } out.println("=========="); out.close(); } void vtForKey (int keysize, PrintWriter out) throws IllegalAccessException, InvocationTargetException { notify("Generating and testing Variable Text KAT (short); key size: " + keysize); Object[] args = {}; //actual arguments byte[] keyMaterial = new byte[keysize / 8]; byte[] pt = new byte[16]; // plaintext (16=default AES block size) byte[] ct; // ciphertext byte[] cpt; // computed plaintext int round = 0; // current round ord. number args = new Object[] { keyMaterial }; Object skeys = makeKey.invoke(null, args); // the cipher's session keys keyCount++; out.println("=========="); out.println(); out.println("KEYSIZE=" + keysize); out.println(); out.println("KEY=" + toString(keyMaterial)); out.println(); args = new Object[3]; args[1] = new Integer(0); args[2] = skeys; // The plaintext bytes are organised and numbered as follows: // // |<- byte 0 ->|<- byte 1 ->|<- ... ->|<- byte 15 ->| // |<--------------- bit_127 to bit_0 -------------->| // for (int i = 0; i < 16; i++) { for (int j = 0; j < 8; j++) { round++; out.println("I=" + round); pt[15-i] = (byte)(1 << (7 - j)); out.println("PT=" + toString(pt)); args[0] = pt; ct = (byte[]) encrypt.invoke(null, args); encBlocks++; out.print("CT=" + toString(ct)); args[0] = ct; cpt = (byte[]) decrypt.invoke(null, args); decBlocks++; if (! areEqual(pt, cpt)) // check if results match out.print(" *** ERROR ***"); out.println(); out.println(); } pt[15-i] = 0x00; } }// utility static methods// (copied from cryptix.util.core ArrayUtil and Hex classes)//........................................................................... /** * Compares two byte arrays for equality. * * @return true if the arrays have identical contents */ private static boolean areEqual (byte[] a, byte[] b) { int aLength = a.length; if (aLength != b.length) return false; for (int i = 0; i < aLength; i++) if (a[i] != b[i]) return false; return true; } private static final char[] HEX_DIGITS = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; /** * Returns a string of hexadecimal digits from a byte array. Each * byte is converted to 2 hex symbols. */ private static String toString (byte[] ba) { int length = ba.length; char[] buf = new char[length * 2]; for (int i = length, j = 0, k; i > 0; ) { k = ba[--i]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[ k & 0x0F]; } return new String(buf); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -