decode ECB From Source - Java Security

Java examples for Security:decrypt encrypt String

Description

decode ECB From Source

Demo Code


//package com.java2s;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;

public class Main {
    public static void main(String[] argv) throws Exception {
        String encrypt = "java2s.com";
        System.out.println(decodeECBFromSource(encrypt));
    }//from w  ww . ja va 2  s  .co  m

    public static final String PUBLIC_KEY = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";

    public static String decodeECBFromSource(String encrypt)
            throws Exception {

        byte[] key = new BASE64Decoder().decodeBuffer(PUBLIC_KEY);
        byte[] encryptBytes = new BASE64Decoder().decodeBuffer(encrypt);
        byte[] decodeBytes = ees3DecodeECB(key, encryptBytes);

        return new String(decodeBytes, "UTF-8");
    }

    public static byte[] ees3DecodeECB(byte[] key, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory
                .getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }
}

Related Tutorials