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

Could use any pointers from any Java/crypto experts here. I need to convert the following java snippet to its equivalent Perl:


StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
standardPBEStringEncryptor.setSaltGenerator(sg); <--- just a byte array ... of 8 bytes
standardPBEStringEncryptor.setPassword("SOME PASSWORD");
standardPBEStringEncryptor.setStringOutputType("hexadecimal");
String myEncryptedText = standardPBEStringEncryptor.encrypt(text_to_encrypt);

The problem here is that there is no mention of the default algorithm used in the encryption. I get the feeling that it is PKCS5 v2. Assuming that to be the case, are there any free Perl libraries that can do PKCS5 ?

Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: encryption related question ..
by moritz (Cardinal) on Jun 22, 2010 at 09:16 UTC
    From reading RFC 2898 it seems to me that PKCS5 is a standard for file formats, padding schemes and the like, not an encryption algorithm.

    I'd guess that it uses AES as encryption method, but in case of doubt you need to find the documentation and/or the source code of the encryption library to find out.

    Crypt::CBC supports PKCS#5 padding, so that's what you should try to use.

    Perl 6 - links to (nearly) everything that is Perl 6.

      Hmm, I'm not sure, see PKCS #5: Password-Based Cryptography Standard. It's not clear to me what the Java implementation uses though. For key derivation MD2, MD5, SHA-1 or some pseudo random function is used. As an encryption scheme, DES or RC2 is used, or again a key derivation function, e.g. a HMAC-SHA variant (from appendix B, draft PKCS #5 v2.1 ). By combining some perl modules from Gisle Aas it should be possible to duplicate the StandardPBEStringEncryptor class behavior. But it starts by finding out what exactly this class implements, so far I have not been able to find it.

      Then I found does-it-implement-or-include-cryptographic-algorithms?. So it seems to use whatever the Java Cryptographic Extension (JCE) provides. And at a first glance it looks like "it depends", like on the java version used?

      Cheers

      Harry