Android从String获取PrivateKey

时间:2014-05-22 13:32:31

标签: android rsa private-key

我尝试从字符串加载我的RSA privateKey。我将花费很长时间来解释为什么我将它作为字符串,但它看起来像这样:

  

OpenSSLRSAPrivateCrtKey {模量= a899b24592040d41c6106142f6bef8ab3be90f26ea385bc24d68363ea95667ea911b4b46612fdd40bde82a73368305ed4aa45a727852298bbec557248f5ef0118ab0d070ef0951a1016017e5c08612c895fe048fc5d4bc789849f3df8267bf32d89df6e2e063a3c2e64e252d69de5ceba90f2583338761a8744be7ea4b6e051f,publicExponent = 10001,privateExponent = 9e0adce27c1a5236faabf610286e21799a0240d2d0b19dca08778c89b680a98e671137c1b46e82af229553dd8616d1e27a9a8fb247f974ba6b4c5f008568900dcbada330c1e0277f85ed8996baf8e8fc4e326d68c69f59a5466b38eef9dc287739ad59b634ce540a1d581a1942d9c3210f0c1ff2384e8b5728f507efe1285519,primeP = d84c927f5ceb4ee1d17b1205cc9c5ec1479dce8261e855f43ee2af4024be681c53d3e3ff42d4b5dadee5304fc462448132b7f1fa98a606af1929221b6712af7d,primeQ = c78bdd1b200f0b82fc9e211f652a309efa124dccccac1f05dd1cb656bcd481997ba8206ae2049f5eddaab53e9a800448ffd7d0e44b26bf81efde5fa8cee0e1cb,primeExponentP = b3f03f8f24c5bc81afbb2a6b18a49d4f3d3cd2a734bc0b857d2b278fbd0189aac731e6e25bbe88eae3b2d65605919a5bc0cdb5c83b6522fa577c189ba34bb02 1,primeExponentQ = 5e2a8b91f464052bb028b33acc93c7540e70bad42db60001d5616a4eae579e3d1ed4ad4fa30f49620c3b35c2b0483f3d6fb699b6521c9bfd26840b226f564257,crtCoefficient = 8121b2049bb99d6fca16c9a2cdf29a8adf2edf1228e004487221a011f64e4bf4518d503417fdf4bd2a0d6abb18ede51a0f898a23989528a6eaa2c4986ba17e92,

如果我使用Base64解码它,我会得到"坏的base64",

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

我想这样做(我认为这应该有效),但我明白了:

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

如果我采用数据类型的私钥#34; PrivateKey"并将其用作参数

 new PKCS8EncodedKeySpec(myRealPrivateKey.getEncoded()));

它工作正常。

如果我拿同样的钥匙:

myRealPrivateKey.toString()
它显示我和我在这里发布的完全相同。它也有相同的长度。那么应该可以将此String转换回PrivateKey?!

谢谢你们:)

2 个答案:

答案 0 :(得分:1)

使用Key.getEncoded()表示密钥的字节表示,而不是toString()方法。

答案 1 :(得分:0)

必须使用扩展程序包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;
}