使用no-args构造函数问题序列化和反序列化对象 [英] Serialize and deserializing objects with no-args constructor problem
本文介绍了使用no-args构造函数问题序列化和反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的代码中使用 ECIES进行加密。提供的网站中的示例没有问题。但是,我需要一种方法将密钥(公钥和私钥)转换为字符串,序列化和反序列化,使用任何工具。但是,在尝试gson和其他一些技术之后,我无法对密钥进行序列化和反序列化。由于第三方派对的对象很多,所以更改课程并不容易。
代码:
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 .
推荐答案
在更好地了解公众之后关键类,我意识到类本身提供了一个.getstring()来序列化公钥。之后,我使用以下代码将字符串再次转换为公钥:
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);
...
这篇关于使用no-args构造函数问题序列化和反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文