Encrypt an object with DES : DES « Security « Java

Encrypt an object with DES

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)));

  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);


Related examples in the same category

1.DES Crypter and Decrypter
2.Decrypt an object with DES
3.Encrypting a String with DES
4.Encrypting an Object with DES
5.Encrypting a File or Stream with DES
6.Triple DES
7.Encrypting with DES Using a Pass Phrase
8.DES Engine
9.DES algorithm
10.Des Encrypter
11.DES Decrypt
12.DES Encrypt