encrypt DES with encrypt Key - Android java.security

Android examples for java.security:DES

Description

encrypt DES with encrypt Key

Demo Code


//package com.java2s;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static byte[] encryptDES(String encryptString, String encryptKey)
            throws Exception {
        byte[] iv = encryptKey.getBytes();

        IvParameterSpec zeroIv = new IvParameterSpec(iv);

        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");

        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

        byte[] encryptedData = cipher.doFinal(encryptString
                .getBytes("UTF-8"));

        return encryptedData;

    }/*from  ww w.j  a v  a2 s  .  c om*/
}

Related Tutorials