use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project faf-java-server by FAForever.
the class RsaHelper method readPkcs1.
/**
* Reads a specified PKCS#1 formatted key (without any headers or footers). Having the key in PKCS#8 format would be
* easier as bouncy castle provides a one-liner to read it but since the original FAF server had its key in PKCS#1,
* this method allows to just use the same key string instead of having to convert it.
*/
@SneakyThrows
RSAPrivateCrtKey readPkcs1(String content) {
ASN1Sequence seq = ASN1Sequence.getInstance(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8)));
Assert.notNull(seq, "RSA private key has not been specified properly. Value is '" + content + "'.");
Assert.isTrue(seq.size() == 9, "Invalid RSA Private Key ASN1 sequence.");
RSAPrivateKey key = RSAPrivateKey.getInstance(seq);
RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient());
return (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate(privSpec);
}
Aggregations