001.
<?php
002.
$plaintext
=
"Helloworld"
;
003.
echo
encrypt_RSA(
$plaintext
);
004.
005.
function
encrypt_RSA(
$text
)
006.
{
007.
$a
= [];
008.
$b
= [];
009.
$p
= 0;
010.
$q
= 0;
011.
while
(true) {
012.
$p
= rand(1, 30);
013.
if
(findPrime(
$p
) == true) {
014.
break
;
015.
}
016.
}
017.
018.
while
(true) {
019.
$q
= rand(1,30);
020.
if
(findPrime(
$q
) == true) {
021.
break
;
022.
}
023.
}
024.
025.
$n
=
$p
*
$q
;
026.
while
(true) {
027.
$n1
= ((
$p
- 1) * (
$q
- 1));
028.
$e
= rand(1,
$n1
);
029.
030.
if
(gcd(
$e
,
$n1
) == 1) {
031.
break
;
032.
}
033.
}
034.
035.
echo
"Public key: "
.
$n
.
" , "
.
$e
;
036.
print(
"\n"
);
037.
038.
for
(
$i
= 0;
$i
<
$n1
- 1;
$i
++) {
039.
if
((
$i
*
$e
) %
$n1
== 1) {
040.
(float)
$d
=
$i
;
041.
break
;
042.
}
043.
}
044.
echo
"Private key: "
.
$d
;
045.
046.
047.
for
(
$i
= 0;
$i
<
strlen
(
$text
);
$i
++) {
048.
$t
= toNum(
$text
[
$i
]);
049.
$t
= myPowMod(
$t
,
$e
,
$n
);
050.
array_push
(
$a
,
$t
);
051.
052.
$t
= toText(
$t
);
053.
array_push
(
$b
,
chr
(
$t
));
054.
}
055.
056.
for
(
$x
= 0;
$x
<
count
(
$a
);
$x
++) {
057.
echo
$a
[
$x
] .
""
;
058.
}
059.
060.
print(
"\n"
);
061.
for
(
$x
= 0;
$x
<
count
(
$b
);
$x
++) {
062.
echo
$b
[
$x
] .
""
;
063.
}
064.
print(
"\n"
);
065.
$a
= null;
066.
$b
= null;
067.
}
068.
069.
function
decrypt_RSA(
$text
)
070.
{
071.
$a
= [];
072.
$b
= [];
073.
$p
= 0;
074.
$q
= 0;
075.
$textLength
=
strlen
(
$text
);
076.
$textN
=[];
077.
while
(true) {
078.
$p
= rand(1, 50);
079.
if
(findPrime(
$p
) == true) {
080.
break
;
081.
}
082.
}
083.
084.
while
(true) {
085.
$q
= rand(1, 50);
086.
if
(findPrime(
$q
) == true) {
087.
break
;
088.
}
089.
}
090.
091.
$n
=
$p
*
$q
;
092.
while
(true) {
093.
$n1
= (
$p
- 1) * (
$q
- 1);
094.
$e
= rand(1,
$n1
);
095.
096.
if
(gcd(
$e
,
$n1
) == 1) {
097.
break
;
098.
}
099.
}
100.
echo
"Public key: "
.
$n
.
" , "
.
$e
;
101.
print(
"\n"
);
102.
103.
for
(
$i
= 0;
$i
<
$n1
- 1;
$i
++) {
104.
if
((
$i
*
$e
) %
$n1
== 1) {
105.
(float)
$d
=
$i
;
106.
break
;
107.
}
108.
}
109.
echo
"Private key: "
.
$d
;
110.
$textLength
=
strlen
(
$text
);
111.
$textN
=[];
112.
113.
for
(
$i
= 0;
$i
<
strlen
(
$text
);
$i
++) {
114.
$t
= toNum(
$text
[
$i
]);
115.
$t
= myPowMod(
$t
,
$e
,
$n
);
116.
$t
= toText(
$t
);
117.
echo
chr
(
$t
);
118.
119.
}
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
return
0;
131.
}
132.
133.
134.
function
toNum(
$a
)
135.
{
136.
$a
= ord(
$a
) - (ord(
'z'
) - ord(
'a'
));
137.
return
$a
;
138.
}
139.
140.
141.
function
toText(
$a
)
142.
{
143.
$a
=
$a
+ (ord(
'z'
) - ord(
'a'
));
144.
return
$a
;
145.
}
146.
147.
148.
149.
function
myPowMod(
$a
,
$b
,
$c
)
150.
{
151.
152.
$x
= 1;
153.
$a
=
$a
%
$c
;
154.
if
(
$a
== 0) {
155.
return
0;
156.
}
157.
while
(
$b
> 0) {
158.
if
((
$b
& 1) == 1) {
159.
$x
= (
$x
*
$a
) %
$c
;
160.
}
161.
162.
$b
= (int)(
$b
/ 2);
163.
$a
= (
$a
*
$a
) %
$c
;
164.
}
165.
return
$x
;
166.
}
167.
168.
function
gcd(
$a
,
$b
)
169.
{
170.
if
(
$a
>
$b
) {
171.
$small
=
$b
;
172.
}
else
{
173.
$small
=
$a
;
174.
}
175.
for
(
$i
= 1;
$i
<
$small
+ 1;
$i
++) {
176.
if
((
$a
%
$i
== 0) && (
$b
%
$i
== 0)) {
177.
$gcd
=
$i
;
178.
}
179.
}
180.
181.
182.
return
$gcd
;
183.
}
184.
185.
186.
function
findPrime(
$a
)
187.
{
188.
for
(
$i
= 2;
$i
<
$a
- 1;
$i
++) {
189.
if
(
$a
%
$i
== 0) {
190.
return
0;
191.
}
192.
}
193.
return
1;
194.
}