DCIT / perl-CryptX

https://metacpan.org/pod/CryptX
Other
34 stars 23 forks source link

[Documentation] Crypt::Mode::CBC and Crypt::CBC compatibility #62

Closed KnowZero closed 4 years ago

KnowZero commented 4 years ago

It is probably a good idea to add that if you want Crypt::Mode::CBC to be backwards compatible with Crypt::CBC if header is set to none or randomiv and literal_key is not set.

       my $keysize = Crypt::Cipher->keysize($name);
       $key = Digest::MD5::md5($key);
      while (length($key) < $keysize)  {
        $key .= Digest::MD5::md5($key);
      }
     $key = substr($key,0,$keysize);

As for decrypting with header salt

my ($salt) = $text =~/^Salted__(.{8})/s;
substr($text,0,16) = '';

($key,$iv) = _salted_key_and_iv($key,$salt);

sub _salted_key_and_iv {

  my ($key,$salt)  = @_;

  my $key_len = Crypt::Cipher->keysize('AES');
  my $iv_len  = Crypt::Cipher->blocksize('AES');

  my $desired_len = $key_len+$iv_len;

  my $data  = '';
  my $d = '';

  while (length $data < $desired_len) {
    $d = Digest::MD5::md5($d . $key . $salt);
    $data .= $d;
  }
  return (substr($data,0,$key_len),substr($data,$key_len,$iv_len));
}
karel-m commented 4 years ago

I am afraid I am not going to push compatibility with Crypt::CBC that far.