Link to home
Start Free TrialLog in
Avatar of hpchong7
hpchong7

asked on

Too slow.What's the problem?

Dear ,
  Below is my encryption program:
InputStream cfis=new FileInputStream("gateway.cer");//gateway's cert
             CertificateFactory mycf=CertificateFactory.getInstance("X.509");
             X509Certificate cert=(X509Certificate)mycf.generateCertificate(cfis);
             cfis.close();
//extract the public key of the cert for encryption      
 PublicKey gatepubkey=cert.getPublicKey();
             System.out.println("gatewaypublickey get!");
             
             //encrypt to become the digital envelop
             cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", "ABA");
             cipher.init(Cipher.ENCRYPT_MODE,gatepubkey);
             byte[] temp1=prelop.getBytes("UTF8");
             byte[] tempraw1=cipher.doFinal(temp1);
             DEnvelop = encoder.encode(tempraw1);
             System.out.println("DEnvelop:"+DEnvelop);

However,each time when the program arrives the line
 cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", "ABA");
it will stop for nearly 100 seconds and then proceed on.Why so slow?
Avatar of hpchong7
hpchong7

ASKER

Sorry.It should be the line
byte[] tempraw1=cipher.doFinal(temp1);
not
cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding", "ABA");

How can I make it faster?
May be I post the full code here:
import java.lang.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import sun.misc.*;
import javax.crypto.*;

public class SendOIPI{
      public static boolean state=false;
      public static String base64epm;  //Encrypted Payment message
      public static String base64p;    //PI message digest
      public static String DualS;    //Dual signature
      public static String OI="<Ticket>5<Venue>Cultural Center";
      public static String DEnvelop;  //the digital envelop
      
      //public void sendoipi(PrintWriter out,Socket skt)
      public SendOIPI(String gatefile)throws Exception
      {  //get the oi and pi from wtab3 and wtab1 respectively
            String PI="Master,1111-1111-1111-1111,11/2000";
            String prelop;
            
            String base64o,base64c,base642,base643;
            String emessage;
            String alias="test";
            String store="client.store";
            String keypass="hpchong7";//keystore password
            Key key;
            Cipher cipher;
            char[] pwd=new char[keypass.length()];
            keypass.getChars(0,keypass.length(),pwd,0);
     
            //message digest factory
            MessageDigest md=MessageDigest.getInstance("SHA-1");
            BASE64Encoder encoder = new BASE64Encoder();

            //message digest for payment information
            byte[] buffer1=new byte[8192];
            buffer1=PI.getBytes();
            int len1=buffer1.length;
            md.update(buffer1,0,len1);
            byte[] raw1=md.digest();
            base64p=encoder.encode(raw1);
            System.out.println(base64p);

            //message digest for order instruction
            byte[] buffer2=new byte[8192];
            buffer2=OI.getBytes();
            int len2=buffer2.length;
            md.update(buffer2,0,len2);
            byte[] raw2=md.digest();
            base64o=encoder.encode(raw2);
            System.out.println(base64o);

            //concat them
            String PIOI=base64p+base64o;
            System.out.println(PIOI);      
            
            //Hash PIOI
            byte[] buffer3=new byte[8192];
            buffer3=PIOI.getBytes();
            int len3=buffer3.length;
            md.update(buffer3,0,len3);
            byte[] raw3=md.digest();
            base64c=encoder.encode(raw3);
            System.out.println(base64c);

            //encrypt base64c using Alice's private key
            
            //load the keystore and get the private key first
            FileInputStream fisk=new FileInputStream(store);
        KeyStore ks = KeyStore.getInstance("JKS", "SUN");
            ks.load(fisk,pwd);
            PrivateKey priv=(PrivateKey)ks.getKey(alias,pwd);
            System.out.println(priv);
            
            //encrypt
            cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", "ABA");
            cipher.init(Cipher.ENCRYPT_MODE,(Key)priv);//private key of the store
            System.out.println("RSA!");
            fisk.close();
 
            //encrypt the PIOI digest base64c first
            byte[] stringBytesx=base64c.getBytes("UTF8");
            byte[] rawx=cipher.doFinal(stringBytesx);
            //encode it using the base64
            DualS = encoder.encode(rawx);
            System.out.println("Encoded Dual signature:"+DualS);  //dual signature

            //Get the randomly generated symmetric key
            //PI:PI
            //OI message digest:base64o
            //dual signature:DualS
            
            emessage=PI+"***"+DualS+"***"+base64o;  //prepare send to merchant then to payment gateway
      
            try { ObjectInputStream in = new ObjectInputStream(
                    new FileInputStream("ckey.ser"));
                    key=(Key)in.readObject();
                    in.close();
            }
            catch (FileNotFoundException fnfe) {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            generator.init(256);
            //generator.init(new SecureRandom());
            key = generator.generateKey();  //It is the encrypt symmetric key
            //jsut fore speed.Would be removed.
            ObjectOutputStream out = new ObjectOutputStream(
        new FileOutputStream("ckey.ser"));
            out.writeObject(key);
            out.close();
            }
            // encrypt the emessage to become the encrypted payment message
            cipher= Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,key);//key that already generated
            
            byte[] stringBytesy=emessage.getBytes("UTF8");
            byte[] rawy=cipher.doFinal(stringBytesy);
            base64epm = encoder.encode(rawy);
            System.out.println("Encoded-Encrypted Payment Message"+base64epm);
            
            //create the digial envelop(key + PI)
            //IN spec,though,account data,take it as payment information
            //for key,turn to bytes,then base64 encode
            //*******************************
             byte[] keybyte=key.getEncoded();
             String ekeystring=encoder.encode(keybyte);
             System.out.println("ekeystring:"+ekeystring);
             
             prelop=ekeystring+"***"+PI;  //*** to separate key and PI
        
             //encrypt the pre-envelop(prelop) using the public key of gateway
          
             //1.read gateway's cert
             InputStream cfis=new FileInputStream("gateway.cer");//(filename[0]);  //gateway's cert
             CertificateFactory mycf=CertificateFactory.getInstance("X.509");
             X509Certificate cert=(X509Certificate)mycf.generateCertificate(cfis);
             cfis.close();
             PublicKey gatepubkey=cert.getPublicKey();
             System.out.println("gatewaypublickey get!");
             
             //encrypt to become the digital envelop
             cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding", "ABA");
             System.out.println("reach1");
             cipher.init(Cipher.ENCRYPT_MODE,gatepubkey);
             byte[] temp1=prelop.getBytes("UTF8");
             System.out.println("reach2");
             //***********************************
             //The line below if very slow
             byte[] tempraw1=cipher.doFinal(temp1);
             //***********************************
             System.out.println("reach3");
             DEnvelop = encoder.encode(tempraw1);  //base64 encoding of Digital envelop
             System.out.println("DEnvelop:"+DEnvelop);
             
             System.out.println("All things okay!");
            //*******************************
            //send:
            //1.Encrypted message disgest:base64epm
            //2.PI message Digest:base64p
            //3.OI:OI
            //4.Daul signature:DualS
            //5.Digital envelop:DEnvelop
            //6.client cert,send later:client.cer
             System.out.println("EMdigest:"+base64epm);
             System.out.println("PIdigest:"+base64p);
             System.out.println("OI:"+OI);
             System.out.println("Dual Signature:"+DualS);
             System.out.println("Digital Envelop:"+DEnvelop);
             setstate(true);
             return;
            
      }
      
      public static String getEPM()
      { return base64epm;
      }
      
      public static String getPIdigest()
      { return base64p;
      }
      
      public static String getOI()
      { return OI;
      }
      
      public static String getdualS()
      { return DualS;
      }
      
      public static String getDEnvelop()
      { return DEnvelop;
      }
      
      
      public static void setstate(boolean abc)
      {      
            state=abc;
            return;
      }
      
      public static boolean getstate()
      {      
            return state;
      }

}
I don't know anything abt Java Cryptography .I have seen that u r very much working on this / interested ,as u have asked some questions on it ,but why don't u buy any good book like this?

http://www.oreilly.com/catalog/javacrypt/
even if u dont buy that ,download readymade code ,click on Examples ...

hope it helps u out.

I always like to go for O'Reilly Books.

A handy tip go to http://www.oreilly.com/catalog and search for topic and DOWNLOAD FREE Code.

Isn't it wonderful ?
:)

hpchong7,

what's the size of prelop (in bytes)  at "reach2" ?? Also, RSA encryption is always a bit slow(especially if you are using a high no. of bits for the key, like you do in certificates). So it will be a bit slow anyhow.
Dear Mbormann:Thanks for your tips.I always work on java crypto because my final year project requires me to do so.I've bought that book,but still remain many questions.Thanks for your help!

Dear Ash:thanks for your comment.So may be I cannot solve the bottle neck.


BTW,I want to write an article(in my report,about 500 words)why java is so slow.Can anyone give me some useful URL?
Check out this link

http://patrick.net/jpt/


ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adjusted points from 5 to 10
Dearjim cakalic, sorry  for grading it so late as I am now busying on my exam and final year project!
No problem. Glad you got a satisfactory answer.

Best regards and good luck!
Jim