Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class Main {
    public static byte[] TDesDec(byte[] key, byte[] in) throws Exception {

        Key deskey = null;
        byte[] tdesKey = new byte[24];
        if (key.length % 8 != 0)
            return null;

        if (key.length == 8) {
            System.arraycopy(key, 0, tdesKey, 0, 8);
            System.arraycopy(key, 0, tdesKey, 8, 8);
            System.arraycopy(key, 0, tdesKey, 16, 8);
        }

        if (key.length == 16) {
            System.arraycopy(key, 0, tdesKey, 0, 16);
            System.arraycopy(key, 0, tdesKey, 16, 8);
        }

        DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

        cipher.init(Cipher.DECRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(in);

        return bOut;
    }
}