forked from zohar1000/in-browser-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
139 lines (125 loc) · 3.89 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Seedcrypt</title>
<style>
.maincontent {
padding-top: 30px;
}
.key {
width: 100%;
}
.text {
margin-top: 1rem;
width: 100%;
}
.buttons {
/** display: flex;
justify-content: space-around;
flex-wrap: wrap-reverse; **/
padding-top: 30px;
}
/** .encrypt-button {
color: white;
background: green;
}
.decrypt-button {
color: white;
background: brown;
}
.encrypt-button, .decrypt-button {
ouline: none;
border: 0;
cursor: pointer;
padding: 5px 10px;
margin-top: 1rem;
}
.encrypt-button:hover, .decrypt-button:hover {
color: black;
background: yellow;
outline: solid 1px #ddd;
} **/
.result-title {
margin-top: 3rem;
}
.result-text {
margin-top: 0;
}
</style>
<script src="https://cdn.swivro.net/crypto-js/3.1.2/rollups/aes.js"></script>
<script src="https://cdn.swivro.net/crypto-js/3.1.2/rollups/pbkdf2.js"></script>
<script>
var keySize = 256;
var ivSize = 128;
var iterations = 100;
function getData() {
var isValid = true;
var key = (document.getElementById('key').value || '').trim();
var text = (document.getElementById('text').value || '').trim();
if (!key || !text) {
isValid = false;
alert('Please enter the required fields.');
}
return { isValid: isValid, key: key, text: text };
}
function encrypt() {
var data = getData();
if (data.isValid) {
// var decrypt = asmCrypto.AES_CBC.encrypt(text, key);
var salt = CryptoJS.lib.WordArray.random(128/8);
var key = CryptoJS.PBKDF2(data.key, salt, {
keySize: keySize/32,
iterations: iterations
});
var iv = CryptoJS.lib.WordArray.random(128/8);
var encrypted = CryptoJS.AES.encrypt(data.text, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
var transitmessage = salt.toString()+ iv.toString() + encrypted.toString();
document.getElementById('result').value = transitmessage;
}
}
function decrypt() {
var data = getData();
if (data.isValid) {
var salt = CryptoJS.enc.Hex.parse(data.text.substr(0, 32));
var iv = CryptoJS.enc.Hex.parse(data.text.substr(32, 32))
var encrypted = data.text.substring(64);
var key = CryptoJS.PBKDF2(data.key, salt, {
keySize: keySize/32,
iterations: iterations
});
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
document.getElementById('result').value = decrypted.toString(CryptoJS.enc.Utf8);
}
}
</script>
<div class="maincontent">
<form class="form">
<!-- <div class="alert alert-danger" role="alert">
<strong>NEVER</strong> share your passphrase with anyone, no matter how much you trust them. If they are compelled by law enforcement to give up your passphrase, your crypto can be stolen.
</div> -->
<div><input id="key" class="key form-control" placeholder="Enter Custom Passphrase" type="password" value></div>
<textarea id="text" class="text form-control" placeholder="Enter Text (either encrypted or decrypted)" type="password"></textarea>
<div class="buttons row">
<div class="col">
<span class="decrypt-button btn btn-danger" onclick="decrypt()">Decrypt Text</span>
</div>
<div class="col">
<span class="encrypt-button btn btn-success" onclick="encrypt();">Encrypt Text</span>
</div>
</div>
<h3 class="result-title">Output:</h3>
<textarea id="result" class="text result-text form-control" readonly></textarea>
</form>
</div>
</body>
</html>