ECDH KDF java_java-ECDH使用Android KeyStore生成的私钥

我正在尝试使用由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: cannot identify EC private key: java.security.InvalidKeyException: no encoding for EC private key

我使用以下命令成功生成了“后端”公钥/私钥对:

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中.在这种情况下,该密钥最初将位于所谓的“正常世界”中,但是您现在可以做到最好.来源:https://www.icode9.com/content-1-555751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值