You are on page 1of 4

Signcrypt.

txt /* The following code Signcryptes tha data Algorithms used - MD5,AES,SHA */

import import import import import import import import import

java.util.Scanner; java.math.*; java.util.*; java.security.*; java.io.*; java.util.Random; javax.crypto.*; javax.crypto.spec.*; java.io.*;

public class Signcrypt{

public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); } return strbuf.toString(); } static byte[] encrypted; static byte n[] = new byte[16]; static String k1,k2,parm; static int primeCheck = 1; static BigInteger r,p,g,bigb; static String c,message,msg1,sessionid1,s; public static void main(String[] args) { BigInteger secretKey,bi,xB,yB,temp,temp1,temp2,param,k; BigInteger xA = new BigInteger("2312"); msg1 = args[0]; parm = args[1]; BigInteger yA = new BigInteger(parm); int val,valu,numPriFac=0,q; int tap_a[]; tap_a = new int[5]; String key,cifer; String sk = "2422"; secretKey = new BigInteger(sk); Random num = new SecureRandom(); Page 1

Signcrypt.txt p = BigInteger.probablePrime(16, num); val = p.intValue(); //System.out.println ("value of p is :" +val+"\n"); numPriFac = val-1; // finding the largest prime factor for(int tap = 1; tap <= numPriFac; tap++) { if(numPriFac%tap == 0) { for(int primeTest = 2; primeTest < tap; primeTest++) { if(tap%primeTest == 0) { primeCheck = 1; break; } else { primeCheck = 0; } } if(primeCheck == 0 || tap == 2) { int j=1; tap_a[j] = tap; j++; } } } //finding q q = tap_a[1]; //System.out.print("Value of q is :"+q+ "\n"); BigInteger q_bi = new BigInteger(Integer.toString(q)); // generating the value of g g = q_bi.modPow(secretKey, p); //System.out.println(g+ " "); // Generating the keys using message digest // generating value of x BigInteger x = new BigInteger(16, num); //System.out.println("Value of x ----------->"+x+ "\n"); // computing (yA * x) mod p temp = yA.multiply(x); param = temp.mod(p); //System.out.println(param+ " Value of (yA * x) mod p"); // using md5 to get an hash of (yA * x) mod p String sessionid = param.toString();

byte[] defaultBytes = sessionid.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); Page 2

Signcrypt.txt byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } String foo = messageDigest.toString(); //System.out.println("value of k is : "+hexString.toString()+"\n"); sessionid=hexString+""; n = sessionid.getBytes(); s = new String(n); //System.out.println("value of k is : "+s+"\n"); while(s.length() < 32) { s = "0" + s; } }catch(NoSuchAlgorithmException nsae){ } final int mid = s.length() / 2; k1 = s.substring(0, mid); k2 = s.substring(mid); //System.out.println("value of k1 is : "+k1 +"and k2 is" + k2+"\n"); try{ byte[] raw = k1.getBytes(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encrypted = cipher.doFinal(msg1.getBytes()); c = asHex(encrypted); //System.out.println("encrypted string: " + c); /*Decryption Code cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] original = cipher.doFinal(encrypted); String originalString = new String(original); System.out.println("Original string: " + originalString + " " + asHex(original)); */ } catch(NoSuchAlgorithmException nsae) { } catch (NoSuchPaddingException noSuchPad) { System.out.println(" No Such Padding exists " + noSuchPad); } catch (InvalidKeyException invalidKey) Page 3

Signcrypt.txt { System.out.println(" Invalid Key " + invalidKey); } catch (BadPaddingException badPadding) { System.out.println(" Bad Padding " + badPadding); } catch (IllegalBlockSizeException illegalBlockSize) { System.out.println(" Illegal Block Size " + illegalBlockSize); } // using md5 to get an hash of message using k2

String

sess = k2;

byte[] defaultBytes1 = msg1.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes1); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } String foo1 = messageDigest.toString(); //System.out.println(" "+sess+" <-------------------value of r "+hexString.toString()); sess = hexString+"";

}catch(NoSuchAlgorithmException nsae){ } r = new BigInteger(sess,16); //System.out.println( r+" <------value of r\n"); // computing value of s temp1 = xA.add(r); temp2 = temp1.mod(q_bi); bigb = x.divide(temp2); //System.out.println(bigb+" <--------------value of s\n"); System.out.println(c+k1+k2+r); } }

Page 4

You might also like