// Test program for Crypto and Keys classes // For : ift6802 // Jean Vaucher Feb. 2004 // // The classes imitate the Public Key Cryptography Architecture // -------------------------------------------------------------------- // First obtain a pair of random but 'complementary' integer KEYS: // K1 & K2... either can serve as a Public Key and the other // as a private Key // // Keys k = new Keys(); // int K1 = k.getPublicKey(); // int K2 = k.getPrivateKey(); // // To be encrypted, strings must first be converted to sequences of // hexadecimal number blocks (represented as a string). The methods // convert to Hex and back are "Crypto.encode" and "Crypto.decode" // respectively. // // There is only one encryption method: crypt( HexIn, Key) => HexOut // Successive uses with sister keys gives the original Hex sequence. // Typical use: // // String t1 = "bla ....bla"; // String t2 = Crypto.crypt( Crypto.encode( t1 ), K1 ); // .... send t2..... // // One should be able to recover the original test with: // // String t3 = Crypto.decode( Crypto.crypt( t2, K2)) ; // // -------------------------------------------------------------------- // Signing and check signatures is done as follows: // // int sign = Crypto.getSignature( msg, K1 ); // boolean ok = Crypto.checkSignature( sign, msg, K2 ); // // --------------------------------------------------------------------- import java.util.*; public class CryptoTest { public static void main ( String args[]) { String t, tt, t1, t2; Keys k = new Keys(); int k1 = k.getPublicKey(); int k2 = k.getPrivateKey(); t = "Winter is Hell!"; System.out.println( ">>>> " + t ); t1 = Crypto.encode(t); System.out.println( "Encoded: " + t1 ); t2 = Crypto.crypt( t1, k1 ); System.out.println( "Crypted: " + t2 ); t1 = Crypto.crypt( t2, k2 ); System.out.println( "DeCrypted: " + t1 ); tt = Crypto.decode( t1 ); System.out.println( "PlainText: >>" + tt + "<<"); System.out.println(); t = "Winter is Hell!"; System.out.println( ">>>> " + t ); int sign1 = Crypto.getSignature(t, k1); System.out.println( "Signature1: " + sign1 ); if ( Crypto.checkSignature( sign1, t, k2 )) System.out.println( "Signature checks out"); System.out.println(); t = Crypto.encode("Winter is Hell!"); System.out.println( ">>>> " + t ); sign1 = Crypto.getSignature(t, k2); System.out.println( "Signature1: " + sign1 ); if ( Crypto.checkSignature( sign1, t, k1 )) System.out.println( "Signature checks out"); Keys xKey = new Keys(); t = "Now is the time for all good men to come!"; sign1 = Crypto.getSignature(t, xKey.getPrivateKey()); t1 = sign1+"/"+ t; t2 = Crypto.crypt( Crypto.encode(t1), k1 ); System.out.println( "> " + t2 ); String t3 = Crypto.decode( Crypto.crypt(t2, k2 )); System.out.println( ">> " + t3 ); String part [] = t3.split("/"); if ( Crypto.checkSignature( Integer.parseInt(part[0]), part[1], xKey.getPublicKey() )) System.out.println( ">>> OK" ); else System.out.println( ">>> FAIL" ); } }