-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#93 ZOrderEncryption.js
More file actions
61 lines (57 loc) · 1.33 KB
/
#93 ZOrderEncryption.js
File metadata and controls
61 lines (57 loc) · 1.33 KB
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
//Z Order Curve
function getSquare(string) {
var width = 2,
square = 0,
length = string.length;
while (square < length) {
width += width;
square = width * width;
}
return width;
}
function zOrder(pos, width) {
var FLAG_A = 0x1,
// 0001
FLAG_B = 0x2,
// 0010
count = 1,
x = 0,
y = 0;
while (pos > 0) {
if (pos & FLAG_A) {
x += count;
}
if (pos & FLAG_B) {
y += count;
}
pos = pos >> 2;
count += count;
}
var newPos = y * width + x;
return newPos;
}
function decrypt() {
var input = prompt('Please inupt your string to decrypt');
var width = getSquare(input),
output = [],
square = width * width;
for (i = 0; i < square; i++) {
var newI = zOrder(i, width);
output[i] = input.substr(newI, 1);;
}
return output.join('');
}
function encrypt() {
var input = prompt('Please input your string to encrypt');
var output = [];
var width = getSquare(input);
var square = width * width;
for (i = 0; i < square; i++) {
var newI = zOrder(i, width);
var letter = input.substr(i, 1);
output[newI] = (letter) ? letter : ' ';
}
return output.join('');
}
alert(encrypt());
alert(decrypt());