简体   繁体   中英

How to extract private key from Keystore in hexadecimal format

KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.Entry entry = keyStore.getEntry("ECKey", null);
PrivateKey privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
PublicKey publicKey = keyStore.getCertificate("ECKey").getPublicKey();

Log.d(TAG, "Private:" + privateKey.getEncoded());

This code returns encoded private key "null". What is the way to extract the key.

There is no way. This is the purpose of AndroidKeyStore. Keys created in it cannot be extracted, which means the key material can't be leaked even to an attacker who compromises the Android OS.

If you want to have a private key you can export, you should create the key outside of AndroidKeyStore, save a copy of the private key, then import it. Keystore will keep secure the imported copy. Be sure not to leak the copy you hold (this is actually pretty hard).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM