當前位置: 首頁>>代碼示例>>Java>>正文


Java EncryptedPrivateKeyInfo.getKeySpec方法代碼示例

本文整理匯總了Java中javax.crypto.EncryptedPrivateKeyInfo.getKeySpec方法的典型用法代碼示例。如果您正苦於以下問題:Java EncryptedPrivateKeyInfo.getKeySpec方法的具體用法?Java EncryptedPrivateKeyInfo.getKeySpec怎麽用?Java EncryptedPrivateKeyInfo.getKeySpec使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.EncryptedPrivateKeyInfo的用法示例。


在下文中一共展示了EncryptedPrivateKeyInfo.getKeySpec方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readPrivateKey

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
private static PKCS8EncodedKeySpec readPrivateKey(File keyFile, Optional<String> keyPassword)
        throws IOException, GeneralSecurityException
{
    String content = Files.toString(keyFile, US_ASCII);

    Matcher matcher = KEY_PATTERN.matcher(content);
    if (!matcher.find()) {
        throw new KeyStoreException("found no private key: " + keyFile);
    }
    byte[] encodedKey = base64Decode(matcher.group(1));

    if (!keyPassword.isPresent()) {
        return new PKCS8EncodedKeySpec(encodedKey);
    }

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(encodedKey);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    SecretKey secretKey = keyFactory.generateSecret(new PBEKeySpec(keyPassword.get().toCharArray()));

    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(DECRYPT_MODE, secretKey, encryptedPrivateKeyInfo.getAlgParameters());

    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
 
開發者ID:airlift,項目名稱:drift,代碼行數:25,代碼來源:PemReader.java

示例2: getPrivateKey

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
/**
 * Reads the private key from an encrypted PKCS#8 file and returns it as an ECPrivateKey instance.
 * 
 * @param A PKCS#8 (.key) file containing the private key with value "s"
 * @return The private key as an ECPrivateKey instance
 */
public static ECPrivateKey getPrivateKey(String keyFilePath) {
	Path fileLocation = Paths.get(keyFilePath);
	byte[] pkcs8ByteArray;
	
	try {
		pkcs8ByteArray = Files.readAllBytes(fileLocation);
		
		// The DER encoded private key is password-based encrypted and provided in PKCS#8. So we need to decrypt it first
		PBEKeySpec pbeKeySpec = new PBEKeySpec(GlobalValues.PASSPHRASE_FOR_CERTIFICATES_AND_KEYS.toString().toCharArray());
	    EncryptedPrivateKeyInfo encryptedPrivKeyInfo = new EncryptedPrivateKeyInfo(pkcs8ByteArray);
	    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(encryptedPrivKeyInfo.getAlgName());
	    Key secret = secretKeyFactory.generateSecret(pbeKeySpec);
	    PKCS8EncodedKeySpec pkcs8PrivKeySpec = encryptedPrivKeyInfo.getKeySpec(secret);
		
		ECPrivateKey privateKey = (ECPrivateKey) KeyFactory.getInstance("EC").generatePrivate(pkcs8PrivKeySpec);

		return privateKey;
	} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException | InvalidKeyException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred while trying to access private key at " +
				  "location '" + keyFilePath + "'");
		return null;
	} 
}
 
開發者ID:V2GClarity,項目名稱:RISE-V2G,代碼行數:30,代碼來源:SecurityUtils.java

示例3: getEncodedPrivateKeySpec

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
public static PKCS8EncodedKeySpec getEncodedPrivateKeySpec(String key, String password) throws Exception {
    String privateKeyPEM = key
            .replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "")
            .replace("-----END ENCRYPTED PRIVATE KEY-----", "")
            .replaceAll("\\s", "");

    // decode to get the binary DER representation
    byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM);

    EncryptedPrivateKeyInfo epkInfo = new EncryptedPrivateKeyInfo(privateKeyDER);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, epkInfo.getAlgParameters());

    return epkInfo.getKeySpec(cipher);
}
 
開發者ID:gahana,項目名稱:edge-jwt-sample,代碼行數:20,代碼來源:JWTValidator.java

示例4: getEncodedPrivateKeySpec

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
public PKCS8EncodedKeySpec getEncodedPrivateKeySpec(String key, String password) throws Exception {
    String privateKeyPEM = key
            .replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "")
            .replace("-----END ENCRYPTED PRIVATE KEY-----", "")
            .replaceAll("\\s", "");

    // decode to get the binary DER representation
    byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM);

    EncryptedPrivateKeyInfo epkInfo = new EncryptedPrivateKeyInfo(privateKeyDER);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, epkInfo.getAlgParameters());

    return epkInfo.getKeySpec(cipher);
}
 
開發者ID:gahana,項目名稱:edge-jwt-sample,代碼行數:20,代碼來源:JWTValidatorTest.java

示例5: main

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
public static void main(String[] argv) throws Exception {
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
            Base64.getMimeDecoder().decode(PKCS8PrivateKey));
    PBEKeySpec pks = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance(epki.getAlgName());
    SecretKey sk = skf.generateSecret(pks);
    PKCS8EncodedKeySpec keySpec = epki.getKeySpec(sk);

    // Get the key algorithm and make sure it's what we expect
    String alg = keySpec.getAlgorithm();
    if (!alg.equals(keyAlg)) {
        throw new Exception("Expected: " + keyAlg + ", Got: " + alg);
    }

    System.out.println("Test passed");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:Algorithm.java

示例6: generateKeySpec

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
private static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidKeyException, InvalidAlgorithmParameterException {

    if (password == null) {
        return new PKCS8EncodedKeySpec(key);
    }

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());

    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
 
開發者ID:floragunncom,項目名稱:search-guard,代碼行數:19,代碼來源:PemKeyReader.java

示例7: generateKeySpec

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
/**
 * Generates a key specification for an (encrypted) private key.
 *
 * @param password characters, if {@code null} or empty an unencrypted key is assumed
 * @param key bytes of the DER encoded private key
 *
 * @return a key specification
 *
 * @throws IOException if parsing {@code key} fails
 * @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
 * @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
 * @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
 * @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
 *                             {@code key}
 * @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
 */
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidKeyException, InvalidAlgorithmParameterException {

    if (password == null || password.length == 0) {
        return new PKCS8EncodedKeySpec(key);
    }

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());

    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
 
開發者ID:wuyinxian124,項目名稱:netty4.0.27Learn,代碼行數:35,代碼來源:SslContext.java

示例8: 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

示例9: testGetKeySpecCipher01

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
public final void testGetKeySpecCipher01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData.algName0[i][0],
                    EncryptedPrivateKeyInfoData.encryptedData);

            try {

                // check that method under test throws NPE
                epki.getKeySpec((Cipher) null);
                fail(getName() + "NullPointerException has not been thrown");

            } catch (NullPointerException ok) {
            } catch (InvalidKeySpecException e) {
                fail(getName() + "Unexpected exception: " + e);
            }

            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:26,代碼來源:EncryptedPrivateKeyInfoTest.java

示例10: testGetKeySpecKey01

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
public final void testGetKeySpecKey01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData.algName0[i][0],
                    EncryptedPrivateKeyInfoData.encryptedData);

            try {

                // check that method under test throws NPE
                epki.getKeySpec((Key) null);
                fail(getName() + "NullPointerException has not been thrown");

            } catch (NullPointerException ok) {
            } catch (InvalidKeyException e) {
                fail(getName() + "Unexpected exception: " + e);
            }

            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:26,代碼來源:EncryptedPrivateKeyInfoTest.java

示例11: createRsaPrivateKey

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
/**
 * バイト列からRSA秘密鍵を生成する。
 * 
 * @param b 生成バイト列。
 * @param password 秘密鍵ファイルを保護するためのパスワード。
 * @return RSA秘密鍵。
 * @throws NoSuchAlgorithmException 秘密鍵ファイルの暗號化アルゴリズムがサポートされていないことを表す。
 * @throws NoSuchPaddingException 秘密鍵ファイルの暗號化アルゴリズムがサポートされていないことを表す。
 * @throws InvalidKeyException 秘密鍵ファイルの復號鍵が不適切であることを表す。
 * @throws InvalidAlgorithmParameterException 秘密鍵ファイルの復號パラメタが不適切であることを表す。
 * @throws InvalidKeySpecException 読込んだ鍵データの形式が正しくないことを表す。
 */
public static PrivateKey createRsaPrivateKey(byte[] b, char[] password) throws NoSuchAlgorithmException,
		NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException {
	try {

		EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(b);
		PBEKeySpec pbeKeySpec = new PBEKeySpec(password);

		SecretKey pbeKey = SecretKeyFactory.getInstance(encryptedKeyInfo.getAlgName()).generateSecret(pbeKeySpec);
		Cipher cipher = Cipher.getInstance(encryptedKeyInfo.getAlgName());
		cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedKeyInfo.getAlgParameters());

		PKCS8EncodedKeySpec keySpec = encryptedKeyInfo.getKeySpec(cipher);
		return rsaKeyFactory.generatePrivate(keySpec);
	} catch (IOException ex) {
		throw new IllegalArgumentException(ex);
	}
}
 
開發者ID:agwlvssainokuni,項目名稱:springapp,代碼行數:30,代碼來源:KeyUtil.java

示例12: generateKeySpec

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
private static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
               InvalidKeyException, InvalidAlgorithmParameterException {

    if (password == null || password.length == 0) {
        return new PKCS8EncodedKeySpec(key);
    }

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());

    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
 
開發者ID:Gadreel,項目名稱:divconq,代碼行數:19,代碼來源:JdkSslServerContext.java

示例13: testGetKeySpecCipher01

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for getKeySpec method.",
    method = "getKeySpec",
    args = {javax.crypto.Cipher.class}
)
public final void testGetKeySpecCipher01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData.algName0[i][0],
                    EncryptedPrivateKeyInfoData.encryptedData);

            try {

                // check that method under test throws NPE
                epki.getKeySpec((Cipher) null);
                fail(getName() + "NullPointerException has not been thrown");

            } catch (NullPointerException ok) {
            } catch (InvalidKeySpecException e) {
                fail(getName() + "Unexpected exception: " + e);
            }

            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:32,代碼來源:EncryptedPrivateKeyInfoTest.java

示例14: testGetKeySpecKey01

import javax.crypto.EncryptedPrivateKeyInfo; //導入方法依賴的package包/類
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "Can not check NoSuchAlgorithmException",
    method = "getKeySpec",
    args = {java.security.Key.class}
)
public final void testGetKeySpecKey01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
                    EncryptedPrivateKeyInfoData.algName0[i][0],
                    EncryptedPrivateKeyInfoData.encryptedData);

            try {

                // check that method under test throws NPE
                epki.getKeySpec((Key) null);
                fail(getName() + "NullPointerException has not been thrown");

            } catch (NullPointerException ok) {
            } catch (InvalidKeyException e) {
                fail(getName() + "Unexpected exception: " + e);
            }

            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:32,代碼來源:EncryptedPrivateKeyInfoTest.java

示例15: 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


注:本文中的javax.crypto.EncryptedPrivateKeyInfo.getKeySpec方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。