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

📄 pbetest.java

📁 kmlnjlkj nlkjlkjkljl okopokipoipo oipipipo i
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.bouncycastle.jce.provider.test;import java.security.AlgorithmParameters;import java.security.Security;import java.security.spec.InvalidParameterSpecException;import javax.crypto.Cipher;import javax.crypto.Mac;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.PBEParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.bouncycastle.crypto.Digest;import org.bouncycastle.crypto.PBEParametersGenerator;import org.bouncycastle.crypto.digests.SHA1Digest;import org.bouncycastle.crypto.digests.SHA256Digest;import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;import org.bouncycastle.crypto.params.KeyParameter;import org.bouncycastle.crypto.params.ParametersWithIV;import org.bouncycastle.jce.provider.BouncyCastleProvider;import org.bouncycastle.util.encoders.Hex;import org.bouncycastle.util.test.SimpleTest;/** * test out the various PBE modes, making sure the JCE implementations * are compatible woth the light weight ones. */public class PBETest    extends SimpleTest{    private class OpenSSLTest        extends SimpleTest    {        char[]    password;        String    baseAlgorithm;        String    algorithm;        int       keySize;        int       ivSize;                OpenSSLTest(            String    baseAlgorithm,            String    algorithm,            int       keySize,            int       ivSize)        {            this.password = algorithm.toCharArray();            this.baseAlgorithm = baseAlgorithm;            this.algorithm = algorithm;            this.keySize = keySize;            this.ivSize = ivSize;        }                public String getName()        {            return "OpenSSLPBE";        }            public void performTest()            throws Exception        {            byte[] salt = new byte[16];            int    iCount = 100;                        for (int i = 0; i != salt.length; i++)            {                salt[i] = (byte)i;            }            OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();            pGen.init(                    PBEParametersGenerator.PKCS5PasswordToBytes(password),                    salt,                    iCount);            ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);            SecretKeySpec   encKey = new SecretKeySpec(((KeyParameter)params.getParameters()).getKey(), baseAlgorithm);            Cipher          c;            if (baseAlgorithm.equals("RC4"))            {                c = Cipher.getInstance(baseAlgorithm, "BC");                c.init(Cipher.ENCRYPT_MODE, encKey);            }            else            {                c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");                c.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV()));            }            byte[]          enc = c.doFinal(salt);            c = Cipher.getInstance(algorithm, "BC");            PBEKeySpec          keySpec = new PBEKeySpec(password, salt, iCount);            SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");            c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));            byte[]          dec = c.doFinal(enc);            if (!arrayEquals(salt, dec))            {                fail("" + algorithm + "failed encryption/decryption test");            }        }    }        private class PKCS12Test        extends SimpleTest    {        char[]    password;        String    baseAlgorithm;        String    algorithm;        Digest    digest;        int       keySize;        int       ivSize;                PKCS12Test(            String    baseAlgorithm,            String    algorithm,            Digest    digest,            int       keySize,            int       ivSize)        {            this.password = algorithm.toCharArray();            this.baseAlgorithm = baseAlgorithm;            this.algorithm = algorithm;            this.digest = digest;            this.keySize = keySize;            this.ivSize = ivSize;        }                public String getName()        {            return "PKCS12PBE";        }            public void performTest()            throws Exception        {            byte[] salt = new byte[digest.getDigestSize()];            int    iCount = 100;                        digest.doFinal(salt, 0);            PKCS12ParametersGenerator   pGen = new PKCS12ParametersGenerator(digest);            pGen.init(                    PBEParametersGenerator.PKCS12PasswordToBytes(password),                    salt,                    iCount);            ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);            SecretKeySpec   encKey = new SecretKeySpec(((KeyParameter)params.getParameters()).getKey(), baseAlgorithm);            Cipher          c;            if (baseAlgorithm.equals("RC4"))            {                c = Cipher.getInstance(baseAlgorithm, "BC");                c.init(Cipher.ENCRYPT_MODE, encKey);            }            else            {                c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");                c.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV()));            }            byte[]          enc = c.doFinal(salt);            c = Cipher.getInstance(algorithm, "BC");            PBEKeySpec          keySpec = new PBEKeySpec(password, salt, iCount);            SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");            c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));            byte[]          dec = c.doFinal(enc);            if (!arrayEquals(salt, dec))            {                fail("" + algorithm + "failed encryption/decryption test");            }            //            // get the parameters            //            AlgorithmParameters param = checkParameters(c, salt, iCount);            //            // try using parameters            //            c = Cipher.getInstance(algorithm, "BC");            keySpec = new PBEKeySpec(password);            c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec), param);            checkParameters(c, salt, iCount);            dec = c.doFinal(enc);            if (!arrayEquals(salt, dec))            {                fail("" + algorithm + "failed encryption/decryption test");            }            //            // try using PBESpec            //            c = Cipher.getInstance(algorithm, "BC");            keySpec = new PBEKeySpec(password);            c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec), param.getParameterSpec(PBEParameterSpec.class));            checkParameters(c, salt, iCount);            dec = c.doFinal(enc);            if (!arrayEquals(salt, dec))            {                fail("" + algorithm + "failed encryption/decryption test");            }        }        private AlgorithmParameters checkParameters(Cipher c, byte[] salt, int iCount)            throws InvalidParameterSpecException        {            AlgorithmParameters param = c.getParameters();            PBEParameterSpec spec = (PBEParameterSpec)param.getParameterSpec(PBEParameterSpec.class);            if (!arrayEquals(salt, spec.getSalt()))            {                fail("" + algorithm + "failed salt test");            }            if (iCount != spec.getIterationCount())            {                fail("" + algorithm + "failed count test");            }            return param;        }    }        private PKCS12Test[] pkcs12Tests = {        new PKCS12Test("DESede", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC",  new SHA1Digest(),   192,  64),        new PKCS12Test("DESede", "PBEWITHSHAAND2-KEYTRIPLEDES-CBC",  new SHA1Digest(),   128,  64),        new PKCS12Test("RC4",    "PBEWITHSHAAND128BITRC4",           new SHA1Digest(),   128,   0),        new PKCS12Test("RC4",    "PBEWITHSHAAND40BITRC4",            new SHA1Digest(),    40,   0),        new PKCS12Test("RC2",    "PBEWITHSHAAND128BITRC2-CBC",       new SHA1Digest(),   128,  64),        new PKCS12Test("RC2",    "PBEWITHSHAAND40BITRC2-CBC",        new SHA1Digest(),    40,  64),        new PKCS12Test("AES",    "PBEWithSHA1And128BitAES-CBC-BC",   new SHA1Digest(),   128, 128),        new PKCS12Test("AES",    "PBEWithSHA1And192BitAES-CBC-BC",   new SHA1Digest(),   192, 128),        new PKCS12Test("AES",    "PBEWithSHA1And256BitAES-CBC-BC",   new SHA1Digest(),   256, 128),        new PKCS12Test("AES",    "PBEWithSHA256And128BitAES-CBC-BC", new SHA256Digest(), 128, 128),        new PKCS12Test("AES",    "PBEWithSHA256And192BitAES-CBC-BC", new SHA256Digest(), 192, 128),           new PKCS12Test("AES",    "PBEWithSHA256And256BitAES-CBC-BC", new SHA256Digest(), 256, 128),        new PKCS12Test("Twofish","PBEWithSHAAndTwofish-CBC",         new SHA1Digest(),   256, 128),        new PKCS12Test("IDEA",   "PBEWithSHAAndIDEA-CBC",            new SHA1Digest(),   128,  64)    };        private OpenSSLTest openSSLTests[] = {

⌨️ 快捷键说明

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