Search in sources :

Example 1 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project MSEC by Tencent.

the class Tools method loadPrivKeyFromFile.

public static PrivateKey loadPrivKeyFromFile(String filename) {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    try {
        FileReader fr = new FileReader(filename);
        PEMReader pr = new PEMReader(fr);
        KeyPair key = (KeyPair) (pr.readObject());
        pr.close();
        fr.close();
        return key.getPrivate();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : PEMReader(org.bouncycastle.openssl.PEMReader) FileReader(java.io.FileReader)

Example 2 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project nhin-d by DirectProject.

the class CAPanel method signCSR.

private void signCSR() {
    JFileChooser fc = new JFileChooser();
    fc.setDragEnabled(false);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);
    fc.setDialogTitle("Open Signing Request PEM File");
    int result = fc.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        final File fl = fc.getSelectedFile();
        PEMReader reader = null;
        try {
            reader = new PEMReader(new InputStreamReader(FileUtils.openInputStream(fl)));
            final PKCS10CertificationRequest certReq = (PKCS10CertificationRequest) reader.readObject();
            certReq.verify();
            final X509Certificate signedCert = CertGenerator.createCertFromCSR(certReq, currentCert);
            // validate the certificate 
            signedCert.verify(currentCert.getSignerCert().getPublicKey());
            // write it to a file
            final String addressName = CryptoExtensions.getSubjectAddress(signedCert);
            final File outFile = new File(addressName + ".der");
            FileUtils.writeByteArrayToFile(outFile, signedCert.getEncoded());
            JOptionPane.showMessageDialog(this, "Signing successful.\r\nCertificate saved to " + outFile.getAbsolutePath(), "CSR Sign", JOptionPane.PLAIN_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error signging CSR: " + e.getMessage(), "CSR Sign Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }
}
Also used : PKCS10CertificationRequest(org.bouncycastle.jce.PKCS10CertificationRequest) JFileChooser(javax.swing.JFileChooser) InputStreamReader(java.io.InputStreamReader) PEMReader(org.bouncycastle.openssl.PEMReader) File(java.io.File) X509Certificate(java.security.cert.X509Certificate)

Example 3 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project RPlay by bencall.

the class RTSPResponder method decryptRSA.

/**
	 * Decrypt with RSA priv key
	 * @param array
	 * @return
	 */
public byte[] decryptRSA(byte[] array) {
    try {
        Security.addProvider(new BouncyCastleProvider());
        // La clef RSA
        PEMReader pemReader = new PEMReader(new StringReader(key));
        KeyPair pObj = (KeyPair) pemReader.readObject();
        // Encrypt
        Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPPadding");
        cipher.init(Cipher.DECRYPT_MODE, pObj.getPrivate());
        return cipher.doFinal(array);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PEMReader(org.bouncycastle.openssl.PEMReader) StringReader(java.io.StringReader) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 4 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project RPlay by bencall.

the class RTSPResponder method encryptRSA.

/**
	 * Crypts with private key
	 * @param array	data to encrypt
	 * @return encrypted data
	 */
public byte[] encryptRSA(byte[] array) {
    try {
        Security.addProvider(new BouncyCastleProvider());
        PEMReader pemReader = new PEMReader(new StringReader(key));
        KeyPair pObj = (KeyPair) pemReader.readObject();
        // Encrypt
        Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pObj.getPrivate());
        return cipher.doFinal(array);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PEMReader(org.bouncycastle.openssl.PEMReader) StringReader(java.io.StringReader) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 5 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project MSEC by Tencent.

the class Tools method loadPubKeyFromFile.

public static PublicKey loadPubKeyFromFile(String filename) {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    try {
        FileReader fr = new FileReader(filename);
        PEMReader pr = new PEMReader(fr);
        PublicKey key = (PublicKey) (pr.readObject());
        pr.close();
        fr.close();
        return key;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : PEMReader(org.bouncycastle.openssl.PEMReader) FileReader(java.io.FileReader)

Aggregations

PEMReader (org.bouncycastle.openssl.PEMReader)8 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 X509Certificate (java.security.cert.X509Certificate)3 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 KeyPair (java.security.KeyPair)2 Cipher (javax.crypto.Cipher)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 ASException (com.intel.mountwilson.as.common.ASException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Reader (java.io.Reader)1 UnknownHostException (java.net.UnknownHostException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SignatureException (java.security.SignatureException)1 CertificateException (java.security.cert.CertificateException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1