使用无参数构造函数对对象进行序列化和反序列化

时间:2018-11-27 21:00:40

标签: java android-studio serialization

我在代码中使用ECIES进行加密。提供的网站中的示例可以正常工作。我需要一种使用任何工具将密钥(公共密钥和私有密钥)转换为字符串,序列化和反序列化的方法。但是,在尝试了gson和其他一些技术之后,我无法对密钥进行序列化和反序列化。由于第三方类别的对象很多,因此更改类别并不容易。

代码:

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();
}

}

日志:

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 .

自从我最近开始使用Java以来​​,我很欣赏一个清晰易懂的解决方案。

编辑: 我通过将类的实例替换为其实现来修复代码,如下所示:

    ...
    Log.e("PublicKey class", String.valueOf(pubKey.getClass()));
    Gson gson = new Gson();
    String pubKeystr = gson.toJson(pubKey),privKeystr=gson.toJson(privKey);
    Log.e("pubkey->String(Gson)",pubKeystr);
    PublicKey pubKey2 = gson.fromJson(pubKeystr, de.flexiprovider.ec.keys.ECPublicKey.class);
    ...

结果:

E/PublicKey class: class de.flexiprovider.ec.keys.ECPublicKey
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":732184415350565563503541898303690594141588218210}},"mY":{"mP":{"bigInt":1332297598440044874827085558802491743757193798159},"mValue":{"bigInt":1069191161364546078005104901313515997036014364970}},"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 class 
de.flexiprovider.ec.parameters.CurveParams. Register an InstanceCreator with 
Gson for this type may fix this problem.

2 个答案:

答案 0 :(得分:1)

错误似乎来自此行:

PublicKey pubKey2 = gson.fromJson(pubKeystr, PublicKey.class);

PublicKey是Java中的一个接口-表示它声明了一组具体对象实现以遵守该接口的方法。特别是,您不能实例化(创建)接口的实例。而是需要指定一个具体的实现。所以...要么:

PublicKey pubKey2 = gson.fromJson(pubKeystr, YourConcretePublicKeyImplementation.class);

或者,您将gson配置为在被告知反序列化PublicKey接口时,告诉它构造“ YourConcretePublicKeyImplementation.class”的实例。

当然,“ YourConcretePublicKeyImplementation.class”在这里只是一个占位符-您需要使用(正确的)具体实现。为此,您首先需要知道序列化PublicKey的实现。

要做到这一点,只需登录(或打印)您先前在代码中创建的pubKey变量的完整类名即可。

答案 1 :(得分:0)

仔细研究了公共密钥类之后,我意识到该类本身提供了一个.getstring()来序列化公共密钥。之后,我使用以下代码再次将字符串转换为公钥:

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