0

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

Dev Tamil
  • 629
  • 1
  • 9
  • 25
  • 1
    *This method is not working...* That is not a useful description of the problem. – President James K. Polk Jul 04 '19 at 13:07
  • The code works fine for me in Android 9.0. – President James K. Polk Jul 04 '19 at 13:23
  • @JamesKPolk: I can't get Private key when you set your target SDK version to 28 – Dev Tamil Jul 05 '19 at 07:54
  • Ok, but *can't get...* is not specific enough to diagnose the problem. What specifically happens? – President James K. Polk Jul 05 '19 at 12:21
  • Private key variable is available, but there is no private key when generating – Dev Tamil Jul 05 '19 at 12:32
  • What does that mean? What's the value of `privateKey`? – President James K. Polk Jul 05 '19 at 12:41
  • @JamesKPolk this is the key which i got when using "BC" provider, EC Private Key S: 30e3def89f6aca7ab4e1e0e0367bf936955339db03a0c32c63a08293066f9423 EC Public Key X: 1675a6b1c8097f651be6f6a555ab9e5da83f03d3082041ae29111609b98594be Y: ed23f9263c6a1e8892d03a0c33ed9d8bfc5886dfe67fb7947457e3ff43baffca – Dev Tamil Jul 18 '19 at 13:26
  • Yes. That is the private key. – President James K. Polk Jul 18 '19 at 17:06
  • when i Add Bouncy castle manually i am getting different keypair, 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. – Dev Tamil Jul 19 '19 at 04:50
  • 1) I don't understand what you mean by "add Bouncycastle manually", and 2) I still don't understand what the problem is. Every time you generate a private key it will be different. It is strange that you get a keypair NOT from Bouncycastle in the second case, but that will require some looking into including duplicating as close as possible your setup. – President James K. Polk Jul 19 '19 at 11:59
  • @JamesKPolk: 1. Not manually, instead of using in KeypairGenerator instance like this KeyPairGenerator.getInstance("EC", "BC") i tired to add it like Security.insertProviderAt(BouncyCastleProvider(), this doesn't make any difference in generating keypair 2. Problem description : I need ECDH Keypair, above code is working fine when target sdk is 27 whereas in 28 i was not able to get the keypair – Dev Tamil Jul 20 '19 at 04:57

3 Answers3

3

Try adding 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

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
1

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'
Dev Tamil
  • 629
  • 1
  • 9
  • 25
0

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"
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216