0

I have a code in android to:

  1. Generate ECDSA key-pair (Public Key and Private Key)

  2. Generate Certificate Signing Request (CSR) from public key

  3. Store Private Key

Then I send CSR to CA server. The CA server generate X.509 certificate.

Now I want to encrypt a string with public key from that X.509 certificate above. And then I will write android code to decrypt encrypted string using stored Private Key.

I have code to encrypt/ decrypt ECIES in android:

Here is my code:

 ///Gen Key
 ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
            try {
                KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA","SC");
                g.initialize(spec, new SecureRandom());
                KeyPair keyPair = g.generateKeyPair();
                privateKey = keyPair.getPrivate();
                publicKey = keyPair.getPublic();
                Toast.makeText(MainActivity.this, "GEN KEY SUCCESS!!", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                e.printStackTrace();
            }
/////Encrypt
String origin = txtOrigin.getText().toString();
            try {
                Cipher c = Cipher.getInstance("ECIES","SC");
                c.init(Cipher.ENCRYPT_MODE,publicKey);
                encodeBytes = c.doFinal(origin.getBytes());
                txtEncrypt.setText(Base64.encodeToString(encodeBytes,Base64.DEFAULT));
                Toast.makeText(MainActivity.this, "ENCRYPT SUCCESS!!", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
 ////Decrypt
 byte[] decodeBytes = null;
            try
            {
                Cipher c = Cipher.getInstance("ECIES","SC");
                c.init(Cipher.DECRYPT_MODE,privateKey);
                decodeBytes = c.doFinal(encodeBytes);
                String deCrypt = new String(decodeBytes,"UTF-8");
                txtDecrypt.setText(deCrypt);
                Toast.makeText(MainActivity.this, "DECRYPT SUCCESS!!", Toast.LENGTH_SHORT).show();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

But I want to encrypt string using python with public key from X.509 certificate on my CA server and decrypt using my code above in android.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Relevant: [Sign CSR from client using CA root certificate in python](https://stackoverflow.com/a/23118628/7414759) – stovfl Sep 19 '17 at 08:07
  • @stovfl: no I want to encrpyt (ECIES) string with pre-exist public key using python –  Sep 19 '17 at 16:16

0 Answers0