如何在Android 9.0派中获取ECDH密钥对? [英] How do I get ECDH keypair in Android 9.0 pie?

查看:284
本文介绍了如何在Android 9.0派中获取ECDH密钥对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取ECDH密钥对(公共密钥和私有密钥).此方法在Android 9.0 pie中不起作用,因为已从此版本中删除了安全提供程序"BC","SC".我尝试了以下方法

I want to get ECDH keypair (Public key and Private key). This method is not working in Android 9.0 pie, because Security provider "BC" , "SC" is removed from this version. I tried below method

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "BC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactory kaif = KeyFactory.getInstance("EC", "BC");
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();

以下是我使用 BC" 提供商与波夫代码,EC专用密钥S时得到关键:30e3def89f6aca7ab4e1e0e0367bf936955339db03a0c32c63a08293066f9423 EC公共密钥X:1675a6b1c8097f651be6f6a555ab9e5da83f03d3082041ae29111609b98594be Y:ed23f9263c6a1e8892d03a0c33ed9d8bfc5886dfe67fb7947457e3ff43baffca

Following is the key which i got when using "BC" provider with the bove code, EC Private Key S: 30e3def89f6aca7ab4e1e0e0367bf936955339db03a0c32c63a08293066f9423 EC Public Key X: 1675a6b1c8097f651be6f6a555ab9e5da83f03d3082041ae29111609b98594be Y: ed23f9263c6a1e8892d03a0c33ed9d8bfc5886dfe67fb7947457e3ff43baffca

方法2:Security.insertProviderAt(BouncyCastleProvider(),1);

Method 2: Security.insertProviderAt(BouncyCastleProvider(), 1);

当我在gradle中添加Bouncy城​​堡并尝试如上所述启动时,输出如下privateKey = {OpenSSLECPrivateKey @ 7518}"OpenSSLECPrivateKey {params = {ECDSA-Parameters:(256 bit)\ n}}""publicKey = {OpenSSLECPublicKey @ 7519}公钥:(256位)\ n00000000 04 5c 2c 76 23 09 41 c4 16 e2 99 ea e0 fa ed 16 |.\,v#.A ......... | \ n00000010 52 ca 91 d2 0c fe 7f c4 94 76 54 9a 3c 49 ab a5 | R ........ vT.

我需要以可读格式像上面一样简单,我是否需要进行任何转换以获取字母数字键

When i Add Bouncy castle in gradle and tried initiating like above, the output is follows privateKey = {OpenSSLECPrivateKey@7518} "OpenSSLECPrivateKey{params={ECDSA-Parameters: (256 bit)\n}}" publicKey = {OpenSSLECPublicKey@7519} "Public-Key: (256 bit)\n00000000 04 5c 2c 76 23 09 41 c4 16 e2 99 ea e0 fa ed 16 |.\,v#.A.........|\n00000010 52 ca 91 d2 0c fe 7f c4 94 76 54 9a 3c 49 ab a5 |R........vT.

I need this to be as simple as above in readable format, do i need to do any conversion to get keys in alphanumeric

推荐答案

尝试手动添加SpongyCastle:

Security.insertProviderAt(BouncyCastleProvider(), 1);

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactory kaif = KeyFactory.getInstance("EC");
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();

将此添加到您的build.gradle依赖项:

add this to your build.gradle dependencies:

/* spongy castle */
implementation "com.madgag.spongycastle:core:1.58.0.0"
implementation "com.madgag.spongycastle:prov:1.58.0.0"

确保BouncyCastleProvider()来自spongycastle:

import org.spongycastle.jce.provider.BouncyCastleProvider

这篇关于如何在Android 9.0派中获取ECDH密钥对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆