2

I read that there is problem with method KeyChain.getPrivateKey(Context,String) on Android 4.1 although on older versions it works. Does anybody has solution how to get private key on JellyBean?

Saw this Using Android 4.1 Keychain , but somehow could not get to work with my case.

Code that does not work on 4.1, but works great on older versions.

PrivateKey pk;
try {
    pk = KeyChain.getPrivateKey(context,string);
    byte[] pkByte = pk.getEncoded();
    pkBase64 = new String(Base64.encode(pkByte, Base64.NO_WRAP));
}

Edited: Found out that if I do not change certificate alias(default hex-hex-hex) when adding it, then getPrivateKey returns null, but when I change alias to, for example, aaa method returns something, but still not the correct privatekey. (for example, returned private key with primeP =null primeQ =null. On Android 4.0.4 primeP and primeQ are not null values)

1 Answer 1

1

You can't assume a PrivateKey can be encoded. If a PrivateKey returns null for getEncoded(), then it just doesn't support encoding. See the documentation for Key.getEncoded().

In JellyBean, the private keys returned via KeyChain are never in the process. Any operation performed on them are actually done in another process because the actual key matter may be stored in a hardware security module of some sort. You may still call java.security.Signature APIs with this key.

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

1 Comment

getPrivateKey returns different value on JB than on different verions. The main thing I need is to get private key as String value. Is it possible on JB?

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.