// Copyright 1997 The Open Group Research Institute.  All rights reserved.

// test of des.java

import krb5.lib.crypto.des;

public class destest {

	public static final String[] hex_digit = {
		"0", "1", "2", "3", "4", "5", "6", "7",
		"8", "9", "a", "b", "c", "d", "e", "f"
	};

	public static String byte2hexString(byte x) {
		String s = "";
		for (int i = 0; i < 2; i++) {
			s = hex_digit[((((int)x) & 0xff) & (15 << (i * 4))) >>> (i * 4)] + s;
		}
		return s;
	}

	public static String int2hexString(int x) {
		String s = "";
		for (int i = 0; i < 8; i++) {
			s = hex_digit[(x & (15 << (i * 4))) >>> (i * 4)] + s;
		}
		return s;
	}

	public static String int2binString(int x) {
		String s = "";
		for (int i = 0; i < 32; i++) {
			if ((i > 0) && (i % 4 == 0)) s = " " + s;
			s = hex_digit[(x & (1 << i)) >>> i] + s;
		}
		return s;
	}

	public static String long2hexString(long x) {
		String s = "";
		for (int i = 0; i < 16; i++) {
			s = hex_digit[(int)((x & (15L << (i * 4))) >>> (i * 4))] + s;
		}
		return s;
	}

	public static String long2binString(long x) {
		String s = "";
		for (int i = 0; i < 64; i++) {
			if ((i > 0) && (i % 4 == 0)) s = " " + s;
			s = hex_digit[(int)((x & (1L << i)) >>> i)] + s;
		}
		return s;
	}

	public static String byte2hexString(byte[] input) {
		return byte2hexString(input, 0, input.length);
	}

	public static String byte2hexString(byte[] input, int offset) {
		return byte2hexString(input, offset, input.length);
	}

	public static String byte2hexString(byte[] input, int offset, int length) {
		String result = "";
		for (int i = 0; i < length; i++) {
			if (i + offset < input.length) {
				result += byte2hexString(input[i + offset]);
			}
		}
		return result;
	}

	public static void main(String args[]) {
		long x, y, z;
		long[] key_sched = new long[16];
		x = 0x0123456789abcdefL;
		y = 0x0123456789abcde7L;
		//y = 0xc95744256a5ed31dL;
		System.out.println("key:    " + long2hexString(x));
		System.out.println("key:    " + Long.toHexString(x));
		System.out.println("data:   " + long2hexString(y));
		des.des_set_key(x, key_sched);
		z = des.des_encrypt(y, key_sched, true);
		System.out.println("cipher: " + long2hexString(z));
		System.out.println("good:   c95744256a5ed31d");

		System.out.println();
		
		byte[] x1 = {
			(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
			(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef
		};
		//byte[] y1 = {
		//	(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
		//	(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xe7
		//};
		byte[] y1 = {
			(byte)0xc9, (byte)0x57, (byte)0x44, (byte)0x25,
			(byte)0x6a, (byte)0x5e, (byte)0xd3, (byte)0x1d
		};
		byte[] z1 = new byte[8];
		System.out.println("x1=" + byte2hexString(x1));
		System.out.println("y1=" + byte2hexString(y1));
		des.des_set_key(des.octet2long(x1), key_sched);
		des.ecb_encrypt(y1, z1, key_sched, false);
		System.out.println("z1=" + byte2hexString(z1));

		System.out.println();
		
		byte[] x2 = {
			(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
			(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef
		};
		byte[] y2 = {
			(byte)0x4e, (byte)0x6f, (byte)0x77, (byte)0x20,
			(byte)0x69, (byte)0x73, (byte)0x20, (byte)0x74,
			(byte)0x68, (byte)0x65, (byte)0x20, (byte)0x74,
			(byte)0x69, (byte)0x6d, (byte)0x65, (byte)0x20,
			(byte)0x66, (byte)0x6f, (byte)0x72, (byte)0x20,
			(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x20
		};
		byte[] y3 = {
			(byte)0x6f, (byte)0x77, (byte)0x20,
			(byte)0x69, (byte)0x73, (byte)0x20, (byte)0x74,
			(byte)0x68, (byte)0x65, (byte)0x20, (byte)0x74,
			(byte)0x69, (byte)0x6d, (byte)0x65, (byte)0x20,
			(byte)0x66, (byte)0x6f, (byte)0x72, (byte)0x20,
			(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x20, (byte)0x00
		};
		byte[] y4 = {
			(byte)0x6f, (byte)0x77, (byte)0x20,
			(byte)0x69, (byte)0x73, (byte)0x20, (byte)0x74,
			(byte)0x68, (byte)0x65, (byte)0x20, (byte)0x74,
			(byte)0x69, (byte)0x6d, (byte)0x65, (byte)0x20,
			(byte)0x66, (byte)0x6f, (byte)0x72, (byte)0x20,
			(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x20
		};
		byte[] y5 = {
			(byte)0x37, (byte)0x36, (byte)0x35, (byte)0x34,
			(byte)0x33, (byte)0x32, (byte)0x31, (byte)0x20,
			(byte)0x4e, (byte)0x6f, (byte)0x77, (byte)0x20,
			(byte)0x69, (byte)0x73, (byte)0x20, (byte)0x74,
			(byte)0x68, (byte)0x65, (byte)0x20, (byte)0x74,
			(byte)0x69, (byte)0x6d, (byte)0x65, (byte)0x20,
			(byte)0x66, (byte)0x6f, (byte)0x72, (byte)0x20
		};
		byte[] z2 = new byte[24];
		long ivec = 0x1234567890abcdefL;
		System.out.println("x2=" + byte2hexString(x2));
		System.out.println("y2=" + byte2hexString(y2));
		System.out.println("ivec=" + long2hexString(ivec));
		des.des_set_key(des.octet2long(x2), key_sched);
		des.cbc_encrypt(y2, 1, z2, 0, 23, key_sched, ivec, true);
		System.out.println("z2=" + byte2hexString(z2));
		des.cbc_encrypt(y3, z2, key_sched, ivec, true);
		System.out.println("z2=" + byte2hexString(z2));
		des.cbc_encrypt(y4, z2, key_sched, ivec, true);
		System.out.println("z2=" + byte2hexString(z2));
		z = des.cbc_cksum(y5, key_sched, ivec);
		System.out.println("cbc_cksum=" + long2hexString(z));

		System.out.println();

		byte[] y6 = des.string2byte("Now is the time for all ");
		des.cbc_encrypt(y6, z2, key_sched, ivec, true);
		System.out.println("z2=" + byte2hexString(z2));
		byte[] y7 = des.string2byte("7654321 Now is the time for ");
		z = des.cbc_cksum(y7, key_sched, ivec);
		System.out.println("cbc_cksum=" + long2hexString(z));

		System.out.println();

		z = des.string_to_key("somewha0cell.sanfilip.osf.orgsanfilip");
		System.out.println("str_to_key=" + long2hexString(z));
	}

}


