-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathslow-rsa.js
More file actions
39 lines (32 loc) · 1.17 KB
/
slow-rsa.js
File metadata and controls
39 lines (32 loc) · 1.17 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
import "../../libs_drpy/jsencrypt.js"
export const RSA = {
decode(data, key, option = {}) {
if (typeof JSEncrypt !== 'function') return false;
const privateKey = this.getPrivateKey(key);
const decryptor = new JSEncrypt();
decryptor.setPrivateKey(privateKey);
return decryptor.decryptUnicodeLong(data);
},
encode(data, key, option = {}) {
if (typeof JSEncrypt !== 'function') return false;
const publicKey = this.getPublicKey(key);
const encryptor = new JSEncrypt();
encryptor.setPublicKey(publicKey);
return encryptor.encryptUnicodeLong(data);
},
fixKey(key, prefix, endfix) {
if (!key.includes(prefix)) key = prefix + key;
if (!key.includes(endfix)) key += endfix;
return key;
},
getPrivateKey(key) {
const prefix = '-----BEGIN RSA PRIVATE KEY-----';
const endfix = '-----END RSA PRIVATE KEY-----';
return this.fixKey(key, prefix, endfix);
},
getPublicKey(key) {
const prefix = '-----BEGIN PUBLIC KEY-----';
const endfix = '-----END PUBLIC KEY-----';
return this.fixKey(key, prefix, endfix);
}
};