Java Code Examples for org.bouncycastle.asn1.ASN1Primitive#getEncoded()

The following examples show how to use org.bouncycastle.asn1.ASN1Primitive#getEncoded() . 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: 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 2
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 3
Source File: Asn1Dump.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get dump of the supplied DER encoded ASN.1 object.
 *
 * @param der
 *            DER encoded ASN.1 object
 * @return Dump of object
 * @throws Asn1Exception
 *             A problem was encountered getting the ASN.1 dump
 * @throws IOException
 *             If an I/O problem occurred
 */
public String dump(byte[] der) throws Asn1Exception, IOException {
	try {
		ASN1Primitive derObject = ASN1Primitive.fromByteArray(der);

		// if size of re-encoded DER primitive differs from input data there must be sth wrong
		if (derObject.getEncoded().length < der.length) {
			throw new Asn1Exception(res.getString("NoAsn1DumpObject.exception.message"));
		}

		return dump(derObject);
	} catch (IOException ex) {
		throw new Asn1Exception(res.getString("NoAsn1DumpObject.exception.message"), ex);
	}
}
 
Example 4
Source File: DSSASN1Utils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a CMSSignedData from the provided {@code attribute}
 * @param attribute {@link Attribute} to generate {@link CMSSignedData} from
 * @return {@link CMSSignedData}
 * @throws IOException in case of encoding exception
 * @throws CMSException in case if the provided {@code attribute} cannot be converted to {@link CMSSignedData}
 */
public static CMSSignedData getCMSSignedData(Attribute attribute) throws CMSException, IOException {
	ASN1Encodable value = getAsn1Encodable(attribute);
	if (value instanceof DEROctetString) {
		LOG.warn("Illegal content for CMSSignedData (OID : {}) : OCTET STRING is not allowed !", attribute.getAttrType());
	} else {
		ASN1Primitive asn1Primitive = value.toASN1Primitive();
		return new CMSSignedData(asn1Primitive.getEncoded());
	}
	return null;
}
 
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;
}