1

I tried to load my RSA privateKey from a string. I will take too long to explain why I have it as string, but it looks like this:

OpenSSLRSAPrivateCrtKey{modulus=a899b24592040d41c6106142f6bef8ab3be90f26ea385bc24d68363ea95667ea911b4b46612fdd40bde82a73368305ed4aa45a727852298bbec557248f5ef0118ab0d070ef0951a1016017e5c08612c895fe048fc5d4bc789849f3df8267bf32d89df6e2e063a3c2e64e252d69de5ceba90f2583338761a8744be7ea4b6e051f,publicExponent=10001,privateExponent=9e0adce27c1a5236faabf610286e21799a0240d2d0b19dca08778c89b680a98e671137c1b46e82af229553dd8616d1e27a9a8fb247f974ba6b4c5f008568900dcbada330c1e0277f85ed8996baf8e8fc4e326d68c69f59a5466b38eef9dc287739ad59b634ce540a1d581a1942d9c3210f0c1ff2384e8b5728f507efe1285519,primeP=d84c927f5ceb4ee1d17b1205cc9c5ec1479dce8261e855f43ee2af4024be681c53d3e3ff42d4b5dadee5304fc462448132b7f1fa98a606af1929221b6712af7d,primeQ=c78bdd1b200f0b82fc9e211f652a309efa124dccccac1f05dd1cb656bcd481997ba8206ae2049f5eddaab53e9a800448ffd7d0e44b26bf81efde5fa8cee0e1cb,primeExponentP=b3f03f8f24c5bc81afbb2a6b18a49d4f3d3cd2a734bc0b857d2b278fbd0189aac731e6e25bbe88eae3b2d65605919a5bc0cdb5c83b6522fa577c189ba34bb021,primeExponentQ=5e2a8b91f464052bb028b33acc93c7540e70bad42db60001d5616a4eae579e3d1ed4ad4fa30f49620c3b35c2b0483f3d6fb699b6521c9bfd26840b226f564257,crtCoefficient=8121b2049bb99d6fca16c9a2cdf29a8adf2edf1228e004487221a011f64e4bf4518d503417fdf4bd2a0d6abb18ede51a0f898a23989528a6eaa2c4986ba17e92,

If I use Base64 to decode it, I get "bad base64",

PrivateKey privateKey=  KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyAsString.getBytes()));

I want to do it this way(I think this should work), but I get:

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag.

If I take a PrivateKey of the datatype "PrivateKey" and use this as Argument

 new PKCS8EncodedKeySpec(myRealPrivateKey.getEncoded()));

it works fine.

If I take the same key:

myRealPrivateKey.toString()

it shows me EXACTLY the same like I posted here. It has also the same length. So it should be possible to convert this String back to PrivateKey?!

Thank you guys :)

2
  • 4
    I hope you don't use that key since it is no longer private ;-) Commented May 22, 2014 at 13:39
  • It uses an nonstandard encoding that is obviously not base64 but rather ascii hex. You will have to write your own parser but it shouldn't be too hard as the structure is obvious. You can use the parsed integers to create a RSAPrivateCrtKeySpec Commented May 24, 2014 at 18:38

2 Answers 2

1

Use Key.getEncoded() for byte representation of your Key, and NOT the toString() method.

Sign up to request clarification or add additional context in comments.

Comments

0

Must use an extend package http://search.maven.org/#search|ga|1|g:com.madgag.spongycastle AND a:core

public static PrivateKey getRsaPrivateKey(Context context, String privateKeyString) {
    String privateKeyString = privateKeyString.replaceAll(TAG_RSA_PRIVATE_KEY_BEGIN, "").replaceAll(TAG_RSA_PRIVATE_KEY_END, "");

    byte[] encodedPrivateKey = Base64.decode(privateKeyString.getBytes(), 0);

    try {
        ASN1Sequence primitive = (ASN1Sequence) ASN1Sequence.fromByteArray(encodedPrivateKey);
        Enumeration<ASN1Integer> e = (Enumeration<ASN1Integer>) primitive.getObjects();

        e.nextElement().getValue();
        BigInteger modulus = e.nextElement().getValue();
        e.nextElement().getValue();
        BigInteger privateExponent = e.nextElement().getValue();

        RSAPrivateKeySpec spec = new RSAPrivateKeySpec(modulus, privateExponent);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(spec);
    } catch (Exception e) {
        Flog.e(e);
    }
    return null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.