macpaul has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have a .jar file to be call the in my perl module. Which is used to calculate 3DES/RSA encryption of password.
The .jar file (ChunghwaPost.jar) was archived like this.
url: https://ipost.post.gov.tw/web/ChunghwaPost.jar
file: MainApplet.class
file: MainApplet.html
Directory and files: PaySecure.*

MainApplet.class is the integrated interface of class PaySecure.
MainApplet.html is the demo of how to use MainApplet.class

So here is the java code which embedded in the MainApplet.html.
The order of function execution to encrypt password is GenTDES()->EncryptTDES()->RSAEncryptTDES();.
In other words, the actual calling order which mapped into .jar file is
Generate3DES()->getEncryptPINcode()->getRsaEncrypt3DES();.



Here is the unzipped jar file ChunghwaPost_jarunzipped.zip
The completed MainApplet.html has been included.
------ Java Script of MainApplet.html -------
function GenTDES() // Generate3DES() can be direct accessed in MainApp +let.class { if(document.app.Generate3DES()) { document.form1.GetGenTDESVle.value= "Success"; } else { document.form1.GetGenTDESVle.value=document.app.getErrorString(); } } // getEncryptPINcode() can be direct accessed in MainApplet.class function EncryptTDES() { if(document.app.EncryptPINcode(document.form1.UserPin.value)) { document.form1.GetEncryptTDESVle.value= document.app.getEn +cryptPINcode(); } else { document.form1.GetEncryptTDESVle.value=document.app.getErr +orString(); } } // RsaEncrypt3DES() can be direct accessed in MainApplet.class however + it will call into PaySecure.* classes. function RSAEncryptTDES() { if(document.app.RsaEncrypt3DES()) { document.form1.GetRSAEncryptTDESVle.value= document.app.getRsaEn +crypt3DES(); } else { document.form1.GetRSAEncryptTDESVle.value= document.app.getError +String(); } }

--------------------------------------------------------
Then I wrote "POST_iCrypt.pm" and a perl script "jartest.pl" in perl
to do the same thing which has been done in the html.
--------------- POST_iCrypt.pm --------------------------
package POST_jCRYPT; use strict; use warnings; use Inline::Java; BEGIN { $ENV{CLASSPATH} .= "./ChunghwaPost.jar"; } use Inline Java => 'STUDY', STUDY => [ 'MainApplet' ], AUTOSTUDY => '1', DEBUG => '1'; sub new { my $MainApplet = shift; return POST_jCRYPT->new(@_); } 1;

-----------------------------------------------------

------------------- jartest.pl ----------------------
#!/usr/bin/perl #use strict; use warnings; use Inline::Java; use Data::Dumper; use lib '/home/macpaul/perl'; use POST_jCRYPT; my $pwd ="12345678"; my $usercode ="example1"; my $javaobj = POST_jCRYPT::MainApplet->new(); # ePin print $javaobj->Generate3DES(), "\n"; $javaobj->EncryptPINcode($pwd); my $ePIN = $javaobj->getEncryptPINcode(); print $ePIN, "\n"; $javaobj->RsaEncrypt3DES(); my $eKey = $javaobj->getRsaEncrypt3DES(); print $eKey, "\n";

----------------------------------------------------



However, when I execute jartest.pl, I can get correct result when I called "Generate3DES()" and "getEncryptPINcode()", but fail when "getRsaEncrypt3DES" and I get "method RsaEncrypt3DES in class MainApplet threw exception java.lang.NullPointerException: null".

Any misteak I have made so that I couldn't get the same result when I do this encryption by opening the demo html in browser?

Thanks for help.

Replies are listed 'Best First'.
Re: Problem about NullPointerException when using Inline::Java and Jar file
by tilly (Archbishop) on Dec 24, 2008 at 03:25 UTC
    In the JavaScript they have an if check around EncryptPINcode and only call getRsaEncrypt3DES if that call succeeded. It might help to call getErrorString if it fails and get more details of what is going wrong. But yes, your Perl code does parallel the JavaScript code and so looks like it should work.

    Anyways my guess is that you have a system administration issue more than a coding issue. For instance you may be on a Linux system, Perl is using the crap java implementation that comes out of GNU while your browser is using a better one from Sun. A sanity check is to run the command java -version and see if it says anything about the Free Software Foundation. If it does then go to http://java.com/en/, get a better version of Java, install it somewhere, then follow the instructions in the README to install Inline::Java to use that version and see if the problems go away.

      I've tested with both with Sun's Java 1.5.0_17 and 1.6.0_11, the result were all the same after I rechecked and reconfigured my system. The system was on Linux Debain Etch, perl 5.8.8, Inline::Java was 0.52. So that's why I'm still wondering about if my perl code was not correct since two of the three function call will work.
        Well I did find one thinko. Your module is named POST::iCrypt but your script uses POST_jCRYPT. Perhaps you have both on your system and are looking at the wrong one? Probably not, but it is worth asking.

        Assuming that is not the problem, all that I can really say is that your code looks reasonable to me.

        I tried installing Inline::Java on my machine. It failed to pass its test suite. I tried running from the partially installed module but it did not find the MainApplet. I didn't try that hard to debug the issue. (I messed with the CLASSPATH a bit then decided I was putting too much work into it.)

        At this point I would suggest asking again. You could ask again here, or try emailing inline-subscribe@perl.org to subscribe to inline@perl.org and ask your question there. I would suggest making your question short to attract more responders. Something like this:

        I am trying to call Java from Perl using Inline::Java and am having trouble. In this zip file there is a third party Java library MainApplet.class, there is an example where it is called from JavaScript in MainApplet.html and my attempt to call it from Perl in test.pl. The JavaScript version works but my Perl does not.

        Can anyone else replicate this problem on their systems or give me an idea of how to fix it?

        That you should at least get someone else to try to download and run it to verify the bug.
Re: Problem about NullPointerException when using Inline::Java and Jar file
by Anonymous Monk on Dec 23, 2008 at 09:48 UTC
    I get "method RsaEncrypt3DES in class MainApplet threw exception java.lang.NullPointerException: null".
    This means your JAVA has error. Debug your own java.
      However the function of demo page MainApplet.html worked. So I think the problem should be the method of Inline::Java which I used to glue Java code inside Jar file between my POST_iCRYPT.pm.

      Here is the unzipped jar file ChunghwaPost_jarunzipped.zip
      The completed MainApplet.html has been included.
Re: Problem about NullPointerException when using Inline::Java and Jar file
by Anonymous Monk on Dec 30, 2008 at 00:45 UTC
    The method RsaEncrypt3DES threw a NullPointerException. This error has nothing to do with Inline::Java. You need to figure out what the problem is. Maybe it has something to do with the fact that you are not executing inside a real browser?