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
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.