decrypt DES with decrypt Key - Android java.security

Android examples for java.security:DES

Description

decrypt DES with decrypt 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 String decryptDES(byte[] encryptedData, String decryptKey)
            throws Exception {
        byte[] iv = decryptKey.getBytes();

        IvParameterSpec zeroIv = new IvParameterSpec(iv);

        SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes("UTF-8"),
                "DES");

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

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

        byte decryptedData[] = cipher.doFinal(encryptedData);

        String decryptedString = new String(decryptedData, "UTF-8");

        return decryptedString;

    }//  w  w  w . j a  v a 2s  .  co m
}

Related Tutorials