DES decode byte data - Android java.security

Android examples for java.security:DES

Description

DES decode byte data

Demo Code


//package com.java2s;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

public class Main {
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";

    public static byte[] decode(String key, byte[] data) throws Exception {
        try {//from  w  ww .  jav  a2  s .co  m
            DESKeySpec dks = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("DES");
 
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}

Related Tutorials