O
ofer
Hello again 
I wrote an encryption program, based on Crypt::Blowfish.
This is a block cipher, which means that I have to encrypt 8 bytes each
time.
The problem is that I want to encrypt strings which are not exactly 8
bytes, or a whole multiplication of 8.
What I did was to pad the last, incomplete buffer.
If I have this string:
XXXX
I just pad the buffer like this:
XXXX4444
some more examples:
XX -> XX666666
XXX -> XXX55555
X -> X7777777
XXXXXXX -> XXXXXXX1
I hope you get it.
Okay, so that went great.
Now I want to decrypt it. So first I decrypt, and then I remove the
padding.
----------------------------------------------------------------------------------------
while ( length( $block ) < $block_size ) {
my $did_read = read( IN, $block, $block_size - length( $block ),
length( $block ) );
defined( $did_read ) || die( "Reading from $fn failed ($!)" );
$did_read == 0 && do {
$last_block = 1;
};
}
# Decrypt block
$block_out = $cipher->decrypt( $block );
# Remove padding if last block
$last_block && do {
my $pad_len = substr( $block_out, -1 );
substr( $block_out, -$pad_len ) = "";
}
----------------------------------------------------------------------------------------
I have pasted only the relevant parts of course.
The first part is here in order to show you what is $last_block and how
I read from the file, and the second part is the decryption and padding
removal.
I can't explain what is the problem exactly, but I know what the
padding is not removed as it finishes.
Can you please help me?
Thank you very much!
And sorry that I don't give you a full functional program, I just
thought that it won't be neccesary because it is a logical problem
which can be solved by only looking at the code. If you need anything
else please let me know
I wrote an encryption program, based on Crypt::Blowfish.
This is a block cipher, which means that I have to encrypt 8 bytes each
time.
The problem is that I want to encrypt strings which are not exactly 8
bytes, or a whole multiplication of 8.
What I did was to pad the last, incomplete buffer.
If I have this string:
XXXX
I just pad the buffer like this:
XXXX4444
some more examples:
XX -> XX666666
XXX -> XXX55555
X -> X7777777
XXXXXXX -> XXXXXXX1
I hope you get it.
Okay, so that went great.
Now I want to decrypt it. So first I decrypt, and then I remove the
padding.
----------------------------------------------------------------------------------------
while ( length( $block ) < $block_size ) {
my $did_read = read( IN, $block, $block_size - length( $block ),
length( $block ) );
defined( $did_read ) || die( "Reading from $fn failed ($!)" );
$did_read == 0 && do {
$last_block = 1;
};
}
# Decrypt block
$block_out = $cipher->decrypt( $block );
# Remove padding if last block
$last_block && do {
my $pad_len = substr( $block_out, -1 );
substr( $block_out, -$pad_len ) = "";
}
----------------------------------------------------------------------------------------
I have pasted only the relevant parts of course.
The first part is here in order to show you what is $last_block and how
I read from the file, and the second part is the decryption and padding
removal.
I can't explain what is the problem exactly, but I know what the
padding is not removed as it finishes.
Can you please help me?
Thank you very much!
And sorry that I don't give you a full functional program, I just
thought that it won't be neccesary because it is a logical problem
which can be solved by only looking at the code. If you need anything
else please let me know