0

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

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();

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

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

当我在 gradle 中添加 Bouncy castle 并尝试像上面一样启动时,输出如下 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.

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

4

3 回答 3

3

尝试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依赖项中:

/* 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

于 2019-07-04T09:30:06.420 回答
0

还可以添加BouncyCastleProvider别名bcprov-jdk15on

dependencies {
    // https://mvnrepository.com/artifact/org.bouncycastle
    implementation "org.bouncycastle:bcprov-jdk15on:1.60"
    implementation "org.bouncycastle:bcpkix-jdk15on:1.60"
}
于 2019-07-25T22:11:25.250 回答
0

删除提供者(“BC”)并手动插入 BouncyCastle

Security.removeProvider("BC");
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依赖项中:

/* Bouncy castle */
implementation 'org.bouncycastle:com.springsource.org.bouncycastle.jce:1.46.0'
于 2019-08-09T11:36:08.183 回答