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

📄 mct.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                // *_Algorithm. revert to IJCE                notify("Exception while invoking a method in " + cipherName +                    "_Algorithm class");//                useIJCE = true;            } catch (InvocationTargetException ex2) {                // no point trying other API. problem lies with code/data                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) cbcEncForKeyIjce(k, pw);            for (k = 0; k < keys.length; k++) cbcEncForKeyIjce(keys[k], pw);        pw.println("==========");        pw.close();    }    void cbcEncForKeyReflect (int keysize, PrintWriter pw)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in CBC-Encrypt mode (long); key size: " + keysize);        notify("Using Reflection API methods");        pw.println("==========");        pw.println();        pw.println("KEYSIZE=" + keysize);        pw.println();        Object[] args = {};            //actual arguments        int keylen = keysize / 8;      // number of bytes in user key material        byte[] keyMaterial = new byte[keylen];        int size = ((Integer) blockSize.invoke(null, args)).intValue();        byte[] pt = new byte[size];    // plaintext        byte[] ct = new byte[size];    // ciphertext        byte[] iv = new byte[size];    // initialization vector        int j, k, count;               // temp vars        Object skeys;                  // algorithm secret key        // step 1. will use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(iv);//        rand.nextBytes(pt);        // we do this cause we don't distinguish between j = 0 or other        // in fact we don't need it at all if we start with zero values        System.arraycopy(iv, 0, ct, 0, size);        for (int i = 0; i < 400; i++) {                             // step 2                        // step 2.a is implicit since we're handling cv as iv            pw.println("I="   + i);                               // step 2.b            pw.println("KEY=" + Hex.toString(keyMaterial));            pw.println("IV="  + Hex.toString(iv));            pw.println("PT="  + Hex.toString(pt));            args = new Object[] { keyMaterial };            skeys = makeKey.invoke(null, args);  // the cipher's session keys            keyCount++;            args = new Object[3];            args[1] = new Integer(0);            args[2] = skeys;            for (j = 0; j < 10000; j++) {                         // step 2.c                for (k = 0; k < size; k++) iv[k] ^= pt[k];      // step 2.c.i                System.arraycopy(ct, 0, pt, 0, size); // copy ct@(j-1) into pt                args[0] = iv;                ct = (byte[]) encrypt.invoke(null, args);      // step 2.c.ii                encBlocks++;                System.arraycopy(ct, 0, iv, 0, size);        // set new iv/cv            }            pw.println("CT=" + Hex.toString(ct));                 // step 2.d            pw.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            //            // remember: we keep ct@(j-1) values in pt...            j = 0;                                                // step 2.e            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= pt[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= ct[k++];        }    }    void cbcEncForKeyIjce (int keysize, PrintWriter pw) throws KeyException {        notify("Processing MCT in CBC-Encrypt mode (long); key size: " + keysize);        notify("Using IJCE API methods");        pw.println("==========");        pw.println();        pw.println("KEYSIZE=" + keysize);        pw.println();        int keylen = keysize / 8;      // number of bytes in user key material        byte[] keyMaterial = new byte[keylen];        int size = cipher.blockSize(); // cipher block size in bytes        byte[] pt = new byte[size];    // plaintext        byte[] ct = new byte[size];    // ciphertext        byte[] iv = new byte[size];    // initialization vector        int j, k, count;               // temp vars        SecretKey key;                 // algorithm secret key object        // step 1. will use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(iv);//        rand.nextBytes(pt);        // we do this cause we don't distinguish between j = 0 or other        // in fact we don't need it at all if we start with zero values        System.arraycopy(iv, 0, ct, 0, size);        for (int i = 0; i < 400; i++) {                             // step 2                        // step 2.a is implicit since we're handling cv as iv            pw.println("I="   + i);                               // step 2.b            pw.println("KEY=" + Hex.toString(keyMaterial));            pw.println("IV="  + Hex.toString(iv));            pw.println("PT="  + Hex.toString(pt));            key = new MCT_Key(keyMaterial);            cipher.initEncrypt(key);            keyCount++;            for (j = 0; j < 10000; j++) {                         // step 2.c                for (k = 0; k < size; k++) iv[k] ^= pt[k];      // step 2.c.i                System.arraycopy(ct, 0, pt, 0, size); // copy ct@(j-1) into pt                ct = cipher.crypt(iv);                         // step 2.c.ii                encBlocks++;                System.arraycopy(ct, 0, iv, 0, size);        // set new iv/cv            }            pw.println("CT=" + Hex.toString(ct));                 // step 2.d            pw.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            //            // remember: we keep ct@(j-1) values in pt...            j = 0;                                                // step 2.e            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= pt[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= ct[k++];        }    }// CBC-Decryption methods//...........................................................................        void cbcDecrypt (String decName) throws KeyException {        PrintWriter pw = null;    // instantiate a PrintWriter for Encryption        File f = new File(destination, decName);        try { pw = new PrintWriter(new FileWriter(f) , true); }        catch (IOException ex1) {            halt("Unable to initialize <" + decName + "> as a Writer:\n" +                ex1.getMessage());        }        pw.println();        pw.println("=========================");        pw.println();        pw.println("FILENAME:  \"" + decName+ "\"");        pw.println();        pw.println("Cipher Block Chaining (CBC) Mode - DECRYPTION");        pw.println("Monte Carlo Test");        pw.println();        pw.println("Algorithm Name: " + cipherName);        pw.println("Principal Submitter: " + SUBMITTER);        pw.println();        int k;        boolean useIJCE = false;        if (useReflection) {            try { for (k = 128; k < 257; k += 64) cbcDecForKeyReflect(k, pw); }            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 other API. problem lies with code/data                halt("Exception encountered in a " + cipherName +                    "_Algorithm method:\n" + ex2.getMessage());            }        }        if (useIJCE)                                     // use the I/JCE API            for (k = 128; k < 257; k += 64) cbcDecForKeyIjce(k, pw);        pw.println("==========");        pw.close();    }    void cbcDecForKeyReflect (int keysize, PrintWriter pw)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in CBC-Decrypt mode (long); key size: " + keysize);        notify("Using Reflection API methods");                pw.println("==========");        pw.println();        pw.println("KEYSIZE=" + keysize);        pw.println();        Object[] args = {};            //actual arguments        int keylen = keysize / 8;      // number of bytes in user key material        byte[] keyMaterial = new byte[keylen];        int size = ((Integer) blockSize.invoke(null, args)).intValue();        byte[] pt = new byte[size];    // plaintext        byte[] ct = new byte[size];    // ciphertext        byte[] iv = new byte[size];    // initialization vector        int j, k, count;               // temp vars        Object skeys;                  // algorithm secret key object        // step 1. will use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(iv);//        rand.nextBytes(ct);        for (int i = 0; i < 400; i++) {                             // step 2                        // step 2.a is implicit since we're handling cv as iv            pw.println("I="   + i);                               // step 2.b            pw.println("KEY=" + Hex.toString(keyMaterial));            pw.println("IV="  + Hex.toString(iv));            pw.println("CT="  + Hex.toString(ct));            args = new Object[] { keyMaterial };            skeys = makeKey.invoke(null, args);  // the cipher's session keys            keyCount++;            args = new Object[3];            args[1] = new Integer(0);            args[2] = skeys;            for (j = 0; j < 10000; j++) {                         // step 2.c                args[0] = ct;                pt = (byte[]) decrypt.invoke(null, args); // steps 2.c.i + ii                decBlocks++;                for (k = 0; k < size; k++) pt[k] ^= iv[k];    // step 2.c.iii                System.arraycopy(ct, 0, iv, 0, size);          // step 2.c.iv                System.arraycopy(pt, 0, ct, 0, size);            }            pw.println("PT=" + Hex.toString(pt));                 // step 2.d            pw.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            //            // remember: iv contains values of pt@(j-1)            j = 0;                                                // step 2.e            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= iv[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= pt[k++];        }    }    void cbcDecForKeyIjce (int keysize, PrintWriter pw) throws KeyException {        notify("Processing MCT in CBC-Decrypt mode (long); key size: " + keysize);        notify("Using IJCE API methods");                pw.println("==========");        pw.println();        pw.println("KEYSIZE=" + keysize);        pw.println();        int keylen = keysize / 8;      // number of bytes in user key material        byte[] keyMaterial = new byte[keylen];        int size = cipher.blockSize(); // cipher block size in bytes        byte[] pt = new byte[size];    // plaintext        byte[] ct = new byte[size];    // ciphertext        byte[] iv = new byte[size];    // initialization vector        int j, k, count;               // temp vars        SecretKey key;                 // algorithm secret key object        // step 1. use all-zeroes values//        rand.nextBytes(keyMaterial);//        rand.nextBytes(iv);//        rand.nextBytes(ct);        for (int i = 0; i < 400; i++) {                             // step 2                        // step 2.a is implicit since we're handling cv as iv            pw.println("I="   + i);                               // step 2.b            pw.println("KEY=" + Hex.toString(keyMaterial));            pw.println("IV="  + Hex.toString(iv));            pw.println("CT="  + Hex.toString(ct));            key = new MCT_Key(keyMaterial);            cipher.initDecrypt(key);            keyCount++;            for (j = 0; j < 10000; j++) {                         // step 2.c                pt = cipher.crypt(ct);                // steps 2.c.i + 2.c.ii                decBlocks++;                for (k = 0; k < size; k++) pt[k] ^= iv[k];    // step 2.c.iii                System.arraycopy(ct, 0, iv, 0, size);          // step 2.c.iv                System.arraycopy(pt, 0, ct, 0, size);            }            pw.println("PT=" + Hex.toString(pt));                 // step 2.d            pw.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*size            //            // remember: iv contains values of pt@(j-1)            j = 0;                                                // step 2.e            if (keylen > size) {                count = keylen - size;                 k = size - count;                while (j < count) keyMaterial[j++] ^= iv[k++];            }            k = 0;            while (j < keylen) keyMaterial[j++] ^= pt[k++];        }    }// ==========================================================================// MCT_Key inner class// ==========================================================================    final class MCT_Key implements SecretKey    {        byte[] key; // copy of user supplied key material        public MCT_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 + -