Java Code Examples for org.bouncycastle.asn1.pkcs.PrivateKeyInfo#parsePrivateKey()

The following examples show how to use org.bouncycastle.asn1.pkcs.PrivateKeyInfo#parsePrivateKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ed25519PrivateKey.java    From hedera-sdk-java with Apache License 2.0 6 votes vote down vote up
private static Ed25519PrivateKey fromPrivateKeyInfo(PrivateKeyInfo privateKeyInfo) {
    Ed25519PrivateKeyParameters privKeyParams;
    Ed25519PublicKeyParameters pubKeyParams = null;

    try {
        ASN1Encodable privateKey = privateKeyInfo.parsePrivateKey();
        privKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey).getOctets(), 0);

        ASN1BitString pubKeyData = privateKeyInfo.getPublicKeyData();

        if (pubKeyData != null) {
            pubKeyParams = new Ed25519PublicKeyParameters(pubKeyData.getOctets(), 0);
        }

    } catch (IOException e) {
        throw new BadKeyException(e);
    }

    if (pubKeyParams != null) {
        return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
    } else {
        return new Ed25519PrivateKey(privKeyParams);
    }
}
 
Example 2
Source File: TlsHelper.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link PEMKeyPair} object with direct access to the public and private keys given a PKCS #8 private key.
 *
 * @param privateKeyInfo the PKCS #8 private key info
 * @return the PKCS #1 public and private key pair
 * @throws IOException if there is an error converting the key pair
 */
private static PEMKeyPair convertPrivateKeyFromPKCS8ToPKCS1(PrivateKeyInfo privateKeyInfo) throws IOException {
    // Parse the key wrapping to determine the internal key structure
    ASN1Encodable asn1PrivateKey = privateKeyInfo.parsePrivateKey();

    // Convert the parsed key to an RSA private key
    RSAPrivateKey keyStruct = RSAPrivateKey.getInstance(asn1PrivateKey);

    // Create the RSA public key from the modulus and exponent
    RSAPublicKey pubSpec = new RSAPublicKey(
        keyStruct.getModulus(), keyStruct.getPublicExponent());

    // Create an algorithm identifier for forming the key pair
    AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
    if (isVerbose()) {
        logger.info("Converted private key from PKCS #8 to PKCS #1 RSA private key");
    }

    // Create the key pair container
    return new PEMKeyPair(new SubjectPublicKeyInfo(algId, pubSpec), new PrivateKeyInfo(algId, keyStruct));
}
 
Example 3
Source File: BCECUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
/**
 * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是PKCS1标准的,
 * 这个工具函数的主要目的就是为了能生成一个openssl可以“识别”的ECC私钥
 *
 * @param priKey
 * @param pubKey
 * @return
 * @throws IOException
 */
public static byte[] convertEcPriKeyToPkcs1Der(ECPrivateKeyParameters priKey,
                                               ECPublicKeyParameters pubKey) throws IOException {
    byte[] pkcs8Bytes = convertEcPriKeyToPkcs8Der(priKey, pubKey);
    PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes);
    ASN1Encodable encodable = pki.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    byte[] pkcs1Bytes = primitive.getEncoded();
    return pkcs1Bytes;
}
 
Example 4
Source File: KeyUtils.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static byte[] getPkcs1Bytes(PrivateKey privateKey) throws IOException{
    byte[] privBytes = privateKey.getEncoded();
    PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(privBytes);
    ASN1Encodable encodable = pkInfo.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    return primitive.getEncoded();
}
 
Example 5
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 3 votes vote down vote up
/**
 * 将ECC私钥转换为SEC1标准的字节流
 * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是SEC1标准的,
 * 这个工具函数的主要目的就是为了能生成一个openssl可以直接“识别”的ECC私钥.
 * 相对RSA私钥的PKCS1标准,ECC私钥的标准为SEC1
 *
 * @param priKey
 * @param pubKey
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeyToSEC1(
        ECPrivateKeyParameters priKey, ECPublicKeyParameters pubKey) throws IOException {
    byte[] pkcs8Bytes = convertECPrivateKeyToPKCS8(priKey, pubKey);
    PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes);
    ASN1Encodable encodable = pki.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    byte[] sec1Bytes = primitive.getEncoded();
    return sec1Bytes;
}
 
Example 6
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 3 votes vote down vote up
/**
 * 将ECC私钥转换为SEC1标准的字节流
 * openssl d2i_ECPrivateKey函数要求的DER编码的私钥也是SEC1标准的,
 * 这个工具函数的主要目的就是为了能生成一个openssl可以直接“识别”的ECC私钥.
 * 相对RSA私钥的PKCS1标准,ECC私钥的标准为SEC1
 *
 * @param priKey
 * @param pubKey
 * @return
 * @throws IOException
 */
public static byte[] convertECPrivateKeyToSEC1(ECPrivateKeyParameters priKey,
    ECPublicKeyParameters pubKey) throws IOException {
    byte[] pkcs8Bytes = convertECPrivateKeyToPKCS8(priKey, pubKey);
    PrivateKeyInfo pki = PrivateKeyInfo.getInstance(pkcs8Bytes);
    ASN1Encodable encodable = pki.parsePrivateKey();
    ASN1Primitive primitive = encodable.toASN1Primitive();
    byte[] sec1Bytes = primitive.getEncoded();
    return sec1Bytes;
}
 
Example 7
Source File: EccUtil.java    From keystore-explorer with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Converts PKCS#8 EC private key (RFC 5208 ASN.1 PrivateKeyInfo structure) to "traditional" OpenSSL
 * ASN.1 structure ECPrivateKey from RFC 5915. As ECPrivateKey is already in the PrivateKey field of PrivateKeyInfo,
 * this must only be extracted:
 *
 * SEQUENCE {
 *	  INTEGER 0
 *	  SEQUENCE {
 *	    OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1)
 *	    OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7)
 *	    }
 *	  OCTET STRING, encapsulates {
 *	    SEQUENCE {
 *	      INTEGER 1
 *	      OCTET STRING
 *	        17 12 CA 42 16 79 1B 45    ...B.y.E
 *	        ...
 *	        C8 B2 66 0A E5 60 50 0B
 *	      [0] {
 *	        OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7)
 *	        }
 *	      [1] {
 *	        BIT STRING
 *	          04 61 C0 08 B4 89 A0 50    .a.....P
 *            ...
 *	          AE D5 ED C3 4D 0E 47 91    ....M.G.
 *	          89                         .
 *	        }
 *	      }
 *	    }
 *	  }
 *
 * @param ecPrivateKey An EC key
 * @return Object holding ASN1 ECPrivateKey structure
 * @throws IOException When ECPrivateKey structure in PrivateKeyInfo's PrivateKey field cannot be parsed
 */
public static org.bouncycastle.asn1.sec.ECPrivateKey convertToECPrivateKeyStructure(ECPrivateKey ecPrivateKey)
		throws IOException {
	byte[] encoded = ecPrivateKey.getEncoded();
	PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(encoded);
	ASN1Encodable privateKey = privateKeyInfo.parsePrivateKey();
	return org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(privateKey);
}