[erlang-questions] blowfish cbc mode decrypt

Bogdan Andu bog495@REDACTED
Tue Nov 10 10:58:22 CET 2015


came back with example:

Encryption part is a Perl script:
use strict;
use warnings;
use MIME::Base64;

use Crypt::CBC;
#use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
use PHP::Serialization qw(serialize unserialize);

my $pt = 'a:10:{s:6:"adresa";s:89:"Address 2 TEST
\xc3\x84\xc2\x83\xc3\x83\xc2\xae\xc3\x88\xc2\x99\xc3\x88\xc2\x9b\xc3\x88\xc2\x99\xc3\x88\xc2\x9bbl
7bap 71district
XXXBucure\xc3\x88\xc2\x99tiJUDE\xc3\x88\xc2\x9a031905RO";s:4:"info";i:1460382;s:7:"urlback";s:41:"
https://192.162.16.116:8020/snep_response";s:4:"cuip";s:18:"Cererea nr
1460382";s:6:"idtaxa";i:5001;s:5:"email";s:16:"xxx123@REDACTED";s:4:"nume";s:55:"\xc3\x88\xc2\x99
\xc3\x88\xc2\x9b \xc3\x84\xc2\x83 \xc3\x83\xc2\xae \xc3\x83\xc2\xa2
\xc3\x83\xc2\x82 \xc3\x83\xc2\x8e \xc3\x84\xc2\x82 \xc3\x88\xc2\x98
\xc3\x88\xc2\x9a u\xc3\x83\xc2\xa7";s:3:"cui";s:18:"Cererea nr
1460382";s:9:"idnomunic";i:13;s:4:"suma";d:262.69;}';

print $pt, "\n";

my $key = "12345678900987654321001234567890";
my $cipher = Crypt::CBC->new(
                   -key    => $key,
                -cipher => 'Blowfish',
                -header => 'randomiv'
);

        #       print "$pt\n";
my $encpt = $cipher->encrypt($pt);
print "\n$encpt", "\n";

print "\n", encode_base64($encpt), "\n";

## TEST
my $decpt = $cipher->decrypt($encpt);
print "\n$decpt", "\n";


I want this (Base64 encoded) quantity decrypted in Erlang part:
$ erl
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:2:2] [async-threads:10]
[kernel-poll:false]

Eshell V7.0  (abort with ^G)
1> f(B),
B="UmFuZG9tSVYGr2WfCVKA0rvdisU2mA+OGo4yopGlUsszoG+V3vXRifqRM+YkAYWzdodx0LdyYJC7cwvY+iRGytQxUBOpOjEBvvhXRFyTu88+FeWPh2nZ28BDLqYPgg5tQujWYlipdrafH6XUzgpXK8QMZ/JpgIYYHZ3b8te2iWU3YsCo76/2W6n1D8zyWSdJ9owr/KtmrYE4FqLszvg4Vj0l62Q95sXNQOBb7g0GwuwJm6CRbLY6FmVDkFCPzqKf/QZbWASfIFokmfhnHR+Rpc8y2dz+eJjJn0LeFwajcSCf78IoZ1j8BW02mcyQFji/e/TfuqImRrhFptBlvMggu6g6dksM4iCW8E63y2KvO2rCbnbIyapENcZaNtfsUkw/WQNGP72VDvZbHeb/E1NIwnbsOO5qHnzMAi2SFVXSMr+k6mIS+R5wKDvkquB7I1Vho5SVdnjE1LtaetLDPF8kFNh09ig51OPCQyezpEsIPBI+U9mWpAyZTJU7YtnZUFY4oA4tbMPUthkOE2E0qIQT7OL0hwTyHsn/YFCyKu7JqnEU9UaCItb8SteLpb4ZlGJsoPxdZPnaBJJniPhod+0a7mDuo/N+hijk6rHEs2hXeUNgSSyoMVylMAuQyqDJMuOWMWMNWlr/HjpIWiRw6h2F5rrR8peV20sNXo9MHdcWG/XbZdUlGwfkHgi23PtcO/fpkdQ3UTRmOcm3I3KG2w8YAc10434pbs2NYihBl7v4Rx5H0NkJePPpRzHsavYO8QH5R9INzUh0yYXgz5jWpQbqSp7FMVYnwLqEgLoXt0r7WQqU+9tgyyPQxfRZQd5URs5QxwoGC+zgDqgLCdNzdzanePh5IfCsDoeUJbZ3C4SAGxiZ+OPZYu8ixM/sYqjQmPgmXs1a7X1VthbNLxrSp5pm2cjyTRal+AFr".
2>
2>f(Bdec),Bdec = base64:decode(B).
3>Key = hexstr2bin("12345678900987654321001234567890").
4><<IvSpec:8/binary, IV:8/binary, Rest/binary>> = Bdec.
5>f(CifDec),CifDec = crypto:block_decrypt(blowfish_cbc, Key, IV, Rest).
6>io:format("~s~n", [CifDec]).
---output garbage---[should be $pt variable from Perl script]
7>erlang:size(Rest).
680
8> 680/8.
85.0
9>IvSpec.

<<"RandomIV">>
10>


...

Notes:
hexstr2bin/1 is taken from otp_src_18.0/lib/crypto/test/crypto_SUITE.erl

On Mon, Nov 9, 2015 at 7:06 PM, Dmitry Kolesnikov <dmkolesnikov@REDACTED>
wrote:

> Hello,
>
> I would double check that you are
>  * using same key on both side
>  * IV is properly serialized and de-serialize to packet frame
>
> BTW, What is the size of Rest? and Can you share minimal viable example of
> encryption?
>
> - Dmitry
>
> > On Nov 9, 2015, at 11:38 AM, Bogdan Andu <bog495@REDACTED> wrote:
> >
> >
> > hi,
> >
> > I really need some pointers about this issue
> >
> > as i tried all kinds of combinations of IV and body
> > to be decrypted.
> >
> > Am i missing something or there is a bug in cipher
> > blowfish_cbc ?
> >
> >
> >
> > On Thu, Nov 5, 2015 at 3:51 PM, Bogdan Andu <bog495@REDACTED> wrote:
> >  Data to be decrypted (arrived on socket, etc) is:
> > <<IvSpec:8/binary, IV:8/binary, Rest/binary>> = EncryptedData
> >
> > IV is ectracted form the 8-byte prepanded value preceding
> > IvSpec which has the value RandomIV.
> >
> > decryption of Rest should be initialized with IV.
> >
> >
> > On Thu, Nov 5, 2015 at 3:38 PM, Radoslaw Gruchalski <
> radek@REDACTED> wrote:
> > You say your data is:
> >
> > <<IvSpec>> == <<RandomIv>>
> >
> > But you read:
> >
> > Is your data <<IvSpec:8/binary, IV:8/binary, Rest/binary>>
> >
> > Should it not be just:
> >
> > <<IV:8/binary, Rest/binary>>
> >
> > ?
> >
> > Sent from Outlook
> >
> > _____________________________
> > From: Bogdan Andu <bog495@REDACTED>
> > Sent: Thursday, November 5, 2015 2:26 p.m.
> > Subject: [erlang-questions] blowfish cbc mode decrypt
> > To: Erlang <erlang-questions@REDACTED>
> >
> >
> >
> > Hi,
> >
> > I have a stream of data called EncryptedData (with randomiv header
> specification).
> > I want to decrypt  using:
> > crypto:block_decrypt(blowfish_cbc, hexstr2bin(Key), IV, Rest).
> >
> > where:
> >
> > EncryptedData is formatted as follows:
> > <<IvSpec:8/binary, IV:8/binary, Rest/binary>>
> >
> > IvSpec == <<"RandomIV">>
> >
> > However the decryption does not works and garbage is generated
> > (the function  ) .
> >
> > What could be done given the fact that IV is an 8-byte quantity
> > the Key is formatted according to crypto test suites and is 16 bytes long
> >
> > Am I missing something?
> >
> > Thanks,
> >
> > Bogdan
> >
> >
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151110/2e3d922f/attachment.htm>


More information about the erlang-questions mailing list