Decrypting text encrypted using CBC Blowish in perl using Crypto++

56 views
Skip to first unread message

Troy

unread,
Oct 25, 2012, 9:30:56 PM10/25/12
to cryptop...@googlegroups.com
Hello,

I have been trying to figure out how to get decrypting of text in perl working in crypto++ (and possibly the reverse).

#!/usr/bin/perl

use Crypt::CBC;
$iv = "12345678";
$cipher = Crypt::CBC->new( -literal_iv => 1, -literal_key => 1, -key => "wowsers ", -iv => $iv, -header => "none");

$plaintext = "my message";

$ciphertext = $cipher->encrypt_hex($plaintext);

$hex_iv = unpack('H*', $iv);

print "$hex_iv$ciphertext\n";

$plaintext = $cipher->decrypt_hex($ciphertext);

print "$plaintext\n";

As per the options enabled in perl, I have disabled all of the default features and am trying to get it working with a literal key and literal iv (which is being prepended to the output.

I am then failing to decrypt the message using the following crypto++ code:

    void prepare_key(std::string& key)
    {
      ssize_t len = static_cast<ssize_t>(key.length());
      if (len < Blowfish::MIN_KEYLENGTH)
        key.append(Blowfish::MIN_KEYLENGTH - len, ' ');
      else
      if (len > Blowfish::MAX_KEYLENGTH)
        key.erase(Blowfish::MIN_KEYLENGTH);
      else
      {
        size_t remainder = len % Blowfish::BLOCKSIZE;
        if (remainder) key.append(Blowfish::BLOCKSIZE - remainder, ' ');
      }
    }
  
    bool encrypt(const std::string& source,
                 std::string& dest,
                 std::string key)
    {
      prepare_key(key);
      
      AutoSeededRandomPool prng;
      byte iv[Blowfish::BLOCKSIZE];
      prng.GenerateBlock(iv, sizeof(iv));

      dest.clear();
      try
      {
        CBC_Mode<Blowfish>::Encryption e;
        e.SetKeyWithIV(reinterpret_cast<const byte*>(key.c_str()), key.length(), iv);
        
        StringSource(source, true,
            new StreamTransformationFilter(e,
                new StringSink(dest)
            )
        );
      }
      catch (const Exception& e)
      {
        return false;
      }

      dest.insert(dest.begin(), std::begin(iv), std::end(iv));
      
      return true;
    }
  
    bool decrypt(std::string source, std::string& dest, std::string key)
    {
      prepare_key(key);
      
      AutoSeededRandomPool prng;
      byte iv[Blowfish::BLOCKSIZE];
      std::memcpy(iv, &source[0], sizeof(iv));
      source.erase(0, sizeof(iv));
      
      dest.clear();
      try
      {
        CBC_Mode<Blowfish>::Decryption d;
        d.SetKeyWithIV(reinterpret_cast<const byte*>(key.c_str()), key.length(), iv);
        
        StringSource(source, true,
            new StreamTransformationFilter(d,
                new StringSink(dest)
            )
        );
      }
      catch (const Exception& e)
      {
        return false;
      }
       
      return true;
    }  

The crypto++ based encrypt / decrypt functions work fine with eachother. If I attempt to encrypt the same message as in the perl script with the same key and iv, the result is different.

Any help much appreciated!
Troy

Troy

unread,
Oct 26, 2012, 1:30:04 AM10/26/12
to cryptop...@googlegroups.com
Please disregard this, I've made a glaring error in the perl script omitting the -cipher option, no idea what it uses for default.
Reply all
Reply to author
Forward
0 new messages