// // Crypto.java : Pseudo Public Key cryptography system // JEAN VAUCHER (Feb. 2004) written for ift6802 // - version 1.0 // // API: (all static) // ------------------ // String encode( String T ): converts any String T to sequence of // HEX blocks suitable for encryption // String decode( String HEX ): Converts HEX sequence back to // original string // String crypt( String HEX, int Key ): // Uses KEY to convert HEX sequence into encrypted HEX sequence. // Normally, KEY is either the 'public' or the 'private' Key of a // Keys pair. A second crypt operation using the sister key gives // gives the original HEX sequence // int getSignature( String msg , int K1) // Returns signature for the 'msg' (which can be any text or HEX // sequence. // boolean checkSignature( int sig, String msg , int K2) // // returns TRUE for for a signature obtained with 'getSignature' // if K2 is the sister key to the one used for Signing. // ------------------------------------------------------------------- public class Crypto { static final int M = 2147483647; public static String encode( String t ) // <<<<<<<<<<<<<<<<<< { int i = 0; StringBuffer t2 = new StringBuffer( 6*t.length() ); for (i=0; i< t.length(); i++) { if(i!=0 && i%3==0) t2.append(" "); t2.append( Integer.toHexString( t.charAt(i)) ); } i = i%3; if (i!=0) { while (i++<3) t2.append( "00") ; } return t2.toString(); } public static String decode( String t ) // <<<<<<<<<<<<<<<<< { StringBuffer t2 = new StringBuffer( t.length()/2 ); String mot [] = t.split(" "); for (int i = 0; i>16); char c2 = (char) ((m & 0XFF00)>>8); char c3 = (char) (m & 0XFF); t2.append(c1) ; if (c2!=0) t2.append(c2); if (c3!=0) t2.append(c3); } return t2.toString(); } public static String encrypt( String t1, int key) // <<<<<<<<<<<<< { return crypt( encode(t1), key ); } public static String decrypt( String t, int key) // <<<<<<<<<<<<< { return decode(crypt(t,key)); } public static String cryptInt( int i1, int key) // <<<<<<<<<<<<< { return crypt( encode("" + i1), key ); } public static int decryptInt( String t, int key) // <<<<<<<<<<<<< { String s = decode(crypt(t,key)); return Integer.parseInt( s ); } public static String crypt( String t, int key) // <<<<<<<<<<<<< { String mot [] = t.split(" "); StringBuffer sb = new StringBuffer( 5*t.length()/4 ); for (int i = 0; i