e466. encrypting an object with des.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 36 行

TXT
36
字号
This example demonstrates how to encrypt a serializable object. 
    try {
        // Generate a temporary key. In practice, you would save this key.
        // See also e464 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";
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?