Click here to Skip to main content
14,695,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using ECIES in my code for encryption. The example in the provided website works without problem. However, I need a way to convert the keys (public and private keys) to string, serialize and deserialize them, using any tools. However, after trying gson and a few other techniques, yet I cannot serialize and deserialize the keys. Since there are many objects of 3-rd party classes, it is not easy to change the classes.

The code:

<pre>import android.Manifest;
import android.content.Context;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;

import de.flexiprovider.common.exceptions.ECException;
import de.flexiprovider.common.ies.IESParameterSpec;
import de.flexiprovider.core.FlexiCoreProvider;
import de.flexiprovider.ec.FlexiECProvider;
import de.flexiprovider.ec.parameters.CurveParams;
import de.flexiprovider.ec.parameters.CurveRegistry.BrainpoolP160r1;

import static java.security.Security.addProvider;

public class ExampleECIES {
static Context context;
ExampleECIES(Context ctx){
    context=ctx;
}
private ExampleECIES() {
}
static {
    // register the FlexiECProvider
    addProvider(new FlexiECProvider());
}

public static void main(String[] args) throws Exception {

    addProvider(new FlexiCoreProvider());
    addProvider(new FlexiECProvider());

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES", "FlexiEC");

    CurveParams ecParams = new BrainpoolP160r1();

    kpg.initialize(ecParams, new SecureRandom());
    KeyPair keyPair = kpg.generateKeyPair();
    PublicKey pubKey = keyPair.getPublic();
    PrivateKey privKey = keyPair.getPrivate();

    Gson gson = new Gson();
    String pubKeystr = gson.toJson(pubKey),privKeystr=gson.toJson(privKey);
    Log.e("pubkey->String(Gson)",pubKeystr);
    PublicKey pubKey2 = gson.fromJson(pubKeystr, PublicKey.class);




    // Encrypt

    Cipher cipher = Cipher.getInstance("ECIES", "FlexiEC");

    IESParameterSpec iesParams = new IESParameterSpec("AES128_CBC",
            "HmacSHA1", null, null);

    cipher.init(Cipher.ENCRYPT_MODE, pubKey, iesParams);
    String cleartextFile = context.getExternalFilesDir(null)+"/"+"ECIES-cleartext.txt";
    String ciphertextFile = context.getExternalFilesDir(null)+"/"+"ECIES-ciphertextECIES.txt";



    byte[] block = new byte[64];
    FileInputStream fis = new FileInputStream(cleartextFile);
    FileOutputStream fos = new FileOutputStream(ciphertextFile);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    int i;
    while ((i = fis.read(block)) != -1) {
        cos.write(block, 0, i);
    }
    cos.close();
    // fis.close();

    File testFile1 = new File(cleartextFile);
    Log.e("Location:",cleartextFile);
    Log.e("size of msg:",String.valueOf(testFile1.length()));

    File testFile2 = new File(ciphertextFile);
    Log.e("Location:",ciphertextFile);
    Log.e("size of cipher:",String.valueOf(testFile2.length()));

    // Decrypt




    String cleartextAgainFile = context.getExternalFilesDir(null)+"/"+"ECIES-cleartextAgainECIES.txt";

    cipher.init(Cipher.DECRYPT_MODE, privKey, iesParams);
    fis = new FileInputStream(ciphertextFile);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    fos = new FileOutputStream(cleartextAgainFile);
    byte[] block1 = new byte[64];
    int length=0;
      while ((i= cis.read(block1)) != -1) {
         fos.write(block1, 0, i);
         length+=1;
     }
    fos.close();
}

}

Log:

What I have tried:

<pre>E/value: Permission Granted, Now you can use local drive .
I/Choreographer: Skipped 47 frames!  The application may be doing too much work on its main thread.
E/pubkey->String(Gson): {"mParams":{"E":{"mA":{"mP":    {"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":297190522446607939568481567949428902921613329152}},"mB":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":173245649450172891208247283053495198538671808088}},"mQ":{"bigInt":1332297598440044874827085558802491743757193798159}},"g":{"mA":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":297190522446607939568481567949428902921613329152}},"mB":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":173245649450172891208247283053495198538671808088}},"mX":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":1089473557631435284577962539738532515920566082499}},"mY":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":127912481829969033206777085249718746721365418785}},"mZ":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":1}},"mE":{"mA":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":297190522446607939568481567949428902921613329152}},"mB":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":173245649450172891208247283053495198538671808088}},"mQ":{"bigInt":1332297598440044874827085558802491743757193798159}},"mP":{"bigInt":1332297598440044874827085558802491743757193798159}},"k":1,"oid":{"value_":[1,3,36,3,3,2,8,1,1,1],"explicit_":true,"optional_":false},"q":{"bigInt":1332297598440044874827085558802491743757193798159},"r":{"bigInt":1332297598440044874827085038830181364212942568457}},"mW":{"mA":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":297190522446607939568481567949428902921613329152}},"mB":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":173245649450172891208247283053495198538671808088}},"mX":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":628394263989319164273890624957594403688612269204}},"mY":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":1072026760431506144307858012106945103504499745844}},"mZ":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":1}},"mE":{"mA":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":297190522446607939568481567949428902921613329152}},"mB":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":173245649450172891208247283053495198538671808088}},"mQ":{"bigInt":1332297598440044874827085558802491743757193798159}},"mP":{"bigInt":1332297598440044874827085558802491743757193798159}}}
E/onClick12: Unable to invoke no-args constructor for interface java.security.PublicKey. Register an InstanceCreator with Gson for this type may fix this problem.
E/value: Permission Granted, Now you can use local drive .
Posted
Updated 28-Nov-18 9:24am
Comments
David Crow 27-Nov-18 21:44pm
   
By serialize, are you wanting to translate your object into a format so it can be stored, for example, in a file or some other memory buffer?
Member 10434755 28-Nov-18 14:25pm
   
Yes, I need objects to be represented as a string to be stored and recovered later.

1 solution

After taking a better look into the public key class, I realized that the class itself provides a .getstring() to serialize the public key. Afterwards, I used the following code to convert the string to public key again:

...
 byte[] publicBytes =pubKey.getEncoded();
 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
 KeyFactory keyFactory = KeyFactory.getInstance("ECIES", "FlexiEC");
 PublicKey pubKey2 = keyFactory.generatePublic(keySpec);
 ...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900