[英]Using RSAPrivateKey (and not RSAPrivateCrtKey) to save an RSA private key to Android KeyStore
[英]ECDH using Android KeyStore generated private key
我正在嘗試使用Android KeyStore Provider生成的私有在Android中實現ECDH。
public byte[] ecdh(PublicKey otherPubKey) throws Exception {
try {
ECPublicKey ecPubKey = (ECPublicKey) otherPubKey;
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH");
PrivateKey pk = (PrivateKey) LoadPrivateKey("Backend");
keyAgreement.init(pk);
keyAgreement.doPhase(ecPubKey, true);
return (keyAgreement.generateSecret());
}
catch (Exception e)
{
Log.e("failure", e.toString());
return null;
}
}
但是,此異常是在keyAgreement.init(pk)中捕獲的:
E / failure:java.security.InvalidKeyException:無法識別EC私鑰:java.security.InvalidKeyException:沒有EC私鑰的編碼
我使用以下方法成功生成“Backend”公鑰/私鑰對:
public void GenerateNewKeyPair(String alias)
throws Exception {
if (!keyStore.containsAlias(alias)) {
// use the Android keystore
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, ANDROID_KEYSTORE);
keyGen.initialize(
new KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY | KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
.setDigests(KeyProperties.DIGEST_SHA256,
KeyProperties.DIGEST_SHA384,
KeyProperties.DIGEST_SHA512)
.setRandomizedEncryptionRequired(true)
.build());
// generates the keypair
KeyPair keyPair = keyGen.generateKeyPair();
}
}
我使用以下方法加載私鑰:
public PrivateKey LoadPrivateKey(String alias) throws Exception {
PrivateKey key = (PrivateKey) keyStore.getKey(alias, null);
return key;
}
任何人都知道發生了什么,可以幫助我了解如何解決它? 謝謝!
據我所知,通過研究和反復試驗,目前尚未得到支持。
我相信你能做的最好的事情就是在AndroidKeyStore外部生成一個EC密鑰對的公鑰, 並使用存儲在AndroidKeyStore中的EC密鑰對。 然后,您可以使用您的簽名密鑰證書將此簽名的公鑰發送給另一方,生成共享密鑰(在AndroidKeyStore之外),然后在生成的密鑰上存儲使用KDF派生的SecretKey。 我建議使用這個非AndroidKeyStore生成的密鑰對一次(因此僅用於導出秘密)並在必要時重復此過程以重新密鑰。
編輯:當我說'存儲SecretKey'時,我的意思是AndroidKeyStore。 在這種情況下,這個密鑰最初會出現在所謂的“正常世界”中,但它是你現在能做的最好的。
API級別23支持ECDH。請參閱Android Keystore System上的android文檔
此鏈接中也提供示例代碼..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.