In an attempt to understand the asymmetric encryption process, I set out a simple PHP script for encrypting and decrypting primes. I noticed that after a while the data for encrypting / decrypting the algorithm will fail, since the decrypted and initial numbers do not match. I set up a loop to see how the algorithm will work when encrypting and decrypting 100 numbers, and after number 32 the process falls apart.
Is it because p * q = 33?
<?php # Test encrypto algo // Choose prime keys $p = 47; $q = 71; // Compute n = pq $n = $p*$q; // Choose e such that 1 < e < f(n) and e and n are coprime $e = 79; // Compute a value for d such that (d * e) % f(n) = 1 $d = 1019; // Compute f(n) = (p-1)(q-1) $z = ($p - 1)*($q - 1); // Create public and private keys $pubK = array('n' => $n, 'e' => $e); $privK = array('n'=> $n, 'd' => $d); // Boundary for loop $l = 100; // Perform encypt/decrypt on 1..100 for($i = 1; $i <= $l; $i++) { $enc = enc($i, $pubK); $dec = dec($enc, $privK); print "encrypted <b>$i</b> = $enc decrypted $enc = <b>$dec</b> "; if($i == $dec) print "Success<br>"; else print "Fail<br>"; } // Encrypt sample with public key function enc($sample, $key) { return bcmod(bcpow($sample,$key['e']),$key['n']); } // Decrypt encrypted sample with private key function dec($sample, $key) { return bcmod(bcpow($sample, $key['d']),$key['n']); } ?>

source share