DES decode encrypt Text and secret Key - Android java.security

Android examples for java.security:DES

Description

DES decode encrypt Text and secret Key

Demo Code


import android.annotation.SuppressLint;
import java.net.URLDecoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main{
    private final static String DES = "DES";
    private final static String DES_TYPE = "DES/CBC/PKCS5Padding";
    private final static String replaceStr = "%2B";
    private final static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
    //from  ww  w . j  a va  2s  .  c o m
    public static String decode(String encryptText, String secretKey)
            throws Exception {
        encryptText = URLDecoder.decode(encryptText, "UTF-8");

        if (encryptText.indexOf(replaceStr) != -1) {
            encryptText = encryptText.replaceAll(replaceStr, "+");
        }

        byte[] byteMi = Base64.decode(encryptText);
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), DES);
        Cipher cipher = Cipher.getInstance(DES_TYPE);
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
        byte decryptedData[] = cipher.doFinal(byteMi);

        return new String(decryptedData);
    }
}

Related Tutorials