Java 解密抛出StreamCorruptedException的加密.ser文件
我正在创建一个.ser文件,其中包含加密字符串及其解密密钥的映射。(我意识到这不是最好的方法,但我需要显示项目的不同加密方法)然后使用以下方法加密序列化的字符串:Java 解密抛出StreamCorruptedException的加密.ser文件,java,encryption,serialization,cryptography,Java,Encryption,Serialization,Cryptography,我正在创建一个.ser文件,其中包含加密字符串及其解密密钥的映射。(我意识到这不是最好的方法,但我需要显示项目的不同加密方法)然后使用以下方法加密序列化的字符串: private void encryptKeysFile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, IOException{ SecretKey key64
private void encryptKeysFile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, IOException{
SecretKey key64 = new SecretKeySpec( new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish" );
Cipher cipher = Cipher.getInstance( "Blowfish" );
cipher.init( Cipher.ENCRYPT_MODE, key64 );
File keysFile = new File(System.getProperty("src"),fileName);
SealedObject sealedObject = new SealedObject(keysFile, cipher);
CipherOutputStream cipherOutputStream = new CipherOutputStream( new BufferedOutputStream( new FileOutputStream(fileName) ), cipher );
ObjectOutputStream outputStream = new ObjectOutputStream( cipherOutputStream );
outputStream.writeObject(sealedObject);
outputStream.close();
}
然后将对象写回驱动器上的文件。
我使用另一种方法读取文件并解密:
private File dencryptKeysFile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, IOException, ClassNotFoundException, BadPaddingException{
SecretKey key64 = new SecretKeySpec( new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish" );
Cipher cipher = Cipher.getInstance( "Blowfish" );
cipher.init( Cipher.DECRYPT_MODE, key64 );
CipherInputStream cipherInputStream = new CipherInputStream(new BufferedInputStream(new FileInputStream(fileName)),cipher);
ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
SealedObject sealedObject = (SealedObject)inputStream.readObject();
inputStream.close();
File keysFile =(File)sealedObject.getObject(cipher);
this.keysFile = keysFile;
return keysFile;
}
运行这些方法时出现以下错误:
java.io.StreamCorruptedException: invalid stream header: E0F0DDB8
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at mainClasses.Encrypter.dencryptKeysFile(Encrypter.java:180)
at mainClasses.Encrypter.main(Encrypter.java:214)
java.io.StreamCorruptedException:无效的流头:E0F0DDB8
位于java.io.ObjectInputStream.readStreamHeader(未知源)
位于java.io.ObjectInputStream。(未知源)
位于mainClasses.Encrypter.dencryptKeysFile(Encrypter.java:180)
位于mainClasses.Encrypter.main(Encrypter.java:214)
在未读取文件时抛出。
java:180=ObjectInputStream inputStream=新ObjectInputStream(cipherInputStream);
您正在使用相同的密码进行密封和加密,这意味着在后密封状态下使用密码进行加密,然后使用另一个密码进行解密和解封,但这次密码当然处于初始状态,而不是解除密封后的状态,它只能在解除密封后达到,这是永远不会发生的
你在这里用的是皮带和背带。您不需要密封和密码流加密。使用其中一种。@SteelToe不,它不是。这一个引用了不同的流头值和完全不同的代码。与这个问题无关。密封的文件可以打开吗?我不知道“密封的文件”是什么意思,但任何文件都可以打开。密封对象是加密的,如果这是您的意思的话。因此,加密方法可能在创建密封对象后结束,并且CipherOutputStream正在尝试加密已加密的对象。这是正确的。这是毫无意义的。这不仅是尝试,而且是成功的,但是由于循环性,结果文件根本无法解密。