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

📄 pbetest.java

📁 bouncycastle 是一个JAVA安全提供者
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        private Cipher makePBECipherUsingParam(        String  algorithm,        int     mode,        char[]  password,        byte[]  salt,        int     iterationCount)        throws Exception    {        PBEKeySpec          pbeSpec = new PBEKeySpec(password);        SecretKeyFactory    keyFact = SecretKeyFactory.getInstance(algorithm, "BC");        PBEParameterSpec    defParams = new PBEParameterSpec(salt, iterationCount);        Cipher cipher = Cipher.getInstance(algorithm, "BC");        cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);        return cipher;    }    private Cipher makePBECipherWithoutParam(        String  algorithm,        int     mode,        char[]  password,        byte[]  salt,        int     iterationCount)        throws Exception    {        PBEKeySpec          pbeSpec = new PBEKeySpec(password, salt, iterationCount);        SecretKeyFactory    keyFact = SecretKeyFactory.getInstance(algorithm, "BC");        Cipher cipher = Cipher.getInstance(algorithm, "BC");        cipher.init(mode, keyFact.generateSecret(pbeSpec));        return cipher;    }        private boolean arrayEquals(        byte[]  a,        byte[]  b)    {        if (a.length != b.length)        {            return false;        }        for (int i = 0; i != a.length; i++)        {            if (a[i] != b[i])            {                return false;            }        }        return true;    }    public void testPBEHMac(        String  hmacName,        byte[]  output)    {        SecretKey           key = null;        byte[]              out;        Mac                 mac = null;        try        {            SecretKeyFactory    fact = SecretKeyFactory.getInstance(hmacName, "BC");            key = fact.generateSecret(new PBEKeySpec("hello".toCharArray()));                        mac = Mac.getInstance(hmacName, "BC");        }        catch (Exception e)        {            fail("Failed - exception " + e.toString(), e);        }        try        {            mac.init(key, new PBEParameterSpec(new byte[20], 100));        }        catch (Exception e)        {            fail("Failed - exception " + e.toString(), e);        }        mac.reset();                mac.update(message, 0, message.length);        out = mac.doFinal();        if (!arrayEquals(out, output))        {            fail("Failed - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out)));        }    }        public void performTest()        throws Exception    {        byte[] input = Hex.decode("1234567890abcdefabcdef1234567890fedbca098765");        //        // DES        //        Cipher  cEnc = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC");        cEnc.init(Cipher.ENCRYPT_MODE,            new SecretKeySpec(Hex.decode("30e69252758e5346"), "DES"),            new IvParameterSpec(Hex.decode("7c1c1ab9c454a688")));        byte[]  out = cEnc.doFinal(input);        char[]  password = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };        Cipher  cDec = makePBECipherUsingParam(                            "PBEWithSHA1AndDES",                            Cipher.DECRYPT_MODE,                            password,                            Hex.decode("7d60435f02e9e0ae"),                            2048);        byte[]  in = cDec.doFinal(out);        if (!arrayEquals(input, in))        {            fail("DES failed");        }        cDec = makePBECipherWithoutParam(                "PBEWithSHA1AndDES",                Cipher.DECRYPT_MODE,                password,                Hex.decode("7d60435f02e9e0ae"),                2048);        in = cDec.doFinal(out);                if (!arrayEquals(input, in))        {            fail("DES failed without param");        }                //        // DESede        //        cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");        cEnc.init(Cipher.ENCRYPT_MODE,            new SecretKeySpec(Hex.decode("732f2d33c801732b7206756cbd44f9c1c103ddd97c7cbe8e"), "DES"),            new IvParameterSpec(Hex.decode("b07bf522c8d608b8")));        out = cEnc.doFinal(input);        cDec = makePBECipherUsingParam(                            "PBEWithSHAAnd3-KeyTripleDES-CBC",                            Cipher.DECRYPT_MODE,                            password,                            Hex.decode("7d60435f02e9e0ae"),                            2048);        in = cDec.doFinal(out);        if (!arrayEquals(input, in))        {            fail("DESede failed");        }        //        // 40Bit RC2        //        cEnc = Cipher.getInstance("RC2/CBC/PKCS7Padding", "BC");        cEnc.init(Cipher.ENCRYPT_MODE,            new SecretKeySpec(Hex.decode("732f2d33c8"), "RC2"),            new IvParameterSpec(Hex.decode("b07bf522c8d608b8")));        out = cEnc.doFinal(input);        cDec = makePBECipherUsingParam(                            "PBEWithSHAAnd40BitRC2-CBC",                            Cipher.DECRYPT_MODE,                            password,                            Hex.decode("7d60435f02e9e0ae"),                            2048);        in = cDec.doFinal(out);        if (!arrayEquals(input, in))        {            fail("RC2 failed");        }        //        // 128bit RC4        //        cEnc = Cipher.getInstance("RC4", "BC");        cEnc.init(Cipher.ENCRYPT_MODE,            new SecretKeySpec(Hex.decode("732f2d33c801732b7206756cbd44f9c1"), "RC4"));        out = cEnc.doFinal(input);        cDec = makePBECipherUsingParam(                            "PBEWithSHAAnd128BitRC4",                            Cipher.DECRYPT_MODE,                            password,                            Hex.decode("7d60435f02e9e0ae"),                            2048);        in = cDec.doFinal(out);        if (!arrayEquals(input, in))        {            fail("RC4 failed");        }        cDec = makePBECipherWithoutParam(                "PBEWithSHAAnd128BitRC4",                Cipher.DECRYPT_MODE,                password,                Hex.decode("7d60435f02e9e0ae"),                2048);        in = cDec.doFinal(out);                if (!arrayEquals(input, in))        {            fail("RC4 failed without param");        }        for (int i = 0; i != pkcs12Tests.length; i++)        {            pkcs12Tests[i].perform();        }                for (int i = 0; i != openSSLTests.length; i++)        {            openSSLTests[i].perform();        }        testPBEHMac("PBEWithHMacSHA1", hMac1);        testPBEHMac("PBEWithHMacRIPEMD160", hMac2);    }    public String getName()    {        return "PBETest";    }    public static void main(        String[]    args)    {        Security.addProvider(new BouncyCastleProvider());        runTest(new PBETest());    }}

⌨️ 快捷键说明

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