Page 1 of 1

PHP RSA (Public/Private key encryption)

Posted: Wed Nov 10, 2004 7:26 am
by sj26
Hi there,

I'm making a distributed PHP app that I was to have RSA security functionality, and I don't want to rely upon (although I may use if available) PHP extensions to do the work, hence I need a pure PHP solution as a backup. Does an RSA solution for PHP exist (I did some googling and couldn't find anything...) or do I have to make one? I've found a really cool Perl RSA Module which I could port, but i'm lazy, so I'm asking if it's already been done (and, of course, everyone knows Perl ~= s/ux0r//) :D

I've already started converting... so I may just keep going, but I'd love some other inspirational sources!

Thanks!

Posted: Wed Nov 10, 2004 11:58 am
by Weirdan

Posted: Wed Nov 10, 2004 8:41 pm
by sj26
Thanks Weirdan! Though art mightier in Googling that I :D It's not quite what I'm after, though. It does do the RSA stuff, but it is dependant on another extension - and it's a problem I've encounted as I port the Perl version.

There is no decent math library for PHP!!! The only ones available are bcmath (which is good, not an extension, but very simple featureset) and GMP (and extension; this is the one the above file uses).

What I need now is a decent PHP math library - one that supports gcd (greatest common divisor) and a prime possibility method (ie an is_prime() kind of function - that identifies primes and pseudoprimes to a certain degree of accuracy). If anyone knows of any decent pure PHP math libraries with these (or any decent) functions, I would love to know :)

Posted: Thu Nov 11, 2004 10:36 am
by Weirdan
sj26 wrote: What I need now is a decent PHP math library - one that supports gcd (greatest common divisor)
gcd is pretty easy to implement. For example, using Euclidian algorithm:

Code: Select all

// Uses Euclid's Algorithm to calculate greatest common divisor.
// See http://www.cut-the-knot.com/blue/Euclid.html for details.
function gcd( $a, $b )
{
    while ( $b > 0 )
    {
        $remainder = $a % $b;
        $a = $b;
        $b = $remainder;
    }
    return abs( $a );
}
Not very efficient, but simple.
sj26 wrote: and a prime possibility method (ie an is_prime() kind of function - that identifies primes and pseudoprimes to a certain degree of accuracy). If anyone knows of any decent pure PHP math libraries with these (or any decent) functions, I would love to know :)
prime is a number which have only two divisors: 1 and itself. Simplest way would be to divide the number ($n) on every number in the range 1 < $i < sqrt($n). $n is prime if there were no integer quotients. You may find more efficient approach at http://www.phpbuilder.com/snippet/downl ... et&id=1148

Posted: Thu Nov 11, 2004 12:10 pm
by sj26
't would be nice to get a math library supporting prime probablities, though. I already had the Euclidian algorithm... it just keeps stuffing up, although that may just be me with the bc functions...
prime is a number which have only two divisors: 1 and itself. Simplest way would be to divide the number ($n) on every number in the range 1 < $i < sqrt($n). $n is prime if there were no integer quotients.
definitly don't want to do that with the numbers involved in RSA ... in order of 2^1024 (1024-bit values)... gets messy.

but thank you :)

Posted: Thu Nov 11, 2004 12:15 pm
by sj26
That snippet is gold! exactly what i needed - thanks!