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

📄 mct.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        "       \"ecb_d_m.txt\", \"cbc_e_m.txt\" and \"cbc_d_m.txt\" will be generated.\n" +        "       If this destination directory is not specified, those files will\n" +        "       be placed in the current user directory.\n\n" +        "  -p <provider>\n" +        "       Name of the Security Provider for the designated algorithm.\n" +        "       If omitted, then assumes provider has the same name as the\n" +        "       algorithm itself.\n\n" +        "  <cipher>\n" +        "       Cipher algorithm name.\n\n" +        "COPYRIGHT\n" +        "  Copyright (c) 1998 Systemics Ltd. on behalf of\n" +        "  the Cryptix Development Team.  All rights reserved.\n");        System.exit(0);    }    /** main action. */    void run() {        long time = System.currentTimeMillis();        try {            if (ecb) ecbMCT(eeFileName, edFileName);            if (cbc) cbcMCT(ceFileName, cdFileName);        }        catch (KeyException ex1) {            ex1.printStackTrace();            halt("Key Exception encountered\n" + ex1.getMessage());        }        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");    }// ECB Monte Carlo Tests//...........................................................................    void ecbMCT (String encName, String decName)    throws KeyException {        PrintWriter enc = null;        File f1 = new File(destination, encName);        try { enc = new PrintWriter(new FileWriter(f1) , true); }        catch (IOException ex3) {            halt("Unable to initialize <" + encName + "> as a Writer:\n" +                ex3.getMessage());        }        PrintWriter dec = null;        File f2 = new File(destination, decName);        try { dec = new PrintWriter(new FileWriter(f2) , true); }        catch (IOException ex4) {            halt("Unable to initialize <" + decName + "> as a Writer:\n" +                ex4.getMessage());        }        enc.println();                                  // do the common load        enc.println("=========================");        enc.println();        enc.println("FILENAME:  \"" + encName+ "\"");        enc.println();        enc.println("Electronic Codebook (ECB) Mode - ENCRYPTION");        enc.println("Monte Carlo Test");        enc.println();        enc.println("Algorithm Name: " + cipherName);        enc.println("Principal Submitter: " + SUBMITTER);        enc.println();                dec.println();        dec.println("=========================");        dec.println();        dec.println("FILENAME:  \"" + decName+ "\"");        dec.println();        dec.println("Electronic Codebook (ECB) Mode - DECRYPTION");        dec.println("Monte Carlo Test");        dec.println();        dec.println("Algorithm Name: " + cipherName);        dec.println("Principal Submitter: " + SUBMITTER);        dec.println();        int k;//        boolean useIJCE = false;        boolean useIJCE = true;        if (useReflection) {            try {//                for (k = 128; k < 257; k += 64) ecbForKeyReflect(k, enc, dec);                for (k = 0; k < keys.length; k++)                    ecbForKeyReflect(keys[k], enc, dec);                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 ex2) {                // no point trying IJCE API. problem is code/data specific                halt("Exception encountered in a " + cipherName +                    "_Algorithm method:\n" + ex2.getMessage());                useIJCE = false;            }        }        if (useIJCE)                                     // use the I/JCE API//            for (k = 128; k < 257; k += 64) ecbForKeyIjce(k, enc, dec);            for (k = 0; k < keys.length; k++) ecbForKeyIjce(keys[k], enc, dec);        enc.println("==========");        dec.println("==========");                enc.close();        dec.close();    }    void ecbForKeyReflect (int keysize, PrintWriter enc, PrintWriter dec)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in ECB mode (long); key size: " + keysize);        notify("Using Reflection API methods");        enc.println("==========");        enc.println();        enc.println("KEYSIZE=" + keysize);        enc.println();                dec.println("==========");        dec.println();        dec.println("KEYSIZE=" + keysize);        dec.println();        Object[] args = {};            //actual arguments        int keylen = keysize / 8;      // number of bytes in user key        byte[] keyMaterial = new byte[keylen];        // cipher block size in bytes        int size = ((Integer) blockSize.invoke(null, args)).intValue();        byte[] pt = new byte[size];    // plaintext        byte[] cpt;                    // computed plaintext        byte[] ct;                     // ciphertext @round j        byte[] ct_1;                   // ciphertext @round j-1        int j, k, count;               // temp vars        String ks, cts; // hexadecimal strings used more than once        Object skeys;                  // algorithm secret key        // step 1 (both). will use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(pt);        for (int i = 0; i < 400; i++) {                      // step 2 (both)            ks = Hex.toString(keyMaterial);            args = new Object[] { keyMaterial };            skeys = makeKey.invoke(null, args);            keyCount++;            //...............................................................            // Encryption            //...............................................................            enc.println("I="   + i);                              // step 2.a            enc.println("KEY=" + ks);            enc.println("PT="  + Hex.toString(pt));            args = new Object[] {pt, new Integer(0), skeys};            ct_1 = (byte[]) encrypt.invoke(null, args);           // step 2.b            for (j = 1; j < 9999; j++) {                args[0] = ct_1;                ct_1 = (byte[]) encrypt.invoke(null, args);                encBlocks++;            }            args[0] = ct_1;            ct = (byte[]) encrypt.invoke(null, args);            encBlocks++;            cts = Hex.toString(ct);            enc.println("CT=" + cts);                             // step 2.c            //...............................................................            // Decryption            //...............................................................            dec.println("I="   + i);                              // step 2.a            dec.println("KEY=" + ks);            dec.println("CT="  + cts);            args[0] = ct;            cpt = (byte[]) decrypt.invoke(null, args);           // step 2.b            decBlocks++;            for (j = 1; j < 10000; j++) {                args[0] = cpt;                cpt = (byte[]) decrypt.invoke(null, args);                decBlocks++;            }            dec.println("PT=" + Hex.toString(cpt));               // step 2.c            if (! ArrayUtil.areEqual(pt, cpt)) {    // check if results match                enc.println(" *** ERROR ***");                dec.println(" *** ERROR ***");                halt("ECB Encryption/Decryption mismatch");            }            enc.println();            dec.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            j = 0;                                         // step 2.d (both)            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= ct_1[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= ct[k++];            System.arraycopy(ct, 0, pt, 0, size);          // step 2.e (both)        }    }    void ecbForKeyIjce (int keysize, PrintWriter enc, PrintWriter dec)    throws KeyException {        notify("Processing MCT in ECB mode (long); key size: " + keysize);        notify("Using IJCE API methods");        enc.println("==========");        enc.println();        enc.println("KEYSIZE=" + keysize);        enc.println();                dec.println("==========");        dec.println();        dec.println("KEYSIZE=" + keysize);        dec.println();        int keylen = keysize / 8;      // number of bytes in user key        byte[] keyMaterial = new byte[keylen];        SecretKey key;                 // algorithm secret key object        int size = cipher.blockSize(); // cipher block size in bytes        byte[] pt = new byte[size];    // plaintext        byte[] cpt;                    // computed plaintext        byte[] ct;                     // ciphertext @round 9999        byte[] ct_1;                   // ciphertext @round 9998        int j, k, count;               // temp vars        String ks, cts; // hexadecimal strings used more than once        // step 1 (both). will use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(pt);        for (int i = 0; i < 400; i++) {                      // step 2 (both)            ks = Hex.toString(keyMaterial);            key = new MCT_Key(keyMaterial);            //...............................................................            // Encryption            //...............................................................            enc.println("I="   + i);                              // step 2.a            enc.println("KEY=" + ks);            enc.println("PT="  + Hex.toString(pt));            cipher.initEncrypt(key);            keyCount++;            ct_1 = cipher.crypt(pt);                              // step 2.b            encBlocks++;            for (j = 1; j < 9999; j++) {                ct_1 = cipher.crypt(ct_1);                encBlocks++;            }            ct = cipher.crypt(ct_1);            encBlocks++;            cts = Hex.toString(ct);            enc.println("CT=" + cts);                             // step 2.c            //...............................................................            // Decryption            //...............................................................            dec.println("I="   + i);                              // step 2.a            dec.println("KEY=" + ks);            dec.println("CT="  + cts);            cipher.initDecrypt(key);            keyCount++;            cpt = cipher.crypt(ct);                               // step 2.b            decBlocks++;            for (j = 1; j < 10000; j++) {                cpt = cipher.crypt(cpt);                decBlocks++;            }            dec.println("PT=" + Hex.toString(cpt));               // step 2.c            if (! ArrayUtil.areEqual(pt, cpt)) {    // check if results match                enc.println(" *** ERROR ***");                dec.println(" *** ERROR ***");                halt("ECB Encryption/Decryption mismatch");            }            enc.println();            dec.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            j = 0;                                         // step 2.d (both)            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= ct_1[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= ct[k++];            System.arraycopy(ct, 0, pt, 0, size);          // step 2.e (both)        }    }// CBC Monte Carlo Tests//...........................................................................    void cbcMCT (String encName, String decName) throws KeyException {        cbcEncrypt(encName);        cbcDecrypt(decName);    }// CBC-Encryption methods//...........................................................................    void cbcEncrypt (String encName) throws KeyException {        PrintWriter pw = null;    // instantiate a PrintWriter for Encryption        File f = new File(destination, encName);        try { pw = new PrintWriter(new FileWriter(f) , true); }        catch (IOException ex1) {            halt("Unable to initialize <" + encName + "> as a Writer:\n" +                ex1.getMessage());        }        pw.println();        pw.println("=========================");        pw.println();        pw.println("FILENAME:  \"" + encName+ "\"");        pw.println();        pw.println("Cipher Block Chaining (CBC) Mode - ENCRYPTION");        pw.println("Monte Carlo Test");        pw.println();        pw.println("Algorithm Name: " + cipherName);        pw.println("Principal Submitter: " + SUBMITTER);        pw.println();        int k;//        boolean useIJCE = false;        boolean useIJCE = true;        if (useReflection) {            try {//                for (k = 128; k < 257; k += 64) cbcEncForKeyReflect(k, pw);                for (k = 0; k < keys.length; k++)                    cbcEncForKeyReflect(keys[k], pw);                useIJCE = false;            } catch (IllegalAccessException ex1) {                // soemthing wrong happened while invoking a known method in

⌨️ 快捷键说明

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