How do I get ECDH keypair in Android 9.0 pie?

房东的猫 提交于 2019-12-24 20:45:50

问题


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

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

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

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


回答1:


Try to add SpongyCastle manually:

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

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"

Make sure the BouncyCastleProvider() was coming from spongycastle:

import org.spongycastle.jce.provider.BouncyCastleProvider




回答2:


One could also add BouncyCastleProvider alias bcprov-jdk15on:

dependencies {
    // https://mvnrepository.com/artifact/org.bouncycastle
    implementation "org.bouncycastle:bcprov-jdk15on:1.60"
    implementation "org.bouncycastle:bcpkix-jdk15on:1.60"
}



回答3:


Remove Provider ("BC") and insert BouncyCastle Manually

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

add this to your build.gradle dependencies:

/* Bouncy castle */
implementation 'org.bouncycastle:com.springsource.org.bouncycastle.jce:1.46.0'


来源:https://stackoverflow.com/questions/56884608/how-do-i-get-ecdh-keypair-in-android-9-0-pie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!