Skip to content

Instantly share code, notes, and snippets.

@ssut
Created May 7, 2014 02:30
Show Gist options
  • Save ssut/82374ea34740e8ad4971 to your computer and use it in GitHub Desktop.
Save ssut/82374ea34740e8ad4971 to your computer and use it in GitHub Desktop.
RSA Encrypt & Decrypt library
<?php
/**
* RSA Encrypt & Decrypt library
*
* Original algorithm is orginated by Ron Rivest, Adi Shamir and Leonard Adleman.
* Produce PHP Code by Hx0A5F.
**/
class RSA {
private $publicKey = 0;
private $privateKey = 0;
private $duplexKey = 0;
public function __construct($_securityClass = 5) {
if($_securityClass > 5) {
$_securityClass = 5;
} else if($_securityClass < 1) {
$_securityClass = 1;
}
$_securityClass *= rand(2, 7);
$_primeA = $this->getPrime(rand(4 * $_securityClass, 28), rand(16 * $_securityClass, 112));
$_primeB = $this->getPrime(rand(4 * $_securityClass, 28), rand(16 * $_securityClass, 112));
$this->duplexKey = $_primeA * $_primeB;
$_eulerPhi = ($_primeA - 1) * ($_primeB - 1);
$_eulerPhi = ($_primeA - 1) * ($_primeB - 1);
for($_Loop = 2; $_Loop < $this->duplexKey; $_Loop++) {
$_epDivs = $this->getDivisor($_eulerPhi);
$_loopDivs = $this->getDivisor($_Loop);
if($_eulerPhi % $_Loop == 0) {
$this->privateKey = $_Loop;
}
}
echo "\r\n<" . '[' . $_eulerPhi . ']{' . $_primeA . '/' . $_primeB . '}';
for($_Loop = 1; $_Loop < $_eulerPhi; $_Loop++) {
if(($this->privateKey * $_Loop) % $_eulerPhi == 1) {
$this->publicKey = $_Loop;
}
} echo ">\r\n\r\n";
}
public function Machine($_targetText, $_duplexKey, $_whateverKey) {
return pow($_targetText, $_whateverKey) % $_duplexKey;
}
private function getDivisor($_Number) {
$_Divisors = array();
for($_Loop = 1; $_Loop <= $_Number; $_Loop++) {
if(($_Number % $_Loop) == 0) {
$_Divisors[] = $_Loop;
}
}
return $_Divisors;
}
private function getPrime($_minNum, $_maxNum) {
if($_minNum != 2) { echo '<' . $_minNum . ' ' . $_maxNum . '>';
$_Primes = $this->getPrime(2, $_maxNum);
$_primesCount = count($_Primes);
for($_Loop = 0; $_Loop < $_primesCount; $_Loop++) {
if($_Primes[$_Loop] < $_minNum) {
array_shift($_Primes);
}
}
echo " (("; print_r($_Primes); echo "))\r\n\r\n";
return $_Primes[rand(0, count($_Primes) - 1)];
} else {
$_Primes = range($_minNum, $_maxNum);
for($_Loop = 2; $_Loop < $_maxNum; $_Loop++) {
$_primesCount = count($_Primes);
for($_subLoop = 0; $_subLoop < $_primesCount; $_subLoop++) {
if($_Loop != $_Primes[$_subLoop] && $_Primes[$_subLoop] % $_Loop == 0) {
unset($_Primes[$_subLoop]);
}
}
sort($_Primes);
}
return $_Primes;
}
}
public function getPublic() {
return $this->publicKey;
}
public function getPrivate() {
return $this->privateKey;
}
public function getDuplex() {
return $this->duplexKey;
}
}
function rsa_encrypt($_targetCode, $_primeA, $_primeB) {
$_mPrime = $_primeA * $_primeB; // Get multiple value from twin prime number A and B
$_eulerPhi = ($_primeA - 1) * ($_primeB - 1); // Get 'Euler phi' value from twin prime number A and B
for($_Loop = 2; $_Loop < $_mPrime; $_Loop++) {
if($_eulerPhi % $_Loop == 0) {
$_epDiv = $_Loop; // Get greatest divisor value from 'Euler phi'
}
}
for($_Loop = 1; $_Loop < $_eulerPhi; $_Loop++) {
if(($_epDiv * $_Loop) % $_eulerPhi == 1) {
$_publicKey = $_Loop;
}
}
return ($_targetCode ^ $_epDiv) % $_mPrime;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment