我正在尝试使用 SealedObjects 通过 RMI 连接发送数据
target 是一个 SealedObject,密码是预先初始化的(据我所知是正确的),但是当我运行以下代码时,我得到一个 IOException,并显示消息
invalid stream header: 52FAA4D1
其中 54FAA4D1 每次都是不同的 8 字符十六进制字符串。
有问题的代码是
target.getObject(cipher);
target 和 cipher 都不为空 - 所以我认为问题出在我的密码设置方式上。 key 在客户端和服务器上以完全相同的方式创建,所以我处于停顿状态。
密码是用
创建的Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//Do this to get the same parameters as were used on the client side
cipher.init(Cipher.ENCRYPT_MODE, this.key);
AlgorithmParameters parameters = cipher.getParameters();
//Set to decrypt mode using the same parameters
byte[] iv = parameters.getParameterSpec(IvParameterSpec.class).getIV();
cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(iv));
我试过使用中间部分和不使用中间部分,但似乎都不起作用。
如果我第二次删除 cipher.init() 中的最后一个参数,我会得到一个 InvalidKeyException,如果我完全删除中间部分并只使用 cipher.getParameters.getParameterSpec(IVParameterSpec.class).getIV ();我得到一个 NullPointerException
是的,完全卡住了 - 任何帮助甚至理解问题所在的帮助都将不胜感激。
最佳答案
当您在客户端调用 cipher.init(Cipher.ENCRYPT_MODE, key)
时,会生成一个随机 IV。在服务器上,对 ciper.init
的第一次调用只是生成一个与传入消息不匹配的不同随机 IV,因此 getObject
只会看到废话。
您需要使用与加密消息相同的 IV 来解密每条消息。在这种情况下,您可能只需将 IV 添加到密码中即可。然后将IV读入服务器上的一个字节数组,并用它来初始化解密。
关于java - IOException,使用 SealedObject.getObject() 时无效的流 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9577141/