DES decrypt with hard coded key - Android java.security

Android examples for java.security:DES

Description

DES decrypt with hard coded key

Demo Code


//package com.java2s;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class Main {
    private static final String CHARSET = "UTF-8";
    private static final String ALGORITHM = "DES";
    private static final String TRANSFORMATION = "DES";
    private static final String KEY = "CSIIYZBK";
    private static SecretKey secretkey = null;

    public static String decrypt(String source) {
        String result = null;/*from   www .j a v a2  s  .c  o  m*/
        byte[] dissect = null;
        try {
            byte[] input = Base64.decode(source.getBytes(CHARSET),
                    Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.DECRYPT_MODE, getKey());
            dissect = cipher.doFinal(input);
            result = new String(dissect, CHARSET);
        } catch (Exception e) {
        }
        return result;
    }

    private static Key getKey() {
        if (secretkey == null) {
            byte[] key = null;
            try {
                key = KEY.getBytes(CHARSET);
                secretkey = new SecretKeySpec(key, ALGORITHM);
            } catch (Exception e) {
            }
        }
        return secretkey;
    }
}

Related Tutorials