forked from warodriuk/jsphpcypher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-encrypt-decrypt.html
181 lines (127 loc) · 5.09 KB
/
js-encrypt-decrypt.html
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script>
///////////////////
// DECRYPT
//////////////////
function decrypt(password, text) {
// convert the string to decrypt into an array
const arr = text.split('');
// let's also create an array from our password
const arrPass = password.split('');
// keep control about which letter from the password we use
let lastPassLetter = 0;
// this is the final decrypted string
let decrypted = '';
// let's start...
for (let i=0; i < arr.length; i++) {
// next letter from the string to decrypt
const letter = arr[ i ];
// get the next letter from the password
const passwordLetter = arrPass[ lastPassLetter ];
// get the decrypted letter according to the password
const temp = getInvertedLetterFromAlphabetForLetter( passwordLetter, letter );
if (temp) {
// concat the response
decrypted += temp;
} else {
// if any error, return null
return null;
}
// if our password is too short, let's start again from the first letter
if (lastPassLetter == (arrPass.length - 1) ) {
lastPassLetter = 0;
} else {
lastPassLetter ++;
}
}
// return the decrypted string and converted from base64 to plain text
return atob( decrypted );
}
function getInvertedLetterFromAlphabetForLetter(letter, letterToChange) {
const abc = 'abcdefghijklmnopqrstuvwxyz0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const posLetter = abc.indexOf( letter );
if (posLetter == -1) {
console.log('Password letter ' + letter + ' not allowed.');
return null;
}
const part1 = abc.substring(posLetter, abc.length);
const part2 = abc.substring(0, posLetter);
const newABC = '' + part1 + '' + part2;
const posLetterToChange = newABC.indexOf( letterToChange );
if (posLetterToChange == -1) {
console.log('Password letter ' + letter + ' not allowed.');
return null;
}
const letterAccordingToAbc = abc.split('')[ posLetterToChange ];
return letterAccordingToAbc;
}
///////////////////
// ENCRYPT
//////////////////
function encrypt(password, text) {
// move text to base64
const base64 = btoa( text );
// text string to array
const arr = base64.split('');
// arr of password
const arrPass = password.split('');
let lastPassLetter = 0;
// encrypted string
let encrypted = '';
// encrypt
for (let i=0; i < arr.length; i++) {
const letter = arr[ i ];
const passwordLetter = arrPass[ lastPassLetter ];
const temp = getLetterFromAlphabetForLetter( passwordLetter, letter );
if (temp) {
// 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 == (arrPass.length - 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
const abc = 'abcdefghijklmnopqrstuvwxyz0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// get the position of the given letter, according to our abc
const posLetter = abc.indexOf( letter );
// if we cannot get it, then we can't continue
if (posLetter == -1) {
console.log('Password letter ' + letter + ' not allowed.');
return null;
}
// according to our abc, get the position of the letter to encrypt
const posLetterToChange = abc.indexOf( letterToChange );
// again, if any error, we cannot continue...
if (posLetterToChange == -1) {
console.log('Password letter ' + letter + ' not allowed.');
return null;
}
// let's build the new abc. this is the important part
const part1 = abc.substring(posLetter, abc.length);
const part2 = abc.substring(0, posLetter);
const newABC = '' + part1 + '' + part2;
// we get the encrypted letter
const letterAccordingToAbc = newABC.split('')[ posLetterToChange ];
// and return to the routine...
return letterAccordingToAbc;
}
const text = 'Hello World';
const password = "pass";
const e = encrypt(password, text);
console.log('encrypted: ' + e);
const d = decrypt('pass', e);
console.log('decrypted: ' + d);
</script>