DES encode plain Text and secret Key - Android java.security

Android examples for java.security:DES

Description

DES encode plain 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 };
    /*w ww . j av  a 2  s .  com*/
    public static String encode(String plainText, String secretKey) {
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), DES);
        Cipher cipher;
        byte[] encryptedData = new byte[10];
        try {
            cipher = Cipher.getInstance(DES_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            encryptedData = cipher.doFinal(plainText.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String text = Base64.encode(encryptedData);
        return text.replace("+", replaceStr);
    }
}

Related Tutorials