ThanhLam112358
ThanhLam112358

Reputation: 916

Store/Retrieve Elliptic Curve Cryptography (ECC) public key and private key

I have to write code to generate the ECC key pair.

Then I use the public key to encrypt string and use the private key to decrypt.

Now I store the key pair by write it to pem file and store on external memory.

It does not seem safe.

So how to store and retrieve these key?

Here is my code to generate key pair and write to pem file:

btnGenKey.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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();
                String state;
                state = Environment.getExternalStorageState();
                if(Environment.MEDIA_MOUNTED.equals(state))
                {
                    File root = Environment.getExternalStorageDirectory();
                    File dir = new File(root.getAbsolutePath()+"/EDCSA1");
                    if(!dir.exists())
                    {
                        dir.mkdir();
                    }
                    File pub = new File(dir,"pub.pem");
                    File prv = new File(dir,"pri.pem");
                    try
                    {
                        FileOutputStream fileOutputStream = new FileOutputStream(pub);
                        StringWriter writer = new StringWriter();
                        PemWriter pemWriter = new PemWriter(writer);
                        pemWriter.writeObject(new PemObject("PUBLIC KEY",publicKey.getEncoded()));
                        pemWriter.flush();
                        pemWriter.close();
                        String publickeyPem = writer.toString();
                        fileOutputStream.write(publickeyPem.getBytes());
                        fileOutputStream.close();
                        Toast.makeText(MainActivity.this, "SAVE PUBLICKEY", Toast.LENGTH_SHORT).show();
                        ///
                        FileOutputStream fileOutputStream2 = new FileOutputStream(prv);
                        StringWriter writer2 = new StringWriter();
                        PemWriter pemWriter2 = new PemWriter(writer2);
                        pemWriter2.writeObject(new PemObject("PRIVATE KEY",privateKey.getEncoded()));
                        pemWriter2.flush();
                        pemWriter2.close();
                        String privatekeyPem = writer2.toString();
                        fileOutputStream2.write(privatekeyPem.getBytes());
                        fileOutputStream2.close();
                        Toast.makeText(MainActivity.this, "SAVE PRIVATE", Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

Upvotes: 4

Views: 2313

Answers (1)

Aniruddha K.M
Aniruddha K.M

Reputation: 7511

I would not suggest the pem file. The best way to store your secret info in android would be in the keystore as its very secure. read more about keystore here.

If you are looking for cipher transformation to apply refer this as this has a list of algorithms and cipher transformations you can use.

Here is a general idea about how you can use all of this.

1) Initialise a keystore, Generate a key pair(public and private key)
2) Encrypt your string and store it in storage(shared preferences, db etc)
3) get the encrypted string from storage and decrypt it for your use.

UPDATE :Examples

Please refer these examples if you want a complete working 1)https://developer.android.com/training/articles/keystore.html 2)http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/

Upvotes: 2

Related Questions