/    Sign up×
Community /Pin to ProfileBookmark

RSA Encryption in php

ok im trying to create some php functions to do rsa encryption / decription. not on the same page of course.

i got the encryption done like this.

[CODE]function rsaEncrypt($M, $N, $e){
$Me = bcpow($M, $e);
$C = bcmod($Me, $N);
return $C;
}[/CODE]

where $M is the message, $N is the public key (worked out by p*q (p and q are prime numbers)) and $e is a relitive prime to (p-1)(q-1) (Ie. p-1*q-1)

so if we take p as 5 and q as 7 then we get a public key (N) of 35.

a relitive prime of (5-1)(7-1) = 4*6 = 24 is 3 so there we have a value for e.

we shal take a message (M) of 123 for now.

so Me = M^e (Me = M to the power of e)
Me = 123^3 = 1860867.

and therefor C = 35(mod 1860867) = 22

so our encrypted number is 22 from the original one of 123.

equations are:
N = pq
e = φ(p-1)(q-1)

φ means relitive prime i think. but anyway thats what im using it for here. lol.

now ive done the explaining of where i think i am up to time for the problem. if i have got that wrong please someone tell me and point me in the right direction.

i have the encrypted number but i dont understand how to decrypt it.

the equations are:
ed = 1(mod (p-1)(q-1))

so therefor it would be something like
3d = 1(mod(4*6))
3d = 1(mod 24)
and this is where i get stuck how do i get “d” and how do i do “1(mod 24)”

to my understanding 1(mod 24) would be done by

[CODE]$ans = bcmod(1,24);[/CODE]

but this returns 1.

does anyone know what im doing wrong and please could you tell me where or point me in the right direction.

ps. i have already looked at these sites.
[URL=”http://mathcircle.berkeley.edu/BMC3/rsa/node4.html”]http://mathcircle.berkeley.edu/BMC3/rsa/node4.html[/URL]
[URL=”http://en.wikipedia.org/wiki/Rsa”]http://en.wikipedia.org/wiki/Rsa[/URL]
and a few others that i forget right now.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MatMelFeb 01.2007 — Well, as I am not as good(well??) informed about encryption I can only advise you to use the pear class:

http://pear.php.net/package/Crypt_RSA
Copy linkTweet thisAlerts:
@cryptoauthorFeb 05.2007 — mod means moudlus.

1 mod 24 would be 24 / 1 and get the remainder. so 1 mod anything would not work.

7 mod 50 would be 1. Ie. 50/7 = 7 remainder 1.

Here is the whole code.

Ill use 197 and 199 for p and q but they should be at least 100 digits long each.

And yes to work with these 100 or more ditit numbers you for some reason need them inside quotes.

[code=php]
$p = "197";
$q = "199";
$e = "13";
$d = "11941";
$N = bcmul($p,$q);

function rsaEncrypt($M){
global $e;
global $N;
$Me = bcpow($M,$e);
$C = bcmod($Me,$N);
return $C;
}

function rsaDecrypt($C){
global $d;
global $N;
$Cd = bcpow($C,$d);
$M = bcmod($Cd,$N);
return $M;
}
[/code]


[B][I]Note: do not just copy and paste this code on your server you should only have the encryption function with $e and $N $p and $q are only in the above script for reference and $d should only be kept verry secret (Ie. dont write it down, memorise it, or lock it in a safe kind of thing but i think the safe is going a bit too far lol)[/I][/B]

Explanation and equations.

p is a prime number of 100 or more digits.

q is a prime number of 100 or more digits.

e is reletivley prime to (p-1)(q-1). Which means that e and (p-1)(q-1) do not have any of the same prime factors. Eg. in my case
<i>
</i>e = &amp;#966;(p-1)(q-1)
e = &amp;#966;(197-1)(199-1)
e = &amp;#966;(196)(198)
e = &amp;#966;38808
so e could be 13.


d = (x(p-1)(q-1)+1)/e where x is any number that will make the answer of d be an intager.

Im my case:
<i>
</i>d = (4*196*198+1)/13
d = 11641


[B][I]Note: e is worked out by guess work. (Ie. pick a number and see if it works. See if you can encrypt a number then decrypt it useng you guessed value for e and if you get the number in its unencrypted format back and its right then e is correct.)[/I][/B]

N is p multiplyed by q

M is the number you want to encrypt.

C in the encrypted number.

Me in my function is M^e (M to the power of e or M times its selft e amount of times).

Cd in my function is C^d (C to the power of e or C times its self d amount of times)

(now you know why we want e and d to be small lol)

Once you have all these numbers you can then do the encryption and decryption.

To encrypt:

<i>
</i>C = (M^e) mod N


To decrypt:

<i>
</i>M = (C^d) mod N


N and e are your public keys these are what you put in your web page code and give out to people who you want to be able to send you encrypted numbers.

p, q and d are you private keys if you give any one of them out then it makes it easyer for someone to break your encryption.

This encryption only works because the 2 primes p and q are so big that N is a masive number.

(If p and q are both 100 digits long then N will be 200 digits.)

The sun would die befor p and q were worked out if you only had N and e if you put all the computers in the world together (at the moment) and made one masive super computer lol.

If someone got their hands on d then they might be able to work out what p and q are. Since p and q are the only numbers that can be multiplyed together to produce N.

[B][I]Note: To encrypt information that you enter into a web form (like creadit card numbers) it must first be sent unencrypted over the internet from the client browser to the server. To get around this use ssl on the server. This will then basicaly tell the browser to encrypt the infomation then send it then the server decrypts it so your script can work with it ?[/I][/B]

Hope this helps anyone strugeling with this.
×

Success!

Help @crypto spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...