H
Hal Vaughan
I have to admit I barely understand any cryptography beyond the simple A=1
type codes my friends and I used in grammar school. I have a server,
running Perl, that takes a file, encrypts it with Blowfish, then MIME
encodes it, sends it out via e-mail. Then I have a Java program that
retrieves the e-mail, MIME decodes it, then decrypts it. I am having no
problem on the Perl end, and any encrypted e-mail I create I can retrieve
and decode. There seems to be no problem with MIME encoding/decoding. The
problem comes when my Java program tries to decrypt the file created and
sent by Perl. I always get:
javax.crypto.BadPaddingException: Given final block not properly padded
When I first set this up, with the Perl code on another computer, I got
errors like that. They stopped, but I was never sure exactly what I did to
solve the problem (and I'm never comfortable with a situation like that).
I tried to examine my changes, and I could never be sure just what fixed
it. Now the Perl code is running on a new system. I'm using a later
version of Crypt::CBC (from 2.08 to 2.12), but I can't see anything in the
changelog that should effect the problem I'm having. I'd also like to
solve the problem once and for all, which means either understanding what
is going wrong with the BadPaddingException, or knowing what to do so Java
has no problem reading the encrypted data Perl sends.
My Perl code to encrypt the data is this:
$cipher = Crypt::CBC->new( { 'key' => $key, iv => $vector,
prepend_iv => 0, 'cipher' => 'Blowfish', 'regenerate_key' => 0 } );
$data = $cipher->encrypt($rawdata);
My Java code to decrypt the data is this:
//sCryptoKey and sCryptoVector are strings with the key and vector
//bCrypto is byte[] of encrypted data
//bDecrypted is byte[] of decrypted data
try {
SecretKeySpec oKey = new SecretKeySpec(sCryptoKey.getBytes("UTF8"),
"Blowfish");
IvParameterSpec oIV = new
IvParameterSpec(sCryptoVector.getBytes("UTF8"));
Cipher oCipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
oCipher.init(Cipher.DECRYPT_MODE, oKey, oIV );
bDecrypted = oCipher.doFinal(bCrypto);
} catch (Exception e) {
//Log the error
sysConfig.log("error", "error decrypting incoming file -- saving to
log
directory: " + e);
}
I'd like to have some better understanding of why I'm getting errors. I'm
not even fully clear on what padding is. I've tried reading up on how
Blowfish works, but I really don't follow it. From what I can see, I
suspect if I change the PKCS5Padding to NoPadding in the Java code should
work, but I'm assuming Padding is just the padding of data on the end. Is
there a way to tell Cipher to figure out the padding, or to take the data
"as is"? I'm also trying to find if there is a way to specify padding on
the Perl end. According to the docs for CBC on CPAN, the padding() method
in CBC is read-only, and I don't see a way to specify the padding in CBC.
I would think as long as I can specify the same padding on both ends there
should be no problem, but, again, there seems to be no way to specify
padding for Crypt::CBC.
Thanks for any help!
Hal
type codes my friends and I used in grammar school. I have a server,
running Perl, that takes a file, encrypts it with Blowfish, then MIME
encodes it, sends it out via e-mail. Then I have a Java program that
retrieves the e-mail, MIME decodes it, then decrypts it. I am having no
problem on the Perl end, and any encrypted e-mail I create I can retrieve
and decode. There seems to be no problem with MIME encoding/decoding. The
problem comes when my Java program tries to decrypt the file created and
sent by Perl. I always get:
javax.crypto.BadPaddingException: Given final block not properly padded
When I first set this up, with the Perl code on another computer, I got
errors like that. They stopped, but I was never sure exactly what I did to
solve the problem (and I'm never comfortable with a situation like that).
I tried to examine my changes, and I could never be sure just what fixed
it. Now the Perl code is running on a new system. I'm using a later
version of Crypt::CBC (from 2.08 to 2.12), but I can't see anything in the
changelog that should effect the problem I'm having. I'd also like to
solve the problem once and for all, which means either understanding what
is going wrong with the BadPaddingException, or knowing what to do so Java
has no problem reading the encrypted data Perl sends.
My Perl code to encrypt the data is this:
$cipher = Crypt::CBC->new( { 'key' => $key, iv => $vector,
prepend_iv => 0, 'cipher' => 'Blowfish', 'regenerate_key' => 0 } );
$data = $cipher->encrypt($rawdata);
My Java code to decrypt the data is this:
//sCryptoKey and sCryptoVector are strings with the key and vector
//bCrypto is byte[] of encrypted data
//bDecrypted is byte[] of decrypted data
try {
SecretKeySpec oKey = new SecretKeySpec(sCryptoKey.getBytes("UTF8"),
"Blowfish");
IvParameterSpec oIV = new
IvParameterSpec(sCryptoVector.getBytes("UTF8"));
Cipher oCipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
oCipher.init(Cipher.DECRYPT_MODE, oKey, oIV );
bDecrypted = oCipher.doFinal(bCrypto);
} catch (Exception e) {
//Log the error
sysConfig.log("error", "error decrypting incoming file -- saving to
log
directory: " + e);
}
I'd like to have some better understanding of why I'm getting errors. I'm
not even fully clear on what padding is. I've tried reading up on how
Blowfish works, but I really don't follow it. From what I can see, I
suspect if I change the PKCS5Padding to NoPadding in the Java code should
work, but I'm assuming Padding is just the padding of data on the end. Is
there a way to tell Cipher to figure out the padding, or to take the data
"as is"? I'm also trying to find if there is a way to specify padding on
the Perl end. According to the docs for CBC on CPAN, the padding() method
in CBC is read-only, and I don't see a way to specify the padding in CBC.
I would think as long as I can specify the same padding on both ends there
should be no problem, but, again, there seems to be no way to specify
padding for Crypt::CBC.
Thanks for any help!
Hal