Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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_Object_Encryption - Fatal编程技术网

有没有一种简单的方法来加密java对象?

有没有一种简单的方法来加密java对象?,java,object,encryption,Java,Object,Encryption,我希望将序列化对象存储到文件中,但我希望对其进行加密。它不需要真正的强加密。我只是想要一些简单的东西(最好是几行代码),这会让其他人加载起来有点困难。我已经调查过SealeObject,但钥匙的问题阻碍了我。理想情况下,我只想传递一个字符串作为加密/解密对象的密钥 有什么建议吗?你应该研究一下。它有一系列的实用函数来简化这个过程 ... BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); textEncryptor.setPa

我希望将序列化对象存储到文件中,但我希望对其进行加密。它不需要真正的强加密。我只是想要一些简单的东西(最好是几行代码),这会让其他人加载起来有点困难。我已经调查过SealeObject,但钥匙的问题阻碍了我。理想情况下,我只想传递一个字符串作为加密/解密对象的密钥

有什么建议吗?

你应该研究一下。它有一系列的实用函数来简化这个过程

...
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(myEncryptionPassword);
...
String myEncryptedText = textEncryptor.encrypt(myText);
...
String plainText = textEncryptor.decrypt(myEncryptedText);
...

使用
CipherOutPutStream
()将对象写入ObjectOutputStream可能是一种简单而好的方法。

您不能使用任何加密库吗?你的基本代码是

Object o=...;
String s= new String();
ObjectOutputStream out = new ObjectOutputStream(StringStream(s));
out.writeObject(o);
然后您只需将
s
传递到您想要的任何加密系统中

< >我忘记了 StristSuth是C++的东西,不是java的。但是您基本上可以做同样的事情,看一看。

作为一个一般性的答案(因为我现在很少使用Java):

我建议搜索多种加密形式,找到适合您的加密方式,然后查找并修改已经为您的数据编写的模块,或者根据您希望使用的算法编写自己的模块

例如,如果您想使用SHA256并发现已经为您编写了一个模块,只需修改它以使用所需的数据流即可

private ObjectName modifiedSHA256(input stream, ..., ...)
{
    // Modified algorithm
}
然后,您可以随时调用它来编写数据流。然后,模块将保存自己的数据流,然后将其写入文件。

肯定是答案。钥匙有什么问题?

请尝试以下代码:

String fileName = "result.dat"; //some result file

//You may use any combination, but you should use the same for writing and reading
SecretKey key64 = new SecretKeySpec( new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish" );
Cipher cipher = Cipher.getInstance( "Blowfish" );

//Code to write your object to file
cipher.init( Cipher.ENCRYPT_MODE, key64 );
Person person = new Person(); //some object to serialise
SealedObject sealedObject = new SealedObject( person, cipher);
CipherOutputStream cipherOutputStream = new CipherOutputStream( new BufferedOutputStream( new FileOutputStream( fileName ) ), cipher );
ObjectOutputStream outputStream = new ObjectOutputStream( cipherOutputStream );
outputStream.writeObject( sealedObject );
outputStream.close();

//Code to read your object from file
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();
Person person1 = (Person) sealedObject.getObject( cipher );

使用SealeObject和Cipher类对对象进行加密和解密

什么是密封对象?

SealeObject封装原始java对象(它应该实现可序列化)。它使用加密算法来密封对象的序列化内容

什么是密码?

这是一个java类,使用加密算法进行加密和解密

示例代码

下面是示例代码

EncriptThisClass so = new EncriptThisClass();
SealedObject encryptedObject =encryptObject(so);
EncriptThisClass etcObject=decryptObject(encryptedObject);
有关完整代码,请访问以下链接。

谢谢!这让我走上了正确的道路,我现在已经开始工作了。我找到了这一页,这使它变得非常简单。我能问你一个问题吗??如果我不想把它作为文件呢?我只需要字节[],我将使用Base64对其进行编码并将其转换为字符串。有办法吗?我找到了一个方法,但这是一个漫长的过程,不知怎的,我觉得很糟糕。请提供帮助。那么您不应该使用FileOutputStream还有一个简单的问题,我是否必须在使用序列化时使用密封对象?我使用ByteArrayInputStream做这件事,但是我得到了不同的结果。我这里有个问题。要点是使用相同的密码参数。因此,如果您使用一些key64和Blowfish序列化对象,那么在读取对象时应该使用相同的key64和Blowfish类型。