RSA Encryption process in PHP -
in effort understand asymmetric encryption process outlined simple php script encrypt , decrypt simple numbers. noticed after while given numbers encrypt/decrypt algorithm fail, in decrypted , initial numbers didn't match. put loop in see how algorithm perform when ecrypting , decrypting 100 numbers , after number 32 process fell apart.
is because p*q = 33?
<?php # test encrypto algo // choose prime keys $p = 47; $q = 71; // compute n = pq $n = $p*$q; // choose e such 1 < e < f(n) , e , n coprime $e = 79; // compute value d such (d * e) % f(n) = 1 $d = 1019; // compute f(n) = (p-1)(q-1) $z = ($p - 1)*($q - 1); // create public , private keys $pubk = array('n' => $n, 'e' => $e); $privk = array('n'=> $n, 'd' => $d); // boundary 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 public key function enc($sample, $key) { return bcmod(bcpow($sample,$key['e']),$key['n']); } // decrypt encrypted sample private key function dec($sample, $key) { return bcmod(bcpow($sample, $key['d']),$key['n']); } ?>
http://en.wikipedia.org/wiki/rsa_(algorithm)
2.2 encryption:
[...] bob wishes send message m alice. first turns m integer m, such 0 ≤ m < n using agreed-upon reversible protocol known padding scheme. ...alice transmits public key (n, e) bob , keeps the
this not hold here, therefore need use larger factorizations.
Comments
Post a Comment