Encrypt an object with DES : DES Data Encryption Standard « Security « Java Tutorial






import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

public class Main {
  private static void writeToFile(String filename, Object object) throws Exception {
   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(filename)));
    oos.writeObject(object);
    oos.flush();
    oos.close();
  }

  public static void main(String[] args) throws Exception {
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();
    writeToFile("secretkey.dat", key);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher);
    writeToFile("sealed.dat", sealedObject);
  }
}








36.12.DES Data Encryption Standard
36.12.1.CBC using DES with an IV based on a nonce. In this case a hypothetical message number.
36.12.2.Basic symmetric encryption example with CTR using DES
36.12.3.Decrypt an object with DES
36.12.4.Encrypt an object with DES
36.12.5.Encrypting with DES Using a Pass Phrase
36.12.6.Encrypting a String with DES
36.12.7.Encrypting a File or Stream with DES
36.12.8.Message without tampering with MAC (DES), encryption AES in CTR mode