当前位置: 首页>>代码示例>>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;未经允许,请勿转载。