DES3.java 3.32 KB
package com.sitech.ibnms.util;

import java.math.BigInteger;
import java.util.Scanner;

public final class DES3 extends Cipher {
	DES des1 = new DES();
	DES des2 = new DES();
	DES des3 = new DES();

	public synchronized void encrypt(byte[] src, int srcOff, byte[] dest,
			int destOff, int len) {
		des1.encrypt(src, srcOff, dest, destOff, len);
		des2.decrypt(dest, destOff, dest, destOff, len);
		des3.encrypt(dest, destOff, dest, destOff, len);
	}

	public synchronized void decrypt(byte[] src, int srcOff, byte[] dest,
			int destOff, int len) {
		des3.decrypt(src, srcOff, dest, destOff, len);
		des2.encrypt(dest, destOff, dest, destOff, len);
		des1.decrypt(dest, destOff, dest, destOff, len);
	}

	public void setKey(byte[] key) {
		byte[] subKey = new byte[8];
		des1.setKey(key);
		System.arraycopy(key, 8, subKey, 0, 8);
		des2.setKey(subKey);
		System.arraycopy(key, 16, subKey, 0, 8);
		des3.setKey(subKey);
	}

	static byte[] key = { (byte) 0x12, (byte) 0x34, (byte) 0x45, (byte) 0x78,
			(byte) 0x87, (byte) 0x34, (byte) 0x43, (byte) 0x23, (byte) 0x89,
			(byte) 0x55, (byte) 0x01, (byte) 0x77, (byte) 0x87, (byte) 0xef,
			(byte) 0x43, (byte) 0x78, (byte) 0xcd, (byte) 0x65, (byte) 0x9a,
			(byte) 0x21, (byte) 0x12, (byte) 0xab, (byte) 0x56, (byte) 0x78, };

	static public String decrypt(String source) {
		byte[] txt = new byte[24];
		BigInteger t = new BigInteger(source, 16);
		byte[] b = t.toByteArray();

		if (b[0] == 0) {
			System.arraycopy(b, 1, txt, 0, b.length - 1);
		} else {
			System.arraycopy(b, 0, txt, 0, b.length);
		}

		byte[] dec;
		DES3 cipher = new DES3();
		cipher.setKey(key);
		dec = cipher.decrypt(txt);

		byte[] dect = new byte[dec.length];
		int j = 0;
		for (int i = 0; i < dec.length; i++) {
			if (dec[i] > 0) {
				dect[j++] = dec[i];
			}
		}
		String rt = (new String(dect, 0, j));
		return rt;
	}

	static public void displayString(String source) {
		byte[] aa = source.getBytes();
		System.out.print("BEGIN:");
		for (int i = 0; i < aa.length; i++) {
			System.out.print(aa[i]);
			System.out.print(" ");
		}
		System.out.println(":END");
	}

	static public String encrypt(String source) {
		byte[] txt = new byte[24];
		byte[] b = source.getBytes();

		System.arraycopy(b, 0, txt, 0, b.length);

		byte[] enc;
		DES3 cipher = new DES3();
		cipher.setKey(key);

		enc = cipher.encrypt(txt);
		return printHex(enc);
	}

	static String printHex(byte[] buf) {
		byte[] out = new byte[buf.length + 1];
		out[0] = 0;
		System.arraycopy(buf, 0, out, 1, buf.length);
		BigInteger big = new BigInteger(out);
		return big.toString(16);
	}

	static String printHex(int i) {
		BigInteger b = BigInteger.valueOf((long) i + 0x100000000L);
		BigInteger c = BigInteger.valueOf(0x100000000L);
		if (b.compareTo(c) != -1)
			b = b.subtract(c);
		return b.toString(16);
	}

	private static String read(String prompt) {
		Scanner scanner = new Scanner(System.in);
		System.out.print(prompt);
		return scanner.nextLine();
	}

	public static void main(String[] args) {
		System.out.println("******DES3 Util********");
		
		String type = read("类型(1:加密, 2:解密):").trim();
		String str = read("字符串:").trim();
		
		switch (Integer.parseInt(type)) {
		case 1:
			System.out.println(DES3.encrypt(str));
			break;
		case 2:
			System.out.println(DES3.decrypt(str));
			break;
		default:
			break;
		}
		
		System.out.println("******DES3 Util********");
	}
}