Java Algorithm

發表於 六月 12, 2008. 分類: JAVA |

DES Algorithm

import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;

public class DES {
    public final String DesKey = "12345678";  //8 bytes
    public final String iv = "12345678"; //DES,T-DES IV must be 8 bytes long

    public static void main(String[] args) {
        byte[] data = "123456789ABCDEFGhijklmnopq".getBytes();
        DES des = new DES();

        /** *ECB Mode** */
        //encrypt
        byte[] ECBencry = null;
        try {
            ECBencry = des.ECBEncryption(data, des.DesKey.getBytes());
        } catch (Exception e) {
            System.out.println("ECB Mode encrypt error: " + e.getMessage());
        }

        //decrypt
        byte[] ECBdecry = null;
        try {
            ECBdecry = des.ECBDecryption(ECBencry, des.DesKey.getBytes());
        } catch (Exception e) {
            System.out.println("ECB Mode decrypt error: " + e.getMessage());
        }

        System.out.println("ECB Mode decrypt(plaintext): " + new String(ECBdecry));

        /** *CBC Mode** */
        //get IV
        IvParameterSpec spec = new IvParameterSpec(des.iv.getBytes());
        //encrypt
        byte[] CBCencry = null;
        try {
            CBCencry = des.CBCEncryption(data, des.DesKey.getBytes(), spec);
        } catch (Exception e) {
            System.out.println("CBC Mode encrypt error: " + e.getMessage());
        }

        //decrypt
        byte[] CBCdecry = null;
        try {
            CBCdecry = des.CBCDecryption(CBCencry, des.DesKey.getBytes(), spec);
        } catch (Exception e) {
            System.out.println("CBC Mode decrypt error: " + e.getMessage());
        }

        System.out.println("CBC Mode decrypt(plaintext): " + new String(CBCdecry));
    }

    public byte[] ECBEncryption(byte[] plaintext, byte[] rawKeyData)
            throws Exception {
        //getkey
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);

        //encrypt
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] enbyte = cipher.doFinal(plaintext);
        return enbyte;
    }

    public byte[] ECBDecryption(byte[] ciphertext, byte[] rawKeyData)
            throws Exception {
        //getkey
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);

        //encrypt
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] debyte = cipher.doFinal(ciphertext);
        return debyte;
    }

    public byte[] CBCEncryption(byte[] plaintext, byte[] rawKeyData,
            IvParameterSpec spec) throws Exception {
        //getkey
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);

        //encrypt
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] enbyte = cipher.doFinal(plaintext);
        return enbyte;
    }

    public byte[] CBCDecryption(byte[] ciphertext, byte[] rawKeyData,
            IvParameterSpec spec) throws Exception {
        //getkey
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);

        //encrypt
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] debyte = cipher.doFinal(ciphertext);
        return debyte;
    }
}

Make a Comment

發表迴響

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

連結到 %s

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.