當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。