📄 pbetest.java
字号:
package org.bouncycastle.jce.provider.test;import java.security.AlgorithmParameters;import java.security.Security;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() { byte[] salt = new byte[16]; int iCount = 100; for (int i = 0; i != salt.length; i++) { salt[i] = (byte)i; } try { 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"); } } catch (Exception e) { fail("" + algorithm + " failed - exception " + e, e); } } } 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() { byte[] salt = new byte[digest.getDigestSize()]; int iCount = 100; digest.doFinal(salt, 0); try { 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 = 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"); } // // try using parameters // keySpec = new PBEKeySpec(password); c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec), param); dec = c.doFinal(enc); if (!arrayEquals(salt, dec)) { fail("" + algorithm + "failed encryption/decryption test"); } } catch (Exception e) { fail("" + algorithm + " failed - exception " + e, e); } } } 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) }; private OpenSSLTest openSSLTests[] = { new OpenSSLTest("AES", "PBEWITHMD5AND128BITAES-CBC-OPENSSL", 128, 128), new OpenSSLTest("AES", "PBEWITHMD5AND192BITAES-CBC-OPENSSL", 192, 128), new OpenSSLTest("AES", "PBEWITHMD5AND256BITAES-CBC-OPENSSL", 256, 128) }; static byte[] message = Hex.decode("4869205468657265"); private byte[] hMac1 = Hex.decode("bcc42174ccb04f425d9a5c8c4a95d6fd7c372911"); private byte[] hMac2 = Hex.decode("cb1d8bdb6aca9e3fa8980d6eb41ab28a7eb2cfd6");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -