⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kat.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        notify("  Created " + keyCount + " session keys");    }// Variable Key KAT methods//...........................................................................    void vkKAT (String fileName) throws KeyException {        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 Key Known Answer Tests");        out.println();        out.println("Algorithm Name: " + cipherName);        out.println("Principal Submitter: " + SUBMITTER);        out.println();        int k;//        boolean useIJCE = false;        boolean useIJCE = true;        if (useReflection) {            try {//                for (k = 128; k < 257; k += 64) vkForKeyReflect(k, out);                for (k = 0; k < keys.length; k++) vkForKeyReflect(keys[k], out);                useIJCE = false;            } catch (IllegalAccessException ex1) {                // soemthing wrong happened while invoking a known method in                // *_Algorithm. revert to IJCE                notify("Exception while invoking a method in " + cipherName +                    "_Algorithm class");//                useIJCE = true;            } catch (InvocationTargetException ex3) {                // no point trying IJCE API. problem lies with code/data                halt("Exception encountered in a " + cipherName +                    "_Algorithm method:\n" + ex3.getMessage());                useIJCE = false;            }        }        if (useIJCE)                                     // use the I/JCE API//            for (k = 128; k < 257; k += 64) vkForKeyIjce(k, out);            for (k = 0; k < keys.length; k++) vkForKeyIjce(keys[k], out);        out.println("==========");        out.close();    }    void vkForKeyReflect (int keysize, PrintWriter out)    throws IllegalAccessException, InvocationTargetException {        notify("Generating and testing Variable Key KAT (short); key size: " +            keysize);        notify("Using Reflection API methods");        Object[] args = {};         //actual arguments        int count = keysize / 8;    // number of bytes in key material        int size = ((Integer) blockSize.invoke(null, args)).intValue();        byte[] keyMaterial = new byte[count];        byte[] pt = new byte[size]; // plaintext        byte[] cpt;                 // computed plaintext        byte[] ct;                  // ciphertext        int round = 0;              // current round ord. number        int i, j;                   // temp vars        Object skeys;               // algorithm secret key                out.println("==========");        out.println();        out.println("KEYSIZE=" + keysize);        out.println();        out.println("PT=" + Hex.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 (i = 0; i < count; i++) {            for (j = 0; j < 8; j++) {                round++;                out.println("I=" + round);                keyMaterial[i] = (byte)(1 << (7 - j));                out.println("KEY=" + Hex.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=" + Hex.toString(ct));                args[0] = ct;                cpt = (byte[]) decrypt.invoke(null, args);                decBlocks++;                if (! ArrayUtil.areEqual(pt, cpt))  // check if results match                    out.print(" *** ERROR ***");                out.println();                out.println();            }            keyMaterial[i] = 0x00;        }    }    void vkForKeyIjce (int keysize, PrintWriter out)    throws KeyException {        notify("Generating and testing Variable Key KAT (short); key size: " +            keysize);        notify("Using IJCE API methods");        int count = keysize / 8; // number of bytes in key material        int size = cipher.blockSize();        byte[] keyMaterial = new byte[count];        byte[] pt = new byte[size]; // plaintext        byte[] ct;                  // ciphertext        byte[] cpt;                 // computed plaintext        int round = 0;              // current round ord. number        int i, j;                   // temp vars        SecretKey key;              // algorithm secret key        out.println("==========");        out.println();        out.println("KEYSIZE=" + keysize);        out.println();        out.println("PT=" + Hex.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 (i = 0; i < count; i++) {            for (j = 0; j < 8; j++) {                round++;                out.println("I=" + round);                keyMaterial[i] = (byte)(1 << (7 - j));                out.println("KEY=" + Hex.toString(keyMaterial));                key = new KAT_Key(keyMaterial);                cipher.initEncrypt(key);                keyCount++;                ct = cipher.crypt(pt);                encBlocks++;                out.print("CT=" + Hex.toString(ct));                cipher.initDecrypt(key);                keyCount++;                cpt = cipher.crypt(ct);                decBlocks++;                if (! ArrayUtil.areEqual(pt, cpt))  // check if results match                    out.print(" *** ERROR ***");                out.println();                out.println();            }            keyMaterial[i] = 0x00;        }    }// Variable Text KAT methods//...........................................................................    void vtKAT (String fileName) throws KeyException {        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();        // use reflection API to load the *_Algorithm class if one exists        // look for class fully named <provider>.<cipherName>_Algorithm        int k;//        boolean useIJCE = false;        boolean useIJCE = true;        if (useReflection) {            try {//                for (k = 128; k < 257; k += 64) vtForKeyReflect(k, out);                for (k = 0; k < keys.length; k++) vtForKeyReflect(keys[k], out);                useIJCE = false;            } catch (IllegalAccessException ex1) {                // soemthing wrong happened while invoking a known method in                // *_Algorithm. revert to IJCE                notify("Exception while invoking a method in " + cipherName +                    "_Algorithm class");//                useIJCE = true;            } catch (InvocationTargetException ex3) {                // no point trying other API. problem lies with code/data                halt("Exception encountered in a " + cipherName +                    "_Algorithm method:\n" + ex3.getMessage());                useIJCE = false;            }        }        if (useIJCE)                                     // use the I/JCE API//            for (k = 128; k < 257; k += 64) vtForKeyIjce(k, out);            for (k = 0; k < keys.length; k++) vtForKeyIjce(keys[k], out);        out.println("==========");        out.close();    }    void vtForKeyReflect (int keysize, PrintWriter out)    throws IllegalAccessException, InvocationTargetException {        notify("Generating and testing Variable Text KAT (short); key size: " +            keysize);        notify("Using Reflection API methods");        Object[] args = {};             //actual arguments        byte[] keyMaterial = new byte[keysize / 8];        int count = ((Integer) blockSize.invoke(null, args)).intValue();        byte[] pt = new byte[count];    // plaintext        byte[] ct;                      // ciphertext        byte[] cpt;                     // computed plaintext        int round = 0;                  // current round ord. number        int i, j;                       // temp vars        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=" + Hex.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 n ->|        // |<------------- bit_(n-1) to bit_0 ------------->|        //        for (i = 0; i < count; i++) {            for (j = 0; j < 8; j++) {                round++;                out.println("I=" + round);                pt[i] = (byte)(1 << (7 - j));                out.println("PT=" + Hex.toString(pt));                args[0] = pt;                ct = (byte[]) encrypt.invoke(null, args);                encBlocks++;                out.print("CT=" + Hex.toString(ct));                args[0] = ct;                cpt = (byte[]) decrypt.invoke(null, args);                decBlocks++;                if (! ArrayUtil.areEqual(pt, cpt))  // check if results match                    out.print(" *** ERROR ***");                out.println();                out.println();            }            pt[i] = 0x00;        }    }    void vtForKeyIjce (int keysize, PrintWriter out) throws KeyException {        notify("Generating and testing Variable Text KAT (short); key size: " +            keysize);        notify("Using IJCE API methods");        byte[] keyMaterial = new byte[keysize / 8];        int count = cipher.blockSize(); // the cipher's block size        byte[] pt = new byte[count];    // plaintext        byte[] ct;                      // ciphertext        byte[] cpt;                     // computed plaintext        int round = 0;                  // current round ord. number        int i, j;                       // temp vars        SecretKey key = new KAT_Key(keyMaterial);        out.println("==========");        out.println();        out.println("KEYSIZE=" + keysize);        out.println();        out.println("KEY=" + Hex.toString(keyMaterial));        out.println();        // The plaintext bytes are organised and numbered as follows:        //        // |<- byte 0 ->|<- byte 1 ->|<- ... ->|<- byte n ->|        // |<------------- bit_(n-1) to bit_0 ------------->|        //        for (i = 0; i < count; i++) {            for (j = 0; j < 8; j++) {                round++;                out.println("I=" + round);                pt[i] = (byte)(1 << (7 - j));                out.println("PT=" + Hex.toString(pt));                cipher.initEncrypt(key);                keyCount++;                ct = cipher.crypt(pt);                encBlocks++;                out.print("CT=" + Hex.toString(ct));                cipher.initDecrypt(key);                keyCount++;                cpt = cipher.crypt(ct);                decBlocks++;                if (! ArrayUtil.areEqual(pt, cpt))  // check if results match                    out.print(" *** ERROR ***");                out.println();                out.println();            }            pt[i] = 0x00;        }    }// ==========================================================================// KAT_Key inner class// ==========================================================================    final class KAT_Key implements SecretKey    {        byte[] key; // copy of user supplied key material        public KAT_Key (byte[] data) { key = (byte[]) data.clone(); }    //    // java.security.Key methods    //        public String getAlgorithm() { return "<ANY>"; }        public String getFormat() { return "RAW"; }        public byte[] getEncoded() { return (byte[]) key.clone(); }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -