use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project fabric-sdk-java by hyperledger.
the class NetworkConfig method getPrivateKeyFromString.
private static PrivateKey getPrivateKeyFromString(String data) throws IOException {
final Reader pemReader = new StringReader(data);
final PrivateKeyInfo pemPair;
try (PEMParser pemParser = new PEMParser(pemReader)) {
pemPair = (PrivateKeyInfo) pemParser.readObject();
}
return new JcaPEMKeyConverter().getPrivateKey(pemPair);
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project vcert-java by Venafi.
the class PEMCollection method derPrivateKey.
public RawPrivateKey derPrivateKey() {
if (Objects.isNull(this.privateKey)) {
return null;
}
try {
RawPrivateKey result = new RawPrivateKey();
if (KeyType.from(this.privateKey.getAlgorithm()) == KeyType.RSA) {
PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(this.privateKey.getEncoded());
ASN1Primitive privateKeyPKCS1ASN1 = pkInfo.parsePrivateKey().toASN1Primitive();
result.data = privateKeyPKCS1ASN1.getEncoded();
} else {
result.data = this.privateKey.getEncoded();
}
if (privateKeyPassword == null) {
return result;
} else {
result.iv = new byte[SECRET_KEY_LENGTH_BITS / 8];
new SecureRandom().nextBytes(result.iv);
SecretKeySpec secretKey = passwordToCipherSecretKey(privateKeyPassword.toCharArray(), result.iv);
Cipher c = Cipher.getInstance(CIPHER_TRANSFORMATION);
c.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(result.iv));
result.data = c.doFinal(result.data);
return result;
}
} catch (IOException | GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project vcert-java by Venafi.
the class PEMCollection method decryptPKCS8PrivateKey.
public static PrivateKey decryptPKCS8PrivateKey(PKCS8EncryptedPrivateKeyInfo encryptedPrivateKeyInfo, String keyPassword) throws PEMException, OperatorCreationException, PKCSException {
InputDecryptorProvider pkcs8Prov = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(keyPassword.toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
PrivateKeyInfo decryptedPrivateKeyInfo = encryptedPrivateKeyInfo.decryptPrivateKeyInfo(pkcs8Prov);
return converter.getPrivateKey(decryptedPrivateKeyInfo);
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project radixdlt by radixdlt.
the class RadixKeyStore method encodePrivKey.
/**
* Encodes the specified raw secp256k1 private key into an ASN.1 encoded <a
* href="https://tools.ietf.org/html/rfc5208#section-5">Private Key Info</a> structure.
*
* <p>Note that the encoded key will not include a public key.
*
* @param rawkey The raw secp256k1 private key.
* @return The ASN.1 encoded private key.
* @throws IOException If an error occurs encoding the ASN.1 key.
*/
private static byte[] encodePrivKey(byte[] rawkey) throws IOException {
AlgorithmIdentifier ecSecp256k1 = new AlgorithmIdentifier(OID_EC_ENCRYPTION, OID_SECP256K1_CURVE);
BigInteger key = new BigInteger(1, rawkey);
ECPrivateKey privateKey = new ECPrivateKey(ECKeyPair.BYTES * Byte.SIZE, key, OID_SECP256K1_CURVE);
PrivateKeyInfo pki = new PrivateKeyInfo(ecSecp256k1, privateKey);
return pki.getEncoded();
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class EncryptedValueBuilder method build.
/**
* Build an EncryptedValue structure containing the private key contained in the passed info
* structure.
*
* @param privateKeyInfo a PKCS#8 private key info structure.
* @return an EncryptedValue containing an EncryptedPrivateKeyInfo structure.
* @throws CRMFException on a failure to encrypt the data, or wrap the symmetric key for this
* value.
*/
public EncryptedValue build(PrivateKeyInfo privateKeyInfo) throws CRMFException {
PKCS8EncryptedPrivateKeyInfoBuilder encInfoBldr = new PKCS8EncryptedPrivateKeyInfoBuilder(privateKeyInfo);
AlgorithmIdentifier intendedAlg = privateKeyInfo.getPrivateKeyAlgorithm();
AlgorithmIdentifier symmAlg = encryptor.getAlgorithmIdentifier();
DERBitString encSymmKey;
try {
PKCS8EncryptedPrivateKeyInfo encInfo = encInfoBldr.build(encryptor);
encSymmKey = new DERBitString(wrapper.generateWrappedKey(encryptor.getKey()));
AlgorithmIdentifier keyAlg = wrapper.getAlgorithmIdentifier();
ASN1OctetString valueHint = null;
return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, new DERBitString(encInfo.getEncryptedData()));
} catch (IllegalStateException e) {
throw new CRMFException("cannot encode key: " + e.getMessage(), e);
} catch (OperatorException e) {
throw new CRMFException("cannot wrap key: " + e.getMessage(), e);
}
}
Aggregations