Encrypt complete object with triple des

ⅰ亾dé卋堺 提交于 2019-12-04 20:43:42

There is a Java-class called SealedObject (doc) which exactly does what you want to achieve.

This class enables a programmer to create an object and protect its confidentiality with a cryptographic algorithm.

There is only one restriction for the Object to encrypt, it must be Serializable.

MyObject myObj = new MyObject(); // must be serializable

Cipher cipher;
/* initialize fully with IV, key and Cipher.ENCRYPT_MODE */

/* encrypt `myObj` */
SealedObject sealedObj = new SealedObject(myObj, cipher);

/* decrypt `sealedObj` */
MyObjct decryptedObj = (MyObject) sealedObj.get(key); // `key` = encryption-key

Basically this class does the serialization with ObjectOutputStream and ByteArrayOutputStream for you and automatically tracks the algorithm used for encryption.

You can encrypt bytes. Text is bytes, you can serialize a Java object to bytes, so technically it's possible (for example with an ObjectOutputStream connected to a ByteArrayOutputStream).

However it sounds strange, why do you think you need to encrypt an object, instead of the essential data inside an object?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!