Question

I am using Perl to perform CBC DES encryption using the Crypt::CBC library:

#!/usr/bin/perl
use Crypt::CBC;
$key  = "\x4A\x6F\xC2\x2A\x44\xE2\xA4\x48"; 
$iv   = "\x00\x00\x00\x00\x00\x00\x00\x00";
$data = "\x51\x55\x45\x53\x54\x49\x4F\x4E";

print "TXT->", $data, "\n";
print "HEX->", unpack("H*", $data), "\n";
$cipher = Crypt::CBC->new(-literal_key => 1,
                    -key         => $key,
                    -iv          => $iv,
                    -header      => 'none');

$ciphertext = $cipher->encrypt($data);
print "ENC->", unpack("H*", $ciphertext), "\n";

The output of the code is:

TXT->QUESTION
HEX->5155455354494f4e
ENC->8220553e09f1b31ba7691f3f7fb52416

My data is conveniently of size 64bits (16 hex digits) which is in accordance with the DES standard. According to Wikipedia

DES is the archetypal block cipher — an algorithm that takes a fixed-length string of plaintext bits and transforms it through a series of complicated operations into another ciphertext bitstring of the same length

Why is it that the encoded output is of longer byte length than the original input?

Thanks.

Était-ce utile?

La solution

Working backward from the second block (a7691f3f7fb52416) gives 8a285d3601f9bb13, and XORed with the first block (8220553e09f1b31b) gives 0808080808080808 (HEX). Something is producing the block value of 0808080808080808 as a second input block value.

CBC Values

So all you have to do is figure out where the backspace characters came from as a second block input.

See https://metacpan.org/pod/Crypt::CBC

This:

#!/usr/bin/perl

use Crypt::CBC;
$key  = "\x4A\x6F\xC2\x2A\x44\xE2\xA4\x48"; 
$iv   = "\x00\x00\x00\x00\x00\x00\x00\x00";
$data = "\x51\x55\x45\x53\x54\x49\x4F\x4E";

print "TXT->", $data, "\n";
print "HEX->", unpack("H*", $data), "\n";
$cipher = Crypt::CBC->new(-literal_key => 1,
                    -key         => $key,
                    -iv          => $iv,
                    -header      => 'none',
                    -padding     => 'null');

$ciphertext = $cipher->encrypt($data);

print "ENC->", unpack("H*", $ciphertext), "\n";

Gave:

david_koontz@Macbook: cbc_des
TXT->QUESTION
HEX->5155455354494f4e
ENC->8220553e09f1b31b
david_koontz@Macbook:

I made the mistake poking around because I know a fair bit about DES, not so much perl.

Adding the padding null seemed to do the trick, after I learned how to add Crypt::CBC and Crypt::DES to a perl library.

I used http://code.google.com/p/dpades/source/browse/trunk/simu_js/JS-DES.html to do the encryptions and decryptions necessary to figure out what's going on. Use the view raw file button and save JS-DES.html locally, open it with a browser.

Autres conseils

The encrypted message is longer because it includes the IV. BTW, a fixed IV does not make sense, it should be random and newly generated for each message.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top