Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem in decrypting the value encrypted in Perl using CBC Blowfish

843811Sep 18 2010 — edited Sep 23 2010
From past two days I'm struggling to decrypt a value (in Java) which is encrypted in Perl. I'm able to encrypt and decrypt them individually in both the languages. But when I input Perl's output to Java , things are not working.


Here is my code:

Perl Code:
#!/usr/bin/perl
use Crypt::CBC;
use MIME::Base64;

my $cipher = Crypt::CBC->new({
                    'literal_key'           => 0,
                    'cipher'                => 'Blowfish',
                    'key'                   => 'V0u96sXASd12P7u8',
                    'iv'                    => 'abcdefgh',
                    'prepend_iv'            => '0',
                    'padding'               => 'standard'
                    });

print encode_base64($cipher->encrypt(985427569875684));
From perl I'm getting the encrypted and base64 decoded value: 66CXML4SLlcHSUxO38rMpg==
This would be the input for the Java program to decrypt.
import java.security.Provider;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.xerces.impl.dv.util.Base64;

public class MyBlowfishCipher {

	public static void main(String[] args) throws Exception {
		Provider provider = new com.sun.crypto.provider.SunJCE();
		Security.addProvider(provider);

		String key = "V0u96sXASd12P7u8";
		SecretKeySpec sks = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");

		String initialVector = "abcdefgh";
		IvParameterSpec ivs = new IvParameterSpec(initialVector.getBytes("UTF-8"));

		Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding","SunJCE");

		cipher.init(Cipher.ENCRYPT_MODE, sks, ivs);

		byte[] encryptedData = cipher.doFinal("985427569875684".getBytes("UTF-8"));

		System.out.println(new String(encryptedData));

		String encoded = Base64.encode(encryptedData);

		System.out.println(encoded); //printing /d1sTQkceVeq5Qk80b9DCQ==, but the expected value is 66CXML4SLlcHSUxO38rMpg==
		
		cipher.init(Cipher.DECRYPT_MODE, sks, ivs);
		
		byte[] decoded = Base64.decode("66CXML4SLlcHSUxO38rMpg=="); // Output of the perl script is input here.

		byte[] decrypted = cipher.doFinal(decoded);
		
		System.out.println(new String(decrypted));

	}
}
Could someone please point out what's going wrong in this.

Thanks,
Srinivas
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 21 2010
Added on Sep 18 2010
2 comments
1,130 views