Org.bouncycastle.asn1.DLSequence cannot be attributed to org.bouncycastle.asn1.ASN1Integer

I am trying to use the BouncyCastle classes to encrypt and decrypt a password. I wrote a test program and generated a test key / certificate in PEM format, as well as in DER format. I can read the / cert switch in my program and get the public key and encrypt the value. When I try to configure the decryption of the value, I get the error message "org.bouncycastle.asn1.DLSequence could not be passed to org.bouncycastle.asn1.ASN1Integer" when creating AsymmetricKeyParameter. It seems that when I try to extract data from the certificate by doing cert.getEncoded (), it also pulled out the header values. I tried just reading the file and deleting the BEGIN and END CERTIFCATE lines along with the dash, and I set the same error. I tried using java.security.cert.Certificate as well as the X509 certificate.which uses the code below. Any help would be greatly appreciated.

I can download the key file, this would be useful for you, since this is a test key that I generated on my local machine and will be thrown away as soon as I succeed.

package com.cds.test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.Security;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;

public class RSAEncryptDecrypt {
    public X509Certificate cert = null;
    //
    public void readCertificate() throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        CertificateFactory factory = CertificateFactory.getInstance("X.509", new BouncyCastleProvider());
        InputStream fis = new FileInputStream("/opt/temp/keys/openssl_crt.pem");
        X509Certificate x509Cert = (X509Certificate) factory.generateCertificate(fis);
        this.cert = x509Cert;
        System.out.println("issuer: " + x509Cert.getIssuerX500Principal());
    }
    //
    public String encrypt(String inputData) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //
        System.out.println("public key: " + new String(Base64.encode(cert.getPublicKey().getEncoded())));
        AsymmetricKeyParameter publicKey = PublicKeyFactory.createKey(cert.getPublicKey().getEncoded());
        AsymmetricBlockCipher cipher = new RSAEngine();
        cipher = new org.bouncycastle.crypto.encodings.PKCS1Encoding(cipher);
        cipher.init(true, publicKey);
        //
        byte[] messageBytes = inputData.getBytes();
        byte[] hexEncodedCipher = cipher.processBlock(messageBytes, 0, messageBytes.length);
        //
        return new String(Base64.encode(hexEncodedCipher));
    }
    //
    private String decrypt (String encryptedData) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //
        byte[] certData = cert.getEncoded();
        //certData = Base64.decode(certData);
        AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(cert.getEncoded());
        AsymmetricBlockCipher cipher = new RSAEngine();
        cipher = new org.bouncycastle.crypto.encodings.PKCS1Encoding(cipher);
        cipher.init(false, privateKey);
        //
        byte[] decoded = Base64.decode(encryptedData.getBytes());
        byte[] result = cipher.processBlock(decoded, 0, decoded.length);
        //
        return new String(result);
    }   
    //
    public static void main(String[] args) throws Exception {
        String inputData = "This is the message I am trying to encrypt.";
        String encrypted = null;
        String decrypted = null;
        //
        RSAEncryptDecrypt rsa = new RSAEncryptDecrypt();
        //
        rsa.readCertificate();
        System.out.println("    input: " + inputData);
        encrypted = rsa.encrypt(inputData);
        System.out.println("encrypted: " + encrypted);
        decrypted = rsa.decrypt(encrypted);
        System.out.println("decrypted: " + decrypted);
    }
}
+4
source share
1 answer

The certificate contains only the public key, not the private key. Of course, the public key has a private key associated with it, but it is not stored in the certificate. A certificate is what you distribute to other parties.

You might be working too hard with Microsoft code. I mention Microsoft, as in the .NET code, the certificate class may internally contain an associated private key, which makes for a simplified API.

, , ( PKCS8EncodedKeySpec "RSA" KeyFactory).

- PKCS # 12 Java KeyStore.load.

+2

All Articles