try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
// Prepare the encrypter
Cipher ecipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
// Seal (encrypt) the object
SealedObject so = new SealedObject(new MySecretClass(), ecipher);
// Get the algorithm used to seal the object
String algoName = so.getAlgorithm(); // DES
// Prepare the decrypter
Cipher dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);
// Unseal (decrypt) the class
MySecretClass o = (MySecretClass)so.getObject(dcipher);
} catch (java.io.IOException e) {
} catch (ClassNotFoundException e) {
} catch (javax.crypto.IllegalBlockSizeException e) {
} catch (javax.crypto.BadPaddingException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
public class MySecretClass implements java.io.Serializable {
String s = "the secret";
}
Encrypting an Object with DES
最新推荐文章于 2020-02-26 17:31:49 发布
本文提供了一个使用DES加密算法进行对象加密和解密的Java示例代码。通过生成临时密钥并利用该密钥对自定义类的对象进行封装和解封,展示了如何在实践中实现数据的安全传输。
2700

被折叠的 条评论
为什么被折叠?



