Solved

Too slow.What's the problem?

Posted on 2000-04-15
10
1,027 Views
Last Modified: 2008-03-03
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?
0
Comment
Question by:hpchong7
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 2

Author Comment

by:hpchong7
ID: 2719090
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?
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2719304
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;
      }

}
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2719805
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 ?
:)

0
 

Expert Comment

by:Ash_
ID: 2720199
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.
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2721496
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?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Expert Comment

by:Ash_
ID: 2721572
Check out this link

http://patrick.net/jpt/


0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 10 total points
ID: 2727468
There are a number of good articles on JavaWorld about performance. Here are links to a few:

Basics of Java Platform Performance
http://www.javaworld.com/jw-03-2000/jw-03-javaperf_2.html

The importance of getting on the right road to a fast Java
http://www.javaworld.com/javaworld/jw-05-1997/f_jw-05-jo-performance.html

Performance problem or design problem?
http://www.javaworld.com/javaworld/jw-03-2000/jw-03-javaperf_3.html

Performance Programming, Part 1 -- Smart Object Management
http://www.javaworld.com/javaworld/jw-11-1999/f_jw-11-performance.html

Performance Programming, Part 2 -- The cost of Casting
http://www.javaworld.com/javaworld/jw-12-1999/jw-12-performance.html

Performance Programming, Part 3 -- Managing Collections
http://www.javaworld.com/javaworld/jw-02-2000/jw-02-performance.html


You might also try searching the security archives at javasoft
http://archives.java.sun.com/archives/java-security.html

where you will find postings such as this:

--- begin quote ---
> I'm experimenting with generating DH key pairs for use
> with a KeyAgreement. I can generate DH key pairs for
> strengths of 64, 128, and 256, but 512 or higher seems
> to hang my machine.  (I suspect that it's not hanging,
> it's just taking a very long time.)  I have a Pentium 90.

Your machine is not hanging - it does take a long time!

Choosing a strong prime modulus "p" takes most of the time!

You can speed up the key generation by using pre-generated
Diffie-Hellman parameters (e.g., the ones defined by SKIP).

Usually, the Diffie-Hellman parameters are defined by some
central authority (which generated them once only), and are
not generated on a per-connection basis.

Try this, and you will see the difference!
--- end quote ---

Best regards,
Jim Cakalic
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2751949
Adjusted points from 5 to 10
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2751951
Dearjim cakalic, sorry  for grading it so late as I am now busying on my exam and final year project!
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2752351
No problem. Glad you got a satisfactory answer.

Best regards and good luck!
Jim
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now