当前位置: 首页>>代码示例>>Java>>正文


Java EncryptedPrivateKeyInfo.getAlgParameters方法代码示例

本文整理汇总了Java中javax.crypto.EncryptedPrivateKeyInfo.getAlgParameters方法的典型用法代码示例。如果您正苦于以下问题:Java EncryptedPrivateKeyInfo.getAlgParameters方法的具体用法?Java EncryptedPrivateKeyInfo.getAlgParameters怎么用?Java EncryptedPrivateKeyInfo.getAlgParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.EncryptedPrivateKeyInfo的用法示例。


在下文中一共展示了EncryptedPrivateKeyInfo.getAlgParameters方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: decryptPrivateKey

import javax.crypto.EncryptedPrivateKeyInfo; //导入方法依赖的package包/类
private static PrivateKey decryptPrivateKey(String encryptedPrivateKey, String secret)
throws GeneralSecurityException, IOException {

	byte[] encodedPrivateKey = Base64.getMimeDecoder()
		.decode(encryptedPrivateKey.getBytes(Constants.DEFAULT_ENCODING));

	EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(encodedPrivateKey);
	Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
	PBEKeySpec pbeKeySpec = new PBEKeySpec(secret.toCharArray());
	SecretKeyFactory secretFactory = SecretKeyFactory.getInstance(CIPHER_ALGORITHM);
	Key pbeKey = secretFactory.generateSecret(pbeKeySpec);
	AlgorithmParameters algorithmParameters = encryptPKInfo.getAlgParameters();
	cipher.init(Cipher.DECRYPT_MODE, pbeKey, algorithmParameters);
	KeySpec pkcsKeySpec = encryptPKInfo.getKeySpec(cipher);
	KeyFactory keyFactory = KeyFactory.getInstance("RSA");

	return keyFactory.generatePrivate(pkcsKeySpec);
}
 
开发者ID:SAP,项目名称:iot-starterkit,代码行数:19,代码来源:SecurityUtil.java

示例2: testGetAlgParameters01

import javax.crypto.EncryptedPrivateKeyInfo; //导入方法依赖的package包/类
/**
 * Test #1 for <code>getAlgParameters()</code> method <br>
 * Assertion: returns the algorithm parameters <br>
 * Test preconditions: test object created using ctor which takes encoded
 * form as the only parameter; encoded form passed contains algorithm
 * parameters encoding <br>
 * Expected: corresponding algorithm parameters must be returned
 *
 * @throws IOException
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for getAlgParameters method.",
    method = "getAlgParameters",
    args = {}
)
public final void testGetAlgParameters01() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData
                            .getValidEncryptedPrivateKeyInfoEncoding(
                                    EncryptedPrivateKeyInfoData.algName0[i][0]));

            AlgorithmParameters apar = epki.getAlgParameters();
            if (apar == null) {
                continue;
            }

            // check that method under test returns
            // parameters with the same encoded form
            assertTrue(Arrays
                    .equals(
                            EncryptedPrivateKeyInfoData
                                    .getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]),
                            apar.getEncoded()));
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:44,代码来源:EncryptedPrivateKeyInfoTest.java

示例3: testGetAlgParameters01

import javax.crypto.EncryptedPrivateKeyInfo; //导入方法依赖的package包/类
/**
 * Test #1 for <code>getAlgParameters()</code> method <br>
 * Assertion: returns the algorithm parameters <br>
 * Test preconditions: test object created using ctor which takes encoded
 * form as the only parameter; encoded form passed contains algorithm
 * parameters encoding <br>
 * Expected: corresponding algorithm parameters must be returned
 * 
 * @throws IOException
 */
public final void testGetAlgParameters01() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData
                            .getValidEncryptedPrivateKeyInfoEncoding(
                                    EncryptedPrivateKeyInfoData.algName0[i][0]));

            AlgorithmParameters apar = epki.getAlgParameters();
            if (apar == null) {
                continue;
            }

            // check that method under test returns
            // parameters with the same encoded form
            assertTrue(Arrays
                    .equals(
                            EncryptedPrivateKeyInfoData
                                    .getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]),
                            apar.getEncoded()));
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
开发者ID:shannah,项目名称:cn1,代码行数:38,代码来源:EncryptedPrivateKeyInfoTest.java

示例4: loadPKCS8PrivateKey

import javax.crypto.EncryptedPrivateKeyInfo; //导入方法依赖的package包/类
private static PrivateKey loadPKCS8PrivateKey(byte[] keyBytes, char[] passphrase) throws Exception {
    EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(keyBytes);
    Cipher cipher = Cipher.getInstance("RSA", "BC");
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passphrase);
    SecretKeyFactory secFac = SecretKeyFactory.getInstance(encryptPKInfo.getAlgName(),"BC");
    Key pbeKey = secFac.generateSecret(pbeKeySpec);
    AlgorithmParameters algParams = encryptPKInfo.getAlgParameters();
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
    KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher);
    KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
    return kf.generatePrivate(pkcs8KeySpec);
}
 
开发者ID:cfmlprojects,项目名称:runwar,代码行数:13,代码来源:SSLUtil.java

示例5: decryptForgePkcs8PrivateKeyPem_PBEWithSHA1AndDESede

import javax.crypto.EncryptedPrivateKeyInfo; //导入方法依赖的package包/类
/**
 * Test to show that tripple des PKCS8 private key pem created with ForgeJS
 * can be decrypted with BC. 
 * 
 * @throws Exception 
 */
@Test
public void decryptForgePkcs8PrivateKeyPem_PBEWithSHA1AndDESede() throws Exception {
    // http://bouncy-castle.1462172.n4.nabble.com/Help-with-EncryptedPrivateKeyInfo-td1468363.html
    // https://community.oracle.com/thread/1530354?start=0&tstart=0
    Security.addProvider(new BouncyCastleProvider());

    //PEMParser keyPemParser = new PEMParser(new StringReader(getPkcs8ForgePriKeyPem_PBEWithMD5AndDES()));
    //String passwd = "1234567890";
    PEMParser keyPemParser = new PEMParser(new StringReader(getPkcs8ForgePriKeyPem_EncryptedWithPBEWithSHA1AndDESede()));
    String passwd = "password";
    PemObject keyObj = keyPemParser.readPemObject();
    byte[] keyBytes = keyObj.getContent();

    EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(keyBytes);
    // 1.2.840.113549.1.5.13 == PBEWithMD5AndDES
    // 1.2.840.113549.1.12.1.3 == PBEWithSHA1AndDESede
    String algName = encryptPKInfo.getAlgName();
    String algId = encryptPKInfo.getAlgParameters().getAlgorithm();
    assertEquals("PBEWithSHA1AndDESede", algName);
    assertEquals("1.2.840.113549.1.12.1.3", algId);
    assertEquals("1.2.840.113549.1.12.1.3", PKCS8Generator.PBE_SHA1_3DES.getId());

    // Decrypt private key 
    Cipher cipher = Cipher.getInstance(algName);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd.toCharArray());
    SecretKeyFactory secFac = SecretKeyFactory.getInstance(algName);
    Key pbeKey = secFac.generateSecret(pbeKeySpec);
    AlgorithmParameters algParams = encryptPKInfo.getAlgParameters();
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
    KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey priKeyDecryptedBC = kf.generatePrivate(pkcs8KeySpec);

    // Compare decrypted private key with a version that was decrypted using 
    // openssl and assert that they are the same. 
    JcaPKCS8Generator pkcs8GeneratorNoEnc = new JcaPKCS8Generator(priKeyDecryptedBC, null);
    PemObject pkcs8PemDecryptedBC = pkcs8GeneratorNoEnc.generate();
    StringWriter writer3 = new StringWriter();
    PEMWriter pemWrite3 = new PEMWriter(writer3);
    pemWrite3.writeObject(pkcs8PemDecryptedBC);
    pemWrite3.close();
    String pkcs8StrDecryptedBC = writer3.toString().trim().replaceAll("\\r\\n", "\n");;
    String pkcs8StrDecryptedOpenSSL = getPkcs8ForgePriKeyPem_DecryptedWithOpenSSL().trim().replaceAll("\\r\\n", "\n");; 
    //System.out.println("["+pkcs8StrNoEncBC+"]");
    //System.out.println("["+pkcs8StrNoEncOpenssL+"]");
    assertTrue(pkcs8StrDecryptedBC.equals(pkcs8StrDecryptedOpenSSL));  
}
 
开发者ID:UKCA,项目名称:CAPortal,代码行数:54,代码来源:TestBC_CreatePKCS10.java


注:本文中的javax.crypto.EncryptedPrivateKeyInfo.getAlgParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。