2

I have a private key as string. It looks like this:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIE6zAd...
cRlpnCaW...
VF73XRYo...
...
-----END ENCRYPTED PRIVATE KEY-----

If i want to decrypt it in windows i write the following in cmd:

openssl rsa -text -in [File]

[File] is a .pem file where the encrypted private key is written in. Then the cmd is asking me for a passphrase. After i type in the passphrase i get another .pem file with this inside:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBA...
sy+CJEnLX...
lbp4Uji0F...
...
-----END RSA PRIVATE KEY-----

This private key and a cerificate i need to send data to a server.

Now i want to send data from a android device to this server. The certificate, the key and the passphrase i get as a JSON from a GET request from the server and it look like this:

{
  "authentications":[
    {
      "type":"clientCertificate",
      "secret":"abc*****",
      "pem":"-----BEGIN ENCRYPTED PRIVATE KEY-----
            MIIE6zAd...
            cRlpnCaW...
            VF73XRYo...
            ...
            -----END ENCRYPTED PRIVATE KEY-----
            -----BEGIN CERTIFICATE-----
            MIIEJTC...
            CEGA1UE...
            ...
            -----END CERTIFICATE-----"
    }
  ]
}

Now i have created the certificate:

CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X.509");
String obstring = params[0].toString();
String substringcert = obstring.substring(obstring.indexOf("-----BEGIN CERTIFICATE-----"), obstring.indexOf("-----END CERTIFICATE-----") + 25);
String certstring = substringcert.replaceAll("\\\\" + "n", "\n");
InputStream is = new ByteArrayInputStream(certstring.getBytes());
Certificate cert = null;
cert = cf.generateCertificate(is);

And i think that worked. But now i want to create/generate the key from string. I have already tried this. But there was a error:

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0000b9:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG

So i google for that and found that post. The prolem ist, android studio doesnt know DataTypeConverter and i tried it without -----BEGIN ENCRYPTED PRIVATE KEY----- and -----END ENCRYPTED PRIVATE KEY-----. Nothing worked.

If it is possible i want to do that without importing other libraries.

In the end i just want to do on android, what i could do in cmd with

openssl rsa -text -in [File]

Any ideas? Need some more information of my problem?

Thanks for your help.

PhilippH
  • 21
  • 5

0 Answers0