Java DES desEncrypt(String content, String key)

Here you can find the source of desEncrypt(String content, String key)

Description

des Encrypt

License

Apache License

Declaration

public static byte[] desEncrypt(String content, String key) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.security.SecureRandom;

import javax.crypto.Cipher;

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

public class Main {

    public static byte[] desEncrypt(String content, String key) {
        try {//from   w  w w .  ja v  a2  s  .c o  m
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            byte[] result = cipher.doFinal(content.getBytes());
            return result;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. des3EncodeECB(byte[] key, byte[] data)
  2. desEncrypt(byte[] data, String encryptKey)
  3. desEncrypt(byte[] input, String password)