import java.util.Arrays; import cyfer.Hash; import cyfer.BlockCipher; import cyfer.StreamCipher; import cyfer.Pk; import cyfer.KeyEx; class Test { public static boolean test_hash() { char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; Hash h = new cyfer.Hash("MD5"); String msg = "Hello World"; h.update(msg.getBytes()); byte[] md = h.finish(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < md.length; i++) { int val = md[i]; if (val < 0) val += 256; sb.append(hex[val / 16]); sb.append(hex[val % 16]); } return sb.toString().equals("b10a8db164e0754105b7a99be72e3fe5"); } public static boolean test_bcipher() { String key = "Forty-two"; String text = "01234567"; byte[] keydata = key.getBytes(); byte[] plaintext = text.getBytes(); BlockCipher c = new cyfer.BlockCipher("Blowfish", keydata, "ECB", null); byte[] ciphertext = c.encrypt(plaintext); byte[] result = c.decrypt(ciphertext); String res = new String(result); return text.equals(res.substring(0, text.length())); } public static boolean test_scipher() { String key = "Forty-two"; String text = "A quick brown fox jumps over the lazy dog."; byte[] keydata = key.getBytes(); byte[] plaintext = text.getBytes(); StreamCipher enc = new cyfer.StreamCipher("RC4", keydata); StreamCipher dec = new cyfer.StreamCipher("RC4", keydata); byte[] ciphertext = enc.encrypt(plaintext); byte[] result = dec.decrypt(ciphertext); String res = new String(result); return text.equals(res); } public static boolean test_pk() { Pk genkey = new cyfer.Pk("RSA"); genkey.generateKey(512); byte[] privkey = genkey.privateKey(); byte[] pubkey = genkey.publicKey(); String text = "A quick brown fox jumps over the lazy dog."; byte[] plaintext = text.getBytes(); Pk enc = new cyfer.Pk("RSA"); enc.setPublicKey(pubkey); byte[] ciphertext = enc.encrypt(plaintext); Pk dec = new cyfer.Pk("RSA"); dec.setPrivateKey(privkey); byte[] result = dec.decrypt(ciphertext); String res = new String(result); return text.equals(res.substring(0, text.length())); } public static boolean test_keyex() { KeyEx alice = new cyfer.KeyEx("DH"); KeyEx bob = new cyfer.KeyEx("DH"); alice.generateKey(); bob.generateKey(); byte[] a = alice.publicKey(); byte[] b = bob.publicKey(); bob.computeKey(a); alice.computeKey(b); byte[] sa = alice.sharedKey(100); byte[] sb = bob.sharedKey(100); return Arrays.equals(sa, sb); } public static void main(String[] args) { System.out.print("Testing hash .. "); if (test_hash()) System.out.println("passed"); else System.out.println("failed"); System.out.print("Testing block cipher .. "); if (test_bcipher()) System.out.println("passed"); else System.out.println("failed"); System.out.print("Testing stream cipher .. "); if (test_scipher()) System.out.println("passed"); else System.out.println("failed"); System.out.print("Testing public key algorithm .. "); if (test_pk()) System.out.println("passed"); else System.out.println("failed"); System.out.print("Testing key-exchange algorithm .. "); if (test_keyex()) System.out.println("passed"); else System.out.println("failed"); } }