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

📄 t_cipher.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            // String key = "Paula bla la da trish123 sdkfs;ldkg;sa'jlskjgklad";            String key = "Paulabla123456789012345";            byte[] buf = key.getBytes();            System.out.println("key length is " + buf.length);            SecretKeySpec sKeySpec = new SecretKeySpec(buf,"Blowfish");            // SecretKeySpec sKeySpec = new SecretKeySpec(buf,"DESede");            Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");            // Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");            // Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            cipher.init(Cipher.ENCRYPT_MODE,sKeySpec);            // only works with NoPadding if size is a multiple of 8 bytes            // with PKCS5Padding, works for all sizes            byte[] original = "This is what should get encrypte".getBytes();            System.out.println("original length is " + original.length);            byte[] encrypted = cipher.doFinal(original);            // works            // AlgorithmParameters algParam = cipher.getParameters();            byte[] iv = cipher.getIV();            System.out.println("length of iv is " + iv.length);            Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/NoPadding");            // Cipher cipher2 = Cipher.getInstance("DESede/CBC/NoPadding");            // Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            // works            // cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,algParam);            IvParameterSpec ivClass = new IvParameterSpec(iv);            cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,ivClass);            byte[] decrypted = cipher2.doFinal(encrypted);            if (byteArrayIdentical(original,decrypted,0,original.length))                System.out.println("PASSED");            else                System.out.println("FAILED");            System.out.println("original length is " + original.length);            System.out.println("encrypted length is " + encrypted.length);            System.out.println("decrypted length is " + decrypted.length);        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testBlowfish");    }    private void testCryptix()    {        System.out.println("Running testCryptix");        try        {            // set up the provider            Class jceClass = Class.forName("cryptix.jce.provider.Cryptix");            java.security.Provider cryptixProvider = (java.security.Provider) jceClass.newInstance();            java.security.Security.addProvider(cryptixProvider);		    byte[] userkey = "a secret".getBytes();            System.out.println("userkey length is " + userkey.length);            Key secretKey = (Key) (new SecretKeySpec(userkey, "DES"));		    byte[] IV = "anivspec".getBytes();            Cipher enCipher = Cipher.getInstance("DES/CBC/NoPadding","Cryptix");            Cipher deCipher = Cipher.getInstance("DES/CBC/NoPadding","Cryptix");			IvParameterSpec ivspec = new IvParameterSpec(IV);            enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);            deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);            int patternLength = 8;            byte[] pattern = new byte[patternLength];			for (int i = 0; i < patternLength; i++)				pattern[i] = (byte)(i & 0xFF);            byte[] cipherOutput1 = new byte[patternLength];            byte[] cipherOutput2 = new byte[patternLength];            int retval = 0;            retval = enCipher.doFinal(pattern, 0, 8, cipherOutput1, 0);            retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 1");            else                System.out.println("FAILED TEST 1");            retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 2");            else                System.out.println("FAILED TEST 2");        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testCryptix");    }    private void testMessageDigest()    {        // No provider needs to be installed for this to work.        try        {            MessageDigest md = MessageDigest.getInstance("MD5");            byte[] data = "Paulas digest".getBytes();            byte[] digest = md.digest(data);            byte[] digest2 = md.digest(data);            if (byteArrayIdentical(digest,digest2,0,digest.length))                System.out.println("PASSED");            else                System.out.println("FAILED");            System.out.println("data length is " + data.length);            System.out.println("digest length is " + digest.length);            System.out.println("digest2 length is " + digest2.length);        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testBlowfish");    }    // PT    private void testPCBC()    {        System.out.println("Running testPCBC");        try        {            // set up the provider            Class jceClass = Class.forName("com.sun.crypto.provider.SunJCE");            java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();            java.security.Security.addProvider(myProvider);            // java.security.Provider sunJce = new com.sun.crypto.provider.SunJCE();            // java.security.Security.addProvider(sunJce);            // String key = "Paula bla la da trish123 sdkfs;ldkg;sa'jlskjgklad";            String key = "PaulablaPaulablaPaulabla";            byte[] buf = key.getBytes();            System.out.println("key length is " + buf.length);            SecretKeySpec sKeySpec = new SecretKeySpec(buf,"DESede");            Cipher cipher = Cipher.getInstance("DESede/PCBC/NoPadding");            // Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");            // Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            cipher.init(Cipher.ENCRYPT_MODE,sKeySpec);            // only works with NoPadding if size is a multiple of 8 bytes            // with PKCS5Padding, works for all sizes            byte[] original = "This is what should get encrypte".getBytes();            System.out.println("original length is " + original.length);            byte[] encrypted = cipher.doFinal(original);            // works            // AlgorithmParameters algParam = cipher.getParameters();            byte[] iv = cipher.getIV();            System.out.println("length of iv is " + iv.length);            Cipher cipher2 = Cipher.getInstance("DESede/PCBC/NoPadding");            // Cipher cipher2 = Cipher.getInstance("DESede/CBC/NoPadding");            // Cipher cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");            // works            // cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,algParam);            IvParameterSpec ivClass = new IvParameterSpec(iv);            cipher2.init(Cipher.DECRYPT_MODE,sKeySpec,ivClass);            byte[] decrypted = cipher2.doFinal(encrypted);            if (byteArrayIdentical(original,decrypted,0,original.length))                System.out.println("PASSED");            else                System.out.println("FAILED");            System.out.println("original length is " + original.length);            System.out.println("encrypted length is " + encrypted.length);            System.out.println("decrypted length is " + decrypted.length);        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testPCBC");    }    private void testPCBC2()    {        System.out.println("Running testPCBC2");        try        {            // set up the provider            Class jceClass = Class.forName("com.sun.crypto.provider.SunJCE");            java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();            java.security.Security.addProvider(myProvider);		    byte[] userkey = "a secreta secreta secret".getBytes();            System.out.println("userkey length is " + userkey.length);            Key secretKey = (Key) (new SecretKeySpec(userkey, "DESede"));		    byte[] IV = "anivspec".getBytes();            Cipher enCipher = Cipher.getInstance("DESede/PCBC/NoPadding","SunJCE");            Cipher deCipher = Cipher.getInstance("DESede/PCBC/NoPadding","SunJCE");			IvParameterSpec ivspec = new IvParameterSpec(IV);            enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);            deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);            int patternLength = 24;            byte[] pattern = new byte[patternLength];			for (int i = 0; i < patternLength; i++)				pattern[i] = (byte)(i & 0xFF);            byte[] cipherOutput1 = new byte[patternLength];            byte[] cipherOutput2 = new byte[patternLength];            int retval = 0;            retval = enCipher.doFinal(pattern, 0, 24, cipherOutput1, 0);            retval = deCipher.doFinal(cipherOutput1, 0, 24, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 1");            else                System.out.println("FAILED TEST 1");            retval = deCipher.doFinal(cipherOutput1, 0, 24, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 2");            else                System.out.println("FAILED TEST 2");        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testPCBC2");    }    private void testIAIK()    {        System.out.println("Running testIAIK");        try        {            // set up the provider            Class jceClass = Class.forName("iaik.security.provider.IAIK");            java.security.Provider myProvider = (java.security.Provider) jceClass.newInstance();            java.security.Security.addProvider(myProvider);            // iaik.security.provider.IAIK.addAsProvider(true);            // iaik.utils.Util.loadClass("iaik.security.provider.IAIK",true);            // IAIK p=new IAIK();            // iaik.security.provider.IAIK.getMd5();		    byte[] userkey = "a secret".getBytes();            System.out.println("userkey length is " + userkey.length);            Key secretKey = (Key) (new SecretKeySpec(userkey, "DES"));		    byte[] IV = "anivspec".getBytes();            Cipher enCipher = Cipher.getInstance("DES/CBC/NoPadding","IAIK");            Cipher deCipher = Cipher.getInstance("DES/CBC/NoPadding","IAIK");			IvParameterSpec ivspec = new IvParameterSpec(IV);            enCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivspec);            deCipher.init(Cipher.DECRYPT_MODE,secretKey,ivspec);            int patternLength = 8;            byte[] pattern = new byte[patternLength];			for (int i = 0; i < patternLength; i++)				pattern[i] = (byte)(i & 0xFF);            byte[] cipherOutput1 = new byte[patternLength];            byte[] cipherOutput2 = new byte[patternLength];            int retval = 0;            retval = enCipher.doFinal(pattern, 0, 8, cipherOutput1, 0);            retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 1");            else                System.out.println("FAILED TEST 1");            retval = deCipher.doFinal(cipherOutput1, 0, 8, cipherOutput2, 0);            if (byteArrayIdentical(cipherOutput2,pattern,0,patternLength))                System.out.println("PASSED TEST 2");            else                System.out.println("FAILED TEST 2");        }        catch (Throwable t)        {            System.out.println("got an exception");            t.printStackTrace();        }        System.out.println("Finished testIAIK");    }    private void printByteArray(String name, byte[] array)    {        System.out.println("printing array " + name);        for (int i = 0; i < array.length; i++)            System.out.println("index " + i + " : " + array[i]);    }    */}

⌨️ 快捷键说明

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