DES Encrypt : DES « Security « Java






DES Encrypt

     
/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.security.codec;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESEncrypt {

  public DESEncrypt(byte desKey[]) {
    this.desKey = desKey;
  }

  public byte[] doEncrypt(byte plainText[]) throws Exception {
    SecureRandom sr = new SecureRandom();
    byte rawKeyData[] = desKey;
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(1, key, sr);
    byte data[] = plainText;
    byte encryptedData[] = cipher.doFinal(data);
    return encryptedData;
  }

  private byte desKey[];
}

   
    
    
    
    
  








Related examples in the same category

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