Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 序列化期间的构造函数调用_Java_Serialization_Encryption_Aes_State - Fatal编程技术网

Java 序列化期间的构造函数调用

Java 序列化期间的构造函数调用,java,serialization,encryption,aes,state,Java,Serialization,Encryption,Aes,State,我的代码中有一个KeyChain类,它允许我存储到磁盘并检索加密的凭证列表 在密钥链的构造过程中,我初始化AES密码 为了序列化对象,我首先将凭证列表序列化到一个缓冲区中,然后加密该缓冲区并将其放入原始的OutputObjectStream 为了反序列化它,我尝试将ObjectInputStream读入缓冲区,解密它并从中反序列化我的凭证,但要做到这一点,首先需要构造密码。我不能这样做,因为反序列化不会调用我的构造函数。我该如何扭转局面 钥匙链: private void readObject(

我的代码中有一个
KeyChain
类,它允许我存储到磁盘并检索加密的凭证列表

密钥链
的构造过程中,我初始化AES密码

为了序列化对象,我首先将凭证列表序列化到一个缓冲区中,然后加密该缓冲区并将其放入原始的
OutputObjectStream

为了反序列化它,我尝试将
ObjectInputStream
读入缓冲区,解密它并从中反序列化我的凭证,但要做到这一点,首先需要构造密码。我不能这样做,因为反序列化不会调用我的构造函数。我该如何扭转局面

钥匙链:

private void readObject(ObjectInputStream is) throws IOException {
    byte[] buffer = new byte[512000];

    int readBytes = is.read(buffer);

    byte[] encryptedBytes = new byte[readBytes];
    System.arraycopy(buffer, 0, encryptedBytes, 0, readBytes);

    // Here it crashes and burns because i can't decrypt yet, the ciphers haven't been setup
    byte[] decryptedBytes = decryptBytes(encryptedBytes);

    ByteInputStream stream = new ByteInputStream(decryptedBytes, readBytes);
    ObjectInputStream unsafeInputStream = new ObjectInputStream(stream);
    try {
        Keys = (List<Key>)unsafeInputStream.readObject();
    } catch (ClassNotFoundException ex) {
        // Fail miserably
    }
}

private void writeObject(ObjectOutputStream os) throws IOException {
    ByteOutputStream streamBytes = new ByteOutputStream();
    ObjectOutputStream unsafeOutputStream = new ObjectOutputStream(streamBytes);

    unsafeOutputStream.writeObject(Keys);
    unsafeOutputStream.flush();

    byte[] decryptedBytes = streamBytes.getBytes();

    byte[] encryptedBytes = encryptBytes(decryptedBytes);

    os.write(encryptedBytes);
    os.flush();

    Arrays.fill(decryptedBytes, (byte)0);
    Arrays.fill(encryptedBytes, (byte)0);
}
private void readObject(ObjectInputStream is)引发IOException{
字节[]缓冲区=新字节[512000];
int readBytes=is.read(缓冲区);
byte[]encryptedBytes=新字节[readBytes];
System.arraycopy(缓冲区,0,encryptedBytes,0,readBytes);
//在这里它崩溃和燃烧,因为我还不能解密,密码还没有设置
byte[]decryptedBytes=decryptedBytes(encryptedBytes);
ByteInputStream=新的ByteInputStream(decryptedBytes,readBytes);
ObjectInputStream unsafeInputStream=新ObjectInputStream(流);
试一试{
Keys=(List)unsafeInputStream.readObject();
}捕获(ClassNotFoundException ex){
//惨败
}
}
私有void writeObject(ObjectOutputStream os)引发IOException{
ByteOutputStreamBytes=新的ByteOutputStream();
ObjectOutputStream unsafeOutputStream=新的ObjectOutputStream(streamBytes);
unsafeOutputStream.writeObject(键);
unsafeOutputStream.flush();
byte[]decryptedBytes=streamBytes.getBytes();
byte[]encryptedBytes=encryptedBytes(decryptedBytes);
写操作系统(加密字节);
os.flush();
fill(decryptedBytes,(byte)0);
数组.fill(encryptedBytes,(byte)0);
}

明白了:我不能在readObject中调用
initCryptograhy(char[]password)
,因为我那里没有可用的密码,我不能将其作为参数传递,这是问题的根源。

Java实际上有一个用于加密序列化实例的工具。也许这对你想要实现的目标更有效。我认为您所做的与SealeObject所做的关键区别在于,它在第二阶段进行解密,而不是在最初的反序列化过程中。

哦,天哪,我不敢相信我做了所有这些只是为了发现它已经被其他人做了。。。那么,你能详细说明一下这个“第二阶段”吗?我仍然不知道如何使用这个SealedObject,因为在我的
readObject
中,我没有准备好密码来读取SealedObject。提示?@wingleader-你不能在你的
readObject
中这样做,这就是重点。您在
readObject
完成后(即,这是初始反序列化完成后的第二个阶段)执行第二次调用以获取SealeObject的内容。实际上,我用了一种相当难看的方式解决了这个问题,但感谢您的建议,我将这样做。