pemreader.java
来自「bouncycastle 是一个JAVA安全提供者」· Java 代码 · 共 576 行 · 第 1/2 页
JAVA
576 行
package org.bouncycastle.openssl;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.Reader;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.PublicKey;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import java.security.spec.DSAPrivateKeySpec;import java.security.spec.DSAPublicKeySpec;import java.security.spec.InvalidKeySpecException;import java.security.spec.KeySpec;import java.security.spec.RSAPrivateCrtKeySpec;import java.security.spec.RSAPublicKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.StringTokenizer;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.IvParameterSpec;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.ASN1Sequence;import org.bouncycastle.asn1.DERInteger;import org.bouncycastle.asn1.cms.ContentInfo;import org.bouncycastle.asn1.x509.RSAPublicKeyStructure;import org.bouncycastle.crypto.PBEParametersGenerator;import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;import org.bouncycastle.crypto.params.KeyParameter;import org.bouncycastle.jce.PKCS10CertificationRequest;import org.bouncycastle.util.encoders.Base64;import org.bouncycastle.util.encoders.Hex;import org.bouncycastle.x509.X509AttributeCertificate;import org.bouncycastle.x509.X509V2AttributeCertificate;/** * Class for reading OpenSSL PEM encoded streams containing * X509 certificates, PKCS8 encoded keys and PKCS7 objects. * <p> * In the case of PKCS7 objects the reader will return a CMS ContentInfo object. Keys and * Certificates will be returned using the appropriate java.security type. */public class PEMReader extends BufferedReader{ private PasswordFinder pFinder; private String provider; /** * Create a new PEMReader * * @param reader the Reader */ public PEMReader( Reader reader) { this(reader, null, "BC"); } /** * Create a new PEMReader with a password finder * * @param reader the Reader * @param pFinder the password finder */ public PEMReader( Reader reader, PasswordFinder pFinder) { this(reader, pFinder, "BC"); } /** * Create a new PEMReader with a password finder * * @param reader the Reader * @param pFinder the password finder * @param provider the cryptography provider to use */ public PEMReader( Reader reader, PasswordFinder pFinder, String provider) { super(reader); this.pFinder = pFinder; this.provider = provider; } public Object readObject() throws IOException { String line; while ((line = readLine()) != null) { if (line.indexOf("-----BEGIN PUBLIC KEY") != -1) { return readPublicKey("-----END PUBLIC KEY"); } if (line.indexOf("-----BEGIN RSA PUBLIC KEY") != -1) { return readRSAPublicKey("-----END RSA PUBLIC KEY"); } if (line.indexOf("-----BEGIN CERTIFICATE REQUEST") != -1) { return readCertificateRequest("-----END CERTIFICATE REQUEST"); } if (line.indexOf("-----BEGIN NEW CERTIFICATE REQUEST") != -1) { return readCertificateRequest("-----END NEW CERTIFICATE REQUEST"); } if (line.indexOf("-----BEGIN CERTIFICATE") != -1) { return readCertificate("-----END CERTIFICATE"); } if (line.indexOf("-----BEGIN PKCS7") != -1) { return readPKCS7("-----END PKCS7"); } if (line.indexOf("-----BEGIN X509 CERTIFICATE") != -1) { return readCertificate("-----END X509 CERTIFICATE"); } if (line.indexOf("-----BEGIN ATTRIBUTE CERTIFICATE") != -1) { return readAttributeCertificate("-----END ATTRIBUTE CERTIFICATE"); } else if (line.indexOf("-----BEGIN RSA PRIVATE KEY") != -1) { try { return readKeyPair("RSA", "-----END RSA PRIVATE KEY"); } catch (Exception e) { throw new IOException( "problem creating RSA private key: " + e.toString()); } } else if (line.indexOf("-----BEGIN DSA PRIVATE KEY") != -1) { try { return readKeyPair("DSA", "-----END DSA PRIVATE KEY"); } catch (Exception e) { throw new IOException( "problem creating DSA private key: " + e.toString()); } } } return null; } private byte[] readBytes(String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); while ((line = readLine()) != null) { if (line.indexOf(endMarker) != -1) { break; } buf.append(line.trim()); } if (line == null) { throw new IOException(endMarker + " not found"); } return Base64.decode(buf.toString()); } private PublicKey readRSAPublicKey(String endMarker) throws IOException { ByteArrayInputStream bAIS = new ByteArrayInputStream(readBytes(endMarker)); ASN1InputStream ais = new ASN1InputStream(bAIS); Object asnObject = ais.readObject(); ASN1Sequence sequence = (ASN1Sequence) asnObject; RSAPublicKeyStructure rsaPubStructure = new RSAPublicKeyStructure(sequence); RSAPublicKeySpec keySpec = new RSAPublicKeySpec( rsaPubStructure.getModulus(), rsaPubStructure.getPublicExponent()); try { KeyFactory keyFact = KeyFactory.getInstance("RSA",provider); PublicKey pubKey = keyFact.generatePublic(keySpec); return pubKey; } catch (NoSuchAlgorithmException e) { // ignore } catch (InvalidKeySpecException e) { // ignore } catch (NoSuchProviderException e) { throw new RuntimeException("can't find provider " + provider); } return null; } private PublicKey readPublicKey(String endMarker) throws IOException { KeySpec keySpec = new X509EncodedKeySpec(readBytes(endMarker)); String[] algorithms = { "DSA", "RSA" }; for (int i = 0; i < algorithms.length; i++) { try { KeyFactory keyFact = KeyFactory.getInstance(algorithms[i], provider); PublicKey pubKey = keyFact.generatePublic(keySpec); return pubKey; } catch (NoSuchAlgorithmException e) { // ignore } catch (InvalidKeySpecException e) { // ignore } catch (NoSuchProviderException e) { throw new RuntimeException("can't find provider " + provider); } } return null; } /** * Reads in a X509Certificate. * * @return the X509Certificate * @throws IOException if an I/O error occured */ private X509Certificate readCertificate( String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); while ((line = readLine()) != null) { if (line.indexOf(endMarker) != -1) { break; } buf.append(line.trim()); } if (line == null) { throw new IOException(endMarker + " not found"); } ByteArrayInputStream bIn = new ByteArrayInputStream( Base64.decode(buf.toString())); try { CertificateFactory certFact = CertificateFactory.getInstance("X.509", provider);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?