Java DES desEncrypt(byte[] data, String encryptKey)

Here you can find the source of desEncrypt(byte[] data, String encryptKey)

Description

des Encrypt

License

Apache License

Declaration

public static byte[] desEncrypt(byte[] data, String encryptKey) 

Method Source Code


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

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

import java.security.SecureRandom;

public class Main {
    public static byte[] desEncrypt(byte[] data, String encryptKey) {
        try {/*  w ww  .  j ava2 s . c  om*/
            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(encryptKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key, sr);
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw new RuntimeException("Encrypt error");
        }
    }
}

Related

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