-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.html
177 lines (157 loc) · 5.27 KB
/
testing.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Private Key Debugging</title>
<script>
class KeyPair {
constructor() {
this.publicKey = null;
this.privateKey = null;
}
async generateKeyPair() {
try {
const keyPair = await window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["encrypt", "decrypt"]
);
this.publicKey = await window.crypto.subtle.exportKey(
"spki",
keyPair.publicKey
);
this.privateKey = await window.crypto.subtle.exportKey(
"pkcs8",
keyPair.privateKey
);
} catch (error) {
console.error("Error generating key pair:", error);
}
}
async encrypt(data) {
try {
const publicKey = await window.crypto.subtle.importKey(
"spki",
this.publicKey,
{
name: "RSA-OAEP",
hash: "SHA-256",
},
true,
["encrypt"]
);
const ciphertext = await window.crypto.subtle.encrypt(
{
name: "RSA-OAEP",
},
publicKey,
new TextEncoder().encode(data)
);
return KeyPair.arrayBufferToHex(ciphertext);
} catch (error) {
console.error("Error encrypting data:", error);
}
}
static async encryptWithPublicKey(publicKey, data) {
try {
const importedPublicKey = await window.crypto.subtle.importKey(
"spki",
KeyPair.hexToArrayBuffer(publicKey),
{
name: "RSA-OAEP",
hash: "SHA-256",
},
true,
["encrypt"]
);
const ciphertext = await window.crypto.subtle.encrypt(
{
name: "RSA-OAEP",
},
importedPublicKey,
new TextEncoder().encode(data)
);
return KeyPair.arrayBufferToHex(ciphertext);
} catch (error) {
console.error("Error encrypting data with public key:", error);
}
}
async decrypt(encryptedData) {
try {
const privateKey = await window.crypto.subtle.importKey(
"pkcs8",
this.privateKey,
{
name: "RSA-OAEP",
hash: "SHA-256",
},
true,
["decrypt"]
);
const decryptedData = await window.crypto.subtle.decrypt(
{
name: "RSA-OAEP",
},
privateKey,
KeyPair.hexToArrayBuffer(encryptedData)
);
return new TextDecoder().decode(decryptedData);
} catch (error) {
console.error("Error decrypting data:", error);
}
}
exportHexKeys() {
const hexPublicKey = KeyPair.arrayBufferToHex(this.publicKey);
const hexPrivateKey = KeyPair.arrayBufferToHex(this.privateKey);
return { publicKey: hexPublicKey, privateKey: hexPrivateKey };
}
importHexKeys(hexPublicKey, hexPrivateKey) {
this.publicKey = this.hexToArrayBuffer(hexPublicKey);
this.privateKey = this.hexToArrayBuffer(hexPrivateKey);
}
static arrayBufferToHex(buffer) {
const view = new DataView(buffer);
let hexString = "";
for (let i = 0; i < view.byteLength; i++) {
const byte = view.getUint8(i);
hexString += byte.toString(16).padStart(2, "0");
}
return hexString;
}
static hexToArrayBuffer(hexString) {
const buffer = new ArrayBuffer(hexString.length / 2);
const view = new DataView(buffer);
for (let i = 0; i < hexString.length; i += 2) {
const byte = parseInt(hexString.substr(i, 2), 16);
view.setUint8(i / 2, byte);
}
return buffer;
}
}
// Example Usage:
const keyPair = new KeyPair();
keyPair.generateKeyPair().then(async () => {
const { publicKey, privateKey } = keyPair.exportHexKeys();
console.log("Public Key:", publicKey);
console.log("Private Key:", privateKey);
const message = "Hello World!";
const encryptedMessage = await keyPair.encrypt(message);
console.log("Encrypted Message:", encryptedMessage);
const decryptedMessage = await keyPair.decrypt(encryptedMessage);
console.log("Decrypted Message:", decryptedMessage);
KeyPair.encryptWithPublicKey(publicKey, message).then(async (m) => {
console.log("Encrypted Message:", m);
const d = await keyPair.decrypt(m, privateKey);
console.log("Decrypted Message:", d);
})
});
</script>
</head>
<body></body>
</html>