forked from warodriuk/jsphpcypher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-encrypt.php
137 lines (112 loc) · 3.66 KB
/
php-encrypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
function encrypt($password, $text) {
// move text to base64
$base64 = base64_encode( $text );
// text string to array
$arr = str_split($base64);
// arr of password
$arrPass = str_split($password);
$lastPassLetter = 0;
// encrypted string
$encrypted = '';
// encrypt
for ($i=0; $i < sizeof( $arr ); $i++) {
$letter = $arr[ $i ];
$passwordLetter = $arrPass[ $lastPassLetter ];
$temp = getLetterFromAlphabetForLetter( $passwordLetter, $letter );
if ($temp != null) {
// concat to the final response encrypted string
$encrypted .= $temp;
} else {
// if any error, return null
return null;
}
/*
This is important: if we're out of letters in our
password, we need to start from the begining.
*/
if ($lastPassLetter == ( sizeof( $arrPass ) - 1) ) {
$lastPassLetter = 0;
} else {
$lastPassLetter ++;
}
}
// We finally return the encrypted string
return $encrypted;
}
function decrypt($password, $text) {
$lastPassLetter = 0;
// this is the final decrypted string
$decrypted = '';
// let's start...
for ($i = 0; $i < strlen($text); $i++) {
// next letter from the string to decrypt
$letter = $text[$i];
// get the next letter from the password
$passwordLetter = $password[$lastPassLetter];
// get the decrypted letter according to the password
$temp = getInvertedLetterFromAlphabetForLetter($passwordLetter, $letter);
// concat the response
$decrypted .= $temp;
// if our password is too short,
// let's start again from the first letter
if ($lastPassLetter == strlen($password) - 1) {
$lastPassLetter = 0;
} else {
$lastPassLetter++;
}
}
// return the decrypted string and converted
// from base64 to plain text
return base64_decode($decrypted);
}
function getLetterFromAlphabetForLetter( $letter, $letterToChange) {
// this is the alphabet we know, plus numbers and the = sign
$abc = 'abcdefghijklmnopqrstuvwxyz0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// get the position of the given letter, according to our abc
$posLetter = strpos( $abc, $letter );
// if we cannot get it, then we can't continue
if ($posLetter === false) {
echo 'Password letter ' . $letter . ' not allowed.';
return null;
}
// according to our abc, get the position of the letter to encrypt
$posLetterToChange = strpos( $abc, $letterToChange );
// again, if any error, we cannot continue...
if ($posLetterToChange == false) {
echo 'Password letter ' . $letterToChange . ' not allowed.';
return null;
}
// let's build the new abc. this is the important part
$part1 = substr( $abc, $posLetter, strlen( $abc ) );
$part2 = substr( $abc, 0, $posLetter);
$newABC = '' . $part1 . '' . $part2;
// we get the encrypted letter
$temp = str_split($newABC);
$letterAccordingToAbc = $temp[ $posLetterToChange ];
// and return to the routine...
return $letterAccordingToAbc;
}
function getInvertedLetterFromAlphabetForLetter($letter, $letterToChange) {
// this is the alphabet we know, plus numbers and the = sign
$abc = 'abcdefghijklmnopqrstuvwxyz0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$posLetter = strpos($abc, $letter);
if ($posLetter === false) {
echo 'Password letter ' . $letter . ' not allowed.';
return null;
}
$part1 = substr($abc, $posLetter);
$part2 = substr($abc, 0, $posLetter);
$newABC = $part1 . $part2;
$posLetterToChange = strpos($newABC, $letterToChange);
if ($posLetterToChange === false) {
echo 'Password letter ' . $letterToChange . ' not allowed.';
return null;
}
$letterAccordingToAbc = $abc[$posLetterToChange];
return "$letterAccordingToAbc";
}
$encrypted = encrypt("password", "Hello World");
echo $encrypted."\n";
$plain = decrypt("password", $encrypted);
echo $plain."\n";