1
votes

I want to test the ECC algorithm encrypt and decrypt.

I write code to:

  • Generate key pair (publickey and privatekey) ---> write them to file (maybe not secure but I just only test).

  • Then using public key to encrypt my string (Write on Java)

  • Now, I take the encrypted string and try to decrypt it on Android (Using private key)

Here is my code:

  1. Encrypt string in Java:

    /// generate key pair
    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
    try 
    {
        KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA",BouncyCastleProvider.PROVIDER_NAME);
        g.initialize(spec, new SecureRandom());
        KeyPair keyPair = g.generateKeyPair();
        privateKey = keyPair.getPrivate();
        publicKey = keyPair.getPublic();
        System.out.println("PublicKey:"+publicKey+"\n");
        System.out.println("PrivateKey:"+privateKey+"\n");
    
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
     try {
        Cipher c = Cipher.getInstance("ECIES",BouncyCastleProvider.PROVIDER_NAME);
        c.init(Cipher.ENCRYPT_MODE,publicKey);
        encodeBytes = c.doFinal(origin.getBytes());
        String encrypt = Base64.getEncoder().encodeToString(encodeBytes);
        System.out.println(encrypt);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

My output encrypt string like this:

BB9Y4sKPbp28y7+FnpGnLGz5aTjD58GiIcXgjnNC4nXlUiqlHVVPc4K+ovlK5HK+Hz1Qps4ZWH9VdoYvm6VE36aEqvy53lJFnANDVpqAKuDUUaqEUrIQgF/TXFCFO22Il411atDsNhvJC3eRXbREL14duQbF8xjnLjru6WLN6GDYAQ==

And here is my private key

-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgCybVICafKcZiEmGNw+Pj/FfL6GMgDc94/E0zJ/EKgdGgCgYIKoZIzj0DAQehRANCAATRTcV/M0OJE94qlZM7VksMOw35fcuGSxejdgzbyYllWtlIQw9gYhKPZx6t/PVOEOec4DyZq2XwaoNxBxWRboL1
-----END PRIVATE KEY-----
  1. Decrypt string on Android

    encodeBytes = Base64.decode(encrypted_string_above,Base64.DEFAULT);
    try
            {
                KeyFactory factory = KeyFactory.getInstance("ECDSA", "SC");
    
                String state;
                state = Environment.getExternalStorageState();
                File root = Environment.getExternalStorageDirectory();
                File dir = new File(root.getAbsolutePath()+"/disk");
                privateKeyFromFile = generatePrivateKey(factory,dir+"/private.pem");
    
                Cipher c = Cipher.getInstance("ECIES","SC");
                c.init(Cipher.DECRYPT_MODE,privateKeyFromFile);
                decodeBytes = c.doFinal(encodeBytes);
                String deCrypt = new String(decodeBytes,"UTF-8");
    
    
                txtHiden.setText(deCrypt);
                Toast.makeText(activity, deCrypt, Toast.LENGTH_SHORT).show();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
    

But my code not success decrypt, it have a problem:

  10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err: org.spongycastle.jcajce.provider.util.BadBlockException: unable to process block
  10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err:     at org.spongycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(IESCipher.java:476)
  10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err:     at javax.crypto.Cipher.doFinal(Cipher.java:2056)
  10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err:     at com.example.napoleon.luanvana.MessageFragment$3.onClick(MessageFragment.java:156)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.view.View.performClick(View.java:6213)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.widget.TextView.performClick(TextView.java:11074)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.view.View$PerformClick.run(View.java:23645)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.os.Looper.loop(Looper.java:154)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6692)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: Caused by: org.spongycastle.crypto.InvalidCipherTextException: unable to recover ephemeral public key: Sender's public key has invalid point encoding 0x51
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at org.spongycastle.crypto.engines.IESEngine.processBlock(IESEngine.java:409)
  10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at org.spongycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(IESCipher.java:472)
 10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     ... 12 more
 10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: Caused by: java.io.IOException: Sender's public key has invalid point encoding 0x51
 10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at org.spongycastle.crypto.parsers.ECIESPublicKeyParser.readKey(ECIESPublicKeyParser.java:46)
 10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     at org.spongycastle.crypto.engines.IESEngine.processBlock(IESEngine.java:405)
 10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err:     ... 13 more

I think it maybe cause by wrong encode/decode string - byte - Base64

(Enctypt side)String encrypt = Base64.getEncoder().encodeToString(encodeBytes);
(Decrypt side): encodeBytes = Base64.decode(encrypted_string_above,Base64.DEFAULT);

But I do not have much experience in this problem.

1
The statck trace point to an IO Exception : Caused by: java.io.IOException: Sender's public key has invalid point encoding 0x51Roshith
Also you seem to be using ECDSA as the key gen alogorithm. ----KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA",B) -----ECDSA is a digital signature algo where you use the private key to sign and public key to verify.Roshith

1 Answers

1
votes

In the ECIE scheme you should be using ECDH as the alogorithm to generate key pair for encryption/decryption and not ECDSA as the later is a digital signature algorithm.

KeyPairGenerator g = KeyPairGenerator.getInstance("ECDH",BouncyCastleProvider.PROVIDER)