-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdemo.html
More file actions
142 lines (121 loc) · 5.64 KB
/
demo.html
File metadata and controls
142 lines (121 loc) · 5.64 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <script src="https://cdn.jsdelivr.net/gh/magiclen/ts-short-crypt/dist/short-crypt.min.js"></script> -->
<script src="./dist/short-crypt.min.js"></script>
<title>ShortCrypt</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.form-control:read-only {
background-color: #EEE;
}
</style>
</head>
<body class="p-3 p-lg-5">
<div id="demo-short-crypt" class="container">
<div class="row g-3">
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="key" class="form-control" placeholder="key" value="magickey">
<label for="key">key</label>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-header">
Encryption
</div>
<div class="card-body">
<div class="row g-3 align-items-center">
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="plaintext" class="form-control" placeholder="plaintext" value="hello">
<label for="plaintext">plaintext</label>
</div>
</div>
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="resultURLComponent" class="form-control" placeholder="resultURLComponent" readonly>
<label for="resultURLComponent">resultURLComponent</label>
</div>
</div>
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="resultQRCodeAlphanumeric" class="form-control" placeholder="resultQRCodeAlphanumeric" readonly>
<label for="resultQRCodeAlphanumeric">resultQRCodeAlphanumeric</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-header">
Decryption
</div>
<div class="card-body">
<div class="row g-3 align-items-center">
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="cipher" class="form-control" placeholder="cipher" value="T_LVjlIC">
<label for="cipher">cipher (URLComponent or QRCodeAlphanumeric)</label>
</div>
</div>
<div class="col-12">
<div class="form-floating mb-3">
<input type="text" id="resultPlainText" class="form-control" placeholder="resultPlainText" readonly>
<label for="resultPlainText">resultPlainText</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const intputKey = document.getElementById('key');
const intputPlaintext = document.getElementById('plaintext');
const intputResultURLComponent = document.getElementById('resultURLComponent');
const intputResultQRCodeAlphanumeric = document.getElementById('resultQRCodeAlphanumeric');
const intputCipher = document.getElementById('cipher');
const intputResultPlainText = document.getElementById('resultPlainText');
const encrypt = () => {
const key = intputKey.value;
const sc = new ShortCrypt(key);
const plaintext = intputPlaintext.value;
intputResultURLComponent.value = sc.encryptToURLComponent(plaintext);
intputResultQRCodeAlphanumeric.value = sc.encryptToQRCodeAlphanumeric(plaintext);
};
const decrypt = () => {
const key = intputKey.value;
const sc = new ShortCrypt(key);
const cipher = intputCipher.value;
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
try {
intputResultPlainText.value = utf8Decoder.decode(sc.decryptURLComponent(cipher), {});
} catch {
try {
intputResultPlainText.value = utf8Decoder.decode(sc.decryptQRCodeAlphanumeric(cipher));
} catch {
intputResultPlainText.value = '';
intputResultPlainText.classList.add('is-invalid');
return;
}
}
intputResultPlainText.classList.remove('is-invalid');
};
intputKey.addEventListener('input', () => {
encrypt();
decrypt();
});
intputPlaintext.addEventListener('input', encrypt);
intputCipher.addEventListener('input', decrypt);
encrypt();
decrypt();
</script>
</body>
</html>