kepler has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I was wondering if this is possible. I'm encrypting some strings in Perl using my $encrypted = $str ^ substr($key,0,length($str)); where $key is 255 length. Can I decrypt, in C, the string with decrypted = encrypted ^ key?

Kind regards

Kepler

Replies are listed 'Best First'.
Re: Encrypt/decrypt string in C and Perl
by choroba (Cardinal) on Sep 18, 2016 at 13:35 UTC
    I've never really programmed in C, but I tried the following:
    #!/usr/bin/perl use warnings; use strict; my $str = shift; my $key = join q(), map chr rand 255, 0 .. 255; print "$key\n"; my $encrypted = $str ^ substr($key,0,length($str)); print "$encrypted\n";

    #include<stdio.h> int main () { char key[255], string[255]; gets(key); gets(string); for (int i=0;string[i] != 0;i++) { printf("%c", key[i] ^ string[i]); } return 0; }

    The ^ operator doesn't seem to work for strings, so I had to iterate the strings character by character.

    It seems to work sometimes , I'm not sure why not always. Maybe you can get 0 as a result of the xor somewhere in the middle of a string? Or a newline in the middle of the key? But it should get you started.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      It seems a very good start!!! Thanks :)
Re: Encrypt/decrypt string in C and Perl
by BrowserUk (Patriarch) on Sep 18, 2016 at 13:42 UTC

    Yes. You just need to loop over the characters in C. Eg:

    #! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'junk1_IC', CLEAN_AFTER_BUILD => 0 +; void decrypt( char *e, char *key ) { while( *e ) *e++ = *e ^ *key++; return; } SV *testit( SV *e, SV *key ) { SV *r = newSVsv( e ); decrypt( SvPVX( r ), SvPVX( key ) ); return r; } END_C use List::Util qw[ shuffle ]; use Data::Dump qw [ pp ]; use constant KEY => join'', shuffle map chr, 0 .. 255; my $source = 'the quick brown fox jumps over the lazy dog'; my $encrypted = $source ^ substr( KEY, 0, length( $source ) ); print "Encrypted:[$encrypted]"; my $decrypted = testit( $encrypted, KEY ); print "Decrypted:[$decrypted]"; __END__ C:\test>1172052.pl Encrypted:[Ù?Â→┬ßÁÀFBQÏnå 8üõø♣ÆÄãk­i↨üÿtë½ +↨♠,♫õ"æ↓oó] Decrypted:[the quick brown fox jumps over the lazy dog]

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.