Hi Chanan, The cipher is initialized with the nonce you provide, but then changes the IV with every encryption operation - so you can't use the same cipher for encrypting AND THEN decrypting (the last encrypted value is used as the IV, and not the initial IV you provided).
You can easily fix this by using two different ciphers, one for encrypting and one for decrypting, initializing both with the same IV. Also you should use a random value as the nonce, and save it or transmit it alongside with the cipher text (a nonce is not a secret) - the only purpose of the nonce is to prevent key re-use. Here's a working example that uses a random value for the nonce and two ciphers: use warnings; use strict; use warnings; use strict; use Crypt::Salsa20; use Bytes::Random::Secure qw/random_bytes/; my $key = 'aaaaaaaaaaaaaaaa'; my $nonce = random_bytes(8); my $value = 'Chanan'; my $C = Crypt::Salsa20->new(-key => $key, -iv => $nonce); my $D = Crypt::Salsa20->new(-key => $key, -iv => $nonce); my $encrypted_value = $C->encrypt($value); my $value_new = $D->decrypt($encrypted_value); print $value_new, "\n"; Regards, Ynon On 4 September 2014 12:08, Chanan Berler <[email protected]> wrote: > Hi All, > > has anyone use salsa20 encryption chipper code? > I have tried to following but failed: > Q: does anyone knows why I cannot decipher the value back? > > thanks > Chanan > > #!/usr/bin/perl +x > use warnings; > use strict; > use warnings; > use strict; > > use Crypt::Salsa20; > > my $key = 'aaaaaaaaaaaaaaaa'; > my $nonce = 'bbbbbbbb'; > my $value = 'Chanan'; > > my $C = Crypt::Salsa20->new(-key => $key, -iv => $nonce); > > my $encrypted_value = $C->encrypt($value); > my $value_new = $C->decrypt($encrypted_value); > > print $value_new; > > -- > =================== > ---- Chanan Berler ---- > =================== > > _______________________________________________ > Perl mailing list > [email protected] > http://mail.perl.org.il/mailman/listinfo/perl >
_______________________________________________ Perl mailing list [email protected] http://mail.perl.org.il/mailman/listinfo/perl
