-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp-encrypt.php
90 lines (66 loc) · 2.08 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
<?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 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 ' . letter . ' 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;
}
echo encrypt("pass", "Hello World");
?>