Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Encrypt in Perl and decrypt in JAVA

by ashok.g (Beadle)
on Jan 05, 2011 at 09:41 UTC ( #880552=perlquestion: print w/replies, xml ) Need Help??

ashok.g has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks,

I got a small requirement where I need to integrate a JAVA application to my perl CGI portal.

The requirement is like I need to encrypt some sample text and pass the same in some URL and that URL is used by some JAVA application where it can get the parameter from that URL, decrypt it and use that value in its application.

So, we came a situation where we need some encryption-decryption algorithm that can be use by PERL/CGI to encrypt and JAVA to decrypt.

Any idea on how this can be done?


Replies are listed 'Best First'.
Re: Encrypt in Perl and decrypt in JAVA
by moritz (Cardinal) on Jan 05, 2011 at 09:53 UTC

    Many encryption methods are standardized, as well as some data interchange formats.

    For example you could use Cypher block chaining with a common standard such as AES, and encode the result with base64, or percent encoding for URLs. Crypt::CBC would be your first choice on the Perl side, and I'm sure that Java also has pretty solid libraries for that.

Re: Encrypt in Perl and decrypt in JAVA
by ELISHEVA (Prior) on Jan 05, 2011 at 09:47 UTC

    Sure you can do it. It isn't a wild idea. Just make sure you use a compatible algorithm on each side. For the Perl/encryption side, you can find support for a variety of encryption algorithms/protocols at CPAN. For Java/decryption side, check the standard libraries or third party vendors.

      We tried with Crypt::CBC. In that I used the  -cipher => 'blowfish' and got some hexa decimal code and passed that value in URL.

      But, that value is not going to be decrypted at the java application. They are just using some blowfish decryption algorithm.
        They are just using some blowfish decryption algorithm.

        Obviously you have to use the same algorithms and keys both in perl and java. That includes the same cypher block mode, the same key and data padding if the are of "wrong" size, and same content-encoding, if any.

        If there's an existing java system which decrypts stuff, you first have to learn exactly what the java side does.

        They are just using some blowfish decryption algorithm.

        Here is an idea, you can use some blowfish encryption algorithm?

Re: Encrypt in Perl and decrypt in JAVA
by elagon (Initiate) on Jan 05, 2011 at 10:58 UTC
    Remember to encode first the whole thing with base64 to be sure nothing get modified by the encoding/decoding in different languages
      you mean to say that encode the string using base64, encrypt the encoded string, send , decrypt the string value and finally decode using base64?
Re: Encrypt in Perl and decrypt in JAVA
by Anonymous Monk on Jan 05, 2011 at 09:54 UTC
    Any idea on how this can be done?

    1. pick an algorithm (say AES)
    2. check to see if there are java/perl libraries for algorithm
    3. install libraries, read synopsis, adapt code, test it
    4. ...
    5. profit
      I have done something similar (php <-> perl).
      I had problems with the binary data for some reason, and I solved my problems converting the data in base64 to pass it from/to php/perl
Re: Encrypt in Perl and decrypt in JAVA
by suaveant (Parson) on Jan 05, 2011 at 14:55 UTC
    I had something where I needed to have another group send me info crypted in the URL, I know they used a basic Java library for ECB Blowfish and the following line encrypted it propery, sorry I can't be more help, but maybe it can get you started.
    Crypt::ECB::decrypt_hex( $key, 'Blowfish', $data, 1 );

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Encrypt in Perl and decrypt in JAVA
by sundialsvc4 (Abbot) on Jan 05, 2011 at 13:37 UTC

    If it were me, I would use an AJAX-call to request the encrypted information.   The JSON encoding system (say...) can handle any sort of data.

    Also, remember that if the information is being handled on the user-side, it really isn’t “secret” at all.   You really can’t keep secrets from the owner of the machine, if he seriously wants to know what they are.   You can “obfuscate” them, but that’s about it.

    You definitely need to use an industry-standard algorithm and it should probably use public-key encryption.   However, the very best place to do any serious encryption, of anything that really needs protection, is in the communications hardware ... i.e. with VPN.   If you know that you are talking through an encrypted channel, you can “speak plainly” and not be understood.

Re: Encrypt in Perl and decrypt in JAVA
by Anonymous Monk on Feb 23, 2012 at 19:30 UTC
    Was googling to find a way to do symmetric key encryption/decryption between Perl and Java. I was surprised that I could not find any working examples. I am replying here because this was the most recent search result. I finally got it working using AES. If anyone is interested in the code I can post it. Turns out that I will not be using it =( JamesUSC
      Hi Anonymous monk, Could you please post the code. Thanks!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://880552]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-10-01 11:36 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (126 votes). Check out past polls.