這篇文章給大家介紹RSA如何無視PEM文件格式直接讀取PEM文件為PrivateKey和PublicKey,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站制作、做網(wǎng)站與策劃設(shè)計(jì),金山網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:金山等地區(qū)。金山做網(wǎng)站價(jià)格咨詢:028-86922220
RSA無視PEM文件格式(pkcs#1,pkcs#8,有無密碼 )直接讀取PEM文件為PrivateKey,PublicKey
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.PEMDecryptorProvider; import org.bouncycastle.openssl.PEMEncryptedKeyPair; import org.bouncycastle.openssl.PEMKeyPair; import org.bouncycastle.openssl.PEMParser; import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder; import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder; import org.bouncycastle.operator.InputDecryptorProvider; import org.bouncycastle.operator.OperatorCreationException; import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo; import org.bouncycastle.pkcs.PKCSException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.StringReader; import java.security.Key; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; /** * RSA無視PEM文件格式(pkcs#1,pkcs#8,有無密碼 )直接讀取PEM文件為PrivateKey,PublicKey */ public class RSAUtil { private final static Logger logger = LoggerFactory.getLogger(RSAUtil.class); static { java.security.Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() ); } public static PrivateKey privateKey(String pemString, String password) { try { return (PrivateKey) parseKey(pemString, password); } catch (IOException e) { logger.error("privateKey error", e); e.printStackTrace(); } return null; } public static PrivateKey privateKey(String pemString) { try { return (PrivateKey) parseKey(pemString, null); } catch (IOException e) { logger.error("privateKey error", e); } return null; } public static PublicKey publicKey(String pemString) { try { return (PublicKey) parseKey(pemString, null); } catch (IOException e) { logger.error("publicKey error", e); } return null; } /** * Parses a Key instance from a PEM representation. ** When the provided key is encrypted, the provided pass phrase is applied. * * @param pemString a PEM representation of a private key (cannot be null or empty) * @param passPhrase optional pass phrase (must be present if the private key is encrypted). * @return a Key instance (never null) */ public static Key parseKey(String pemString, String passPhrase) throws IOException { if (passPhrase == null) { passPhrase = ""; } try (StringReader reader = new StringReader(pemString); // PEMParser pemParser = new PEMParser(reader)) { final Object object = pemParser.readObject(); final JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME); final KeyPair kp; if (object instanceof PEMEncryptedKeyPair) { // Encrypted key - we will use provided password final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passPhrase.toCharArray()); kp = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)); } else if (object instanceof PKCS8EncryptedPrivateKeyInfo) { // Encrypted key - we will use provided password try { final PKCS8EncryptedPrivateKeyInfo encryptedInfo = (PKCS8EncryptedPrivateKeyInfo) object; final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(passPhrase.toCharArray()); final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo(provider); return converter.getPrivateKey(privateKeyInfo); } catch (PKCSException | OperatorCreationException e) { throw new IOException("Unable to decrypt private key.", e); } } else if (object instanceof PrivateKeyInfo) { return converter.getPrivateKey((PrivateKeyInfo) object); } else if (object instanceof SubjectPublicKeyInfo) { return converter.getPublicKey((SubjectPublicKeyInfo) object); } else { // Unencrypted key - no password needed kp = converter.getKeyPair((PEMKeyPair) object); } return kp.getPrivate(); } } }
關(guān)于RSA如何無視PEM文件格式直接讀取PEM文件為PrivateKey和PublicKey就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。