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


Java EncryptedPrivateKeyInfo类代码示例

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


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

示例1: testBad

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
static void testBad(String s) throws Exception {
    System.err.println("Trying " + s);
    try {
        new ObjectIdentifier(s);
        throw new Exception("should be invalid ObjectIdentifier");
    } catch (IOException ioe) {
        System.err.println(ioe);
    }

    try {
        new Oid(s);
        throw new Exception("should be invalid Oid");
    } catch (GSSException gsse) {
        ;
    }

    try {
        new EncryptedPrivateKeyInfo(s, new byte[8]);
        throw new Exception("should be invalid algorithm");
    } catch (NoSuchAlgorithmException e) {
        ;
    }
}
 
开发者ID:ojdkbuild,项目名称:lookaside_java-1.8.0-openjdk,代码行数:24,代码来源:OidFormat.java

示例2: engineSetKeyEntry

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
public void engineSetKeyEntry(String alias, byte[] encodedKey, Certificate[] certChain)
    throws KeyStoreException
{
    alias = alias.toLowerCase();
    if (trustedCerts.containsKey(alias))
        throw new KeyStoreException("\"" + alias + "\" is a trusted certificate entry");
    try
    {
        new EncryptedPrivateKeyInfo(encodedKey);
    }
    catch (IOException ioe)
    {
        throw new KeyStoreException("encoded key is not an EncryptedPrivateKeyInfo");
    }
    privateKeys.put(alias, encodedKey);
    if (certChain != null)
        certChains.put(alias, certChain);
    else
        certChains.put(alias, new Certificate[0]);
    if (!aliases.contains(alias))
    {
        dates.put(alias, new Date());
        aliases.add(alias);
    }
}
 
开发者ID:uhuru-mobile,项目名称:mobile-store,代码行数:26,代码来源:JKS.java

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

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

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

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

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

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

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

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

示例11: EncryptedPrivateKeyInfo

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
 * Test #6 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
 * <br>
 * Assertion: byte array is copied to prevent subsequent modification <br>
 * Test preconditions: valid array passed then modified <br>
 * Expected: getEncoded(), invoked after above modification, must return
 * array as it was before the modification
 *
 * @throws IOException
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
    method = "EncryptedPrivateKeyInfo",
    args = {byte[].class}
)
public final void testEncryptedPrivateKeyInfobyteArray6() throws Exception {
    byte[] encoded = EncryptedPrivateKeyInfoData
            .getValidEncryptedPrivateKeyInfoEncoding("DSA");
    byte[] encodedCopy = encoded.clone();
    // pass valid array
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encodedCopy);
    // modify array passed
    encodedCopy[9] = (byte) 6;
    // check that internal state has not been affected
    assertTrue(Arrays.equals(encoded, epki.getEncoded()));
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:28,代码来源:EncryptedPrivateKeyInfoTest.java

示例12: testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
 * Test #3 for
 * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
 * </code>
 * constructor <br>
 * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
 * empty, i.e. 0-length <br>
 * Test preconditions: pass empty encrypted data <br>
 * Expected: <code>IllegalArgumentException</code>
 *
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "IllegalArgumentException checked.",
    method = "EncryptedPrivateKeyInfo",
    args = {java.security.AlgorithmParameters.class, byte[].class}
)
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3()
        throws Exception {
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
        // use pregenerated AlgorithmParameters encodings
        ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));

        new EncryptedPrivateKeyInfo(ap, new byte[] {});
        fail(getName() + ": IllegalArgumentException has not been thrown");

    } catch (IllegalArgumentException ok) {
    }
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:33,代码来源:EncryptedPrivateKeyInfoTest.java

示例13: testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
 * Test #4 for
 * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
 * </code>
 * constructor <br>
 * Assertion: byte array is copied to prevent subsequent modification <br>
 * Test preconditions: valid array passed then modified <br>
 * Expected: getEncryptedData(), invoked after above modification, must
 * return array as it was before the modification
 *
 * @throws IOException
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "Functionality checked.",
    method = "EncryptedPrivateKeyInfo",
    args = {java.security.AlgorithmParameters.class, byte[].class}
)
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4()
        throws Exception {
    AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
    // use pregenerated AlgorithmParameters encodings
    ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));

    byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
    // pass valid array
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
            encryptedDataCopy);

    // modify array passed
    encryptedDataCopy[0] = (byte) 6;

    // check that internal state has not been affected
    assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
            epki.getEncryptedData()));
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:37,代码来源:EncryptedPrivateKeyInfoTest.java

示例14: testGetAlgParameters01_01

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for getAlgParameters method.",
    method = "getAlgParameters",
    args = {}
)
public final void testGetAlgParameters01_01() throws Exception {
    byte[] validEncodingWithUnknownAlgOID = EncryptedPrivateKeyInfoData
            .getValidEncryptedPrivateKeyInfoEncoding("DH");
    // correct oid value
    validEncodingWithUnknownAlgOID[18] = 0;
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
            validEncodingWithUnknownAlgOID);

    assertNull(epki.getAlgParameters());
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:17,代码来源:EncryptedPrivateKeyInfoTest.java

示例15: testGetAlgParameters02

import javax.crypto.EncryptedPrivateKeyInfo; //导入依赖的package包/类
/**
 * Test #2 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 does not contain
 * algorithm parameters encoding <br>
 * Expected: <code>null</code> 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 testGetAlgParameters02() 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],
                                    false));

            // check that method under test returns null
            assertNull(epki.getAlgParameters());

            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
 
开发者ID:keplersj,项目名称:In-the-Box-Fork,代码行数:36,代码来源:EncryptedPrivateKeyInfoTest.java


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