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

📄 mct.java

📁 加密解密算法大全。很多很多的加密解密的实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        dec.println("Monte Carlo Test");        dec.println();        dec.println("Algorithm Name: " + cipherName);        dec.println("Principal Submitter: " + SUBMITTER);        dec.println();        try {            for (int k = 0; k < keys.length; k++)                ecbDecryptForKey(keys[k], dec);        } catch (Exception x) {            halt("Exception encountered in a " + cipherName +                "_Algorithm method:\n" + x.getMessage());        }        dec.println("==========");        dec.close();    }    void ecbEncryptForKey (int keysize, PrintWriter enc)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in ECB-Encrypt mode (long); key size: " + keysize);        enc.println("==========");        enc.println();        enc.println("KEYSIZE=" + keysize);        enc.println();        Object[] args = {};            // actual arguments        int keylen = keysize / 8;      // number of bytes in user key        byte[] keyMaterial = new byte[keylen];        int SIZE = 16;                 // default AES block size in bytes        byte[] pt = new byte[SIZE];    // plaintext @i == ciphertext@i-1        byte[] ct_1;                   // ciphertext @j-1        int j, k, count;               // temp vars        Object skeys;                  // algorithm secret key        // step 1. we use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(pt);        for (int i = 0; i < 400; i++) {                      // step 2 (both)            args = new Object[] { keyMaterial };            skeys = makeKey.invoke(null, args);            keyCount++;            enc.println("I="   + i);                              // step 2.a            enc.println("KEY=" + toString(keyMaterial));            enc.println("PT="  + toString(pt));            args = new Object[] {pt, new Integer(0), skeys};            ct_1 = (byte[]) encrypt.invoke(null, args);           // step 2.b            encBlocks++;            for (j = 1; j < 9999; j++) {                args[0] = ct_1;                ct_1 = (byte[]) encrypt.invoke(null, args);                encBlocks++;            }            args[0] = ct_1;            pt = (byte[]) encrypt.invoke(null, args);             // step 2.e            encBlocks++;            enc.println("CT=" + toString(pt));                    // step 2.c            enc.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*SIZE            j = 0;                                                // step 2.d            if (keylen > SIZE) {                count = keylen - SIZE;                 k = SIZE - count;                while (j < count)                    keyMaterial[j++] ^= ct_1[k++];            }            k = 0;            while (j < keylen)                keyMaterial[j++] ^= pt[k++];        }    }    void ecbDecryptForKey (int keysize, PrintWriter dec)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in ECB-Decrypt mode (long); key size: " + keysize);                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];        int SIZE = 16;                 // default AES block size in bytes        byte[] ct = new byte[SIZE];    // ciphertext @i == plaintext @i+1        byte[] pt_1;                   // plaintext @j-1        int j, k, count;               // temp vars        Object skeys;                  // algorithm secret key        // step 1. we use all zeroes.//        rand.nextBytes(keyMaterial);//        rand.nextBytes(ct);        for (int i = 0; i < 400; i++) {                      // step 2 (both)            args = new Object[] { keyMaterial };            skeys = makeKey.invoke(null, args);            keyCount++;            dec.println("I="   + i);                              // step 2.a            dec.println("KEY=" + toString(keyMaterial));            dec.println("CT="  + toString(ct));            args = new Object[] {ct, new Integer(0), skeys};            pt_1 = (byte[]) decrypt.invoke(null, args);           // step 2.b            decBlocks++;            for (j = 1; j < 9999; j++) {                args[0] = pt_1;                pt_1 = (byte[]) decrypt.invoke(null, args);                decBlocks++;            }            args[0] = pt_1;            ct = (byte[]) decrypt.invoke(null, args);             // step 2.e            decBlocks++;            dec.println("PT=" + toString(ct));                    // step 2.c            dec.println();            // may throw ArrayIndexOutOfBoundsException with            // non-AES ciphers; ie. those for which:            // keylen < size || keylen > 2*SIZE            j = 0;                                                // step 2.d            if (keylen > SIZE) {                count = keylen - SIZE;                 k = SIZE - count;                while (j < count)                    keyMaterial[j++] ^= pt_1[k++];            }            k = 0;            while (j < keylen)                keyMaterial[j++] ^= ct[k++];        }    }// CBC MCT methods//...........................................................................    void cbcEncrypt (String encName) {        PrintWriter pw = null;    // instantiate a PrintWriter for Encryption        File f = new File(destination, encName);        try {            pw = new PrintWriter(new FileWriter(f) , true);        } catch (IOException x) {            halt("Unable to initialize <" + encName + "> as a Writer:\n" +                x.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();        try {            for (int k = 0; k < keys.length; k++)                cbcEncryptForKey(keys[k], pw);        } catch (Exception x) {            halt("Exception encountered in a " + cipherName +                "_Algorithm method:\n" + x.getMessage());        }        pw.println("==========");        pw.close();    }        void cbcDecrypt (String decName) {        PrintWriter pw = null;    // instantiate a PrintWriter for Encryption        File f = new File(destination, decName);        try {            pw = new PrintWriter(new FileWriter(f) , true);        } catch (IOException x) {            halt("Unable to initialize <" + decName + "> as a Writer:\n" +                x.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();        try {            for (int k = 128; k < 257; k += 64)                cbcDecryptForKey(k, pw);        } catch (Exception x) {            halt("Exception encountered in a " + cipherName +                "_Algorithm method:\n" + x.getMessage());        }        pw.println("==========");        pw.close();    }    void cbcEncryptForKey (int keysize, PrintWriter pw)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in CBC-Encrypt mode (long); key size: "+keysize);        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 = 16;                 // default AES 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        Object skeys;                  // algorithm secret key        // step 1. we 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=" + toString(keyMaterial));            pw.println("IV="  + toString(iv));            pw.println("PT="  + 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++)                      // step 2.c.i                    iv[k] ^= pt[k];                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=" + 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 cbcDecryptForKey (int keysize, PrintWriter pw)    throws IllegalAccessException, InvocationTargetException {        notify("Processing MCT in CBC-Decrypt mode (long); key size: "+keysize);                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 = 16;                 // default AES 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        Object skeys;                  // algorithm secret key object        // step 1. we 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=" + toString(keyMaterial));            pw.println("IV="  + toString(iv));            pw.println("CT="  + 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++)                    // step 2.c.iii                    pt[k] ^= iv[k];                System.arraycopy(ct, 0, iv, 0, SIZE);          // step 2.c.iv                System.arraycopy(pt, 0, ct, 0, SIZE);            }            pw.println("PT=" + 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++];        }    }// utility static methods (from cryptix.util.core ArrayUtil and Hex classes)//...........................................................................    /**     * 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 + -