encrypt DES ECB - Android java.security

Android examples for java.security:DES

Description

encrypt DES ECB

Demo Code


//package com.java2s;

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

public class Main {
    public static byte[] encryptDESECB(byte[] key, byte[] data) {
        try {/*w  w  w  .  j  a  va2s .  c o  m*/
            DESKeySpec keySpec = new DESKeySpec(key);
            SecretKey secret = SecretKeyFactory.getInstance("DES")
                    .generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, secret);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials