des Encode ECB - Java Security

Java examples for Security:DES

Description

des Encode ECB

Demo Code


//package com.java2s;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] key = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(des3EncodeECB(key,
                data)));//from   w  w  w .  j ava 2  s.  c om
    }

    public static byte[] des3EncodeECB(byte[] key, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory
                .getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }
}

Related Tutorials