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

Hello,
I am trying to encrypt and decrypt a string using the RSA encryption algorithm from http://cypherspace.org/~adam/rsa/story.html.
I am using the pure Perl version and following the instructions exactly as printed. Yet it doesn't work. The algorithm is:
do 'bigint.pl';($_,$n)=@ARGV;s/^.(..)*$/0$&/;($k=unpack('B*',pack('H*' +,$_)))=~ s/^0*//;$x=0;$z=$n=~s/./$x=&badd(&bmul($x,16),hex$&)/ge;while(read(STD +IN,$_,$w =((2*$d-1+$z)&~1)/2)){$r=1;$_=substr($_."\0"x$w,$c=0,$w);s/.|\n/$c=&ba +dd(&bmul ($c,256),ord$&)/ge;$_=$k;s/./$r=&bmod(&bmul($r,$r),$x),$&?$r=&bmod(&bm +ul($r,$c ),$x):0,""/ge;($r,$t)=&bdiv($r,256),$_=pack(C,$t).$_ while$w--+1-2*$d; +print}
Is there an issue with Win32 and this code?

Thanks

Replies are listed 'Best First'.
(ar0n) Re: RSA encryption on Win32
by ar0n (Priest) on Feb 26, 2001 at 19:41 UTC
    Just a wild guess here, but do you actually have bigint.pl?

    Notice the first statement: do 'bigint.pl';

    Or: you could show us the error message ;-)

    [ar0n]

Re: RSA encryption on Win32
by perly_gates (Initiate) on Feb 26, 2001 at 15:59 UTC
    A bit more on the question above...
    I have tried altering the code to use files opened via sysread and sysopen, specifying a binary read. It still doesn't work.
    This seems to disprove that the code might be having trouble with CR/LFs.
Re: RSA encryption on Win32
by a (Friar) on Feb 27, 2001 at 11:20 UTC
    I tried it (I think) and get "out of memory". Is there some reason for the obfusticated code? What does:
    ($r,$t)=&bdiv($r,256),$_=pack(C,$t).$_ while$w--+1-2*$d;
    become? ($w--)+1-(2*$d)??

    a

      Yeah, it is a bit obfuscated, but not too much. The $w--+1-2*$d means that it outputs one extra byte per group when encrypting ($d==0) and then strips that extra byte per group when decrypting ($d==1 because the script specified -s to perl and you specified -d to the script).

      Stepping through the algorythm, it appears at least close. I haven't had time to debug the problem, though. If I could find a better all-Perl implementation of this, I might be able to compare the behavior of the two and figure out the problem.

              - tye (but my friends call me "Tye")
Re: RSA encryption on Win32
by perly_gates (Initiate) on Feb 28, 2001 at 01:32 UTC
    More information...
    There is no error message.
    I do have Bigint.pl
    Expanded Perl code is available at http://cypherspace.org/~adam/rsa/story.html
    I'm following a tutorial from http://www.webreference.com/perl/tutorial/16/
    My encryption key is: 3353bc7
    My decryption key is: 1c07e37
    N is 6537b15
    My dos commands look like this: perl rsa.pl -k=3353bc7 -n=6537b15 < text.txt > text.out
    perl rsa.pl -d -k=1c07e37 -n=6537b15 < text.out
    The decrypted message should print to the screen.
    I got these keys off the tutorial b/c I didn't want to compile a key generator on win32. Are the keys incorrect?
    I don't think so.

      Well, the usage you describe doesn't match the code. Try:

      perl rsa.pl 3353bc7 6537b15 <text.out >text.enc perl rsa.pl -d 1c07e37 6537b15 <text.enc >text.dec

      Not that I think that will work (that is mostly what I tried), but at least it matches what the code appears to want. I've watched it go through its calculations and they appear to make sense.

      Of course there are no error messages, there is no error checking. O-: There must be a "real" (as opposed to "cute") Perl-only implementation of RSA floating around (I only looked briefly).

              - tye (but my friends call me "Tye")
        Actually this isn't going to work, because rsa.pl relies on perl being executed with the -s switch, so if you execute the script like this perl never gets the -s switch. Either do
        % perl -s rsa.pl ...
        Or make rsa.pl executable and run it like
        % ./rsa.pl ...
        BTW, w/r/t to a "real" implementation of RSA: I think this'd be a great thing and I know that I've not really found much, so I think I'm going to do some work on this.
        Re: Perl-only implementation of RSA: take a look at this node, just written.