-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgp.js
99 lines (89 loc) · 4.04 KB
/
pgp.js
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
/**
* plaintext: message to encrypt
* pubkey: public key of recipient
* passphrase: passphrase for my private key
*
* Returns promise with ASCII armored PGP message
*/
var pgpEncrypt = function(plaintext, pubkey, passphrase) {
var options = {
data: plaintext, // input as String (or Uint8Array)
publicKeys: openpgp.key.readArmored(pubkey).keys // for encryption
};
return openpgp.encrypt(options).then(function(ciphertext) {
return ciphertext.data;
});
};
/**
* ciphertext: ASCII armored PGP message
* privkey: my private key
* passphrase: passphrase for my private key
*
* Returns promise with plaintext string
*/
var pgpDecrypt = function(ciphertext, privkey, passphrase) {
var privKeyObj = openpgp.key.readArmored(privkey).keys[0];
privKeyObj.decrypt(passphrase);
var options = {
message: openpgp.message.readArmored(ciphertext), // parse armored message
privateKey: privKeyObj // for decryption
};
return openpgp.decrypt(options).then(function(plaintext) {
return plaintext.data; // 'Hello, World!'
});
};
$(function() {
var pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG C# v1.6.1.0
mQENBFj6XP0BCACbZFDa9jnLGqATZMpQRrYgyC/KSEbp6xHfKthWODaWeDu7WADV
c8vwoF7/lp1CkB/tJsNl0gz3cpJ8B564bnnyHleMLj1ZUPbc1uNOGe4250nv27nr
SwmmdEGjM6W+J8S0nC0AmSsv0umcrYHEEAzw2f3Mc0Xa/LokBrWRJTScw/SYQ87L
nxzMxvnViHhFye+kkzSx5EDlJ+mV2znRXFOlM6mCBhO10oLz5KsjRnetx2C4bMHp
0Hi9ja+B1dNYTr9mUcfCMQI2CZniIbzspmIy5yfsk5gs/0egndAwnauXc3QgEOrV
i91WFHk7F26ZoAtnq09vy1tOjgVBmLObxCrlABEBAAG0AIkBHAQQAQIABgUCWPpc
/QAKCRB5jY1P3TaX2wbiB/0edjkRN3fxDJN62FHtnUhFJ9jpQ1wT59r9fzJWEpOf
heeseE2l7ys4d5JG6eHXSof48T+y1rFVujBJu6avV8zdZc/LEVBFXg88IKl3uzA+
H2/YmeapyoCODqxBPx3/+95Bz+SLfPtBmthluFW3NXMVYxvPv/nqmbDkuifXkbLm
/TnPWCQpBEjh4a3f+LOmytmRBvrWQ08GdYujgto9vUG+JpkyFsRccp1mzK6t4OEY
dbeeQt2oA/8RBKmyaylak47PszZHcpyZyV8Ug9Q0d+QYYSJqMch2uqgp3w5FFk5z
fey4nbl056mu3PqmMrbOVWNClP5DjTqjm4q8MVoCcT8F
=xRD9
-----END PGP PUBLIC KEY BLOCK-----`;
var privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG C# v1.6.1.0
lQOsBFj6XP0BCACbZFDa9jnLGqATZMpQRrYgyC/KSEbp6xHfKthWODaWeDu7WADV
c8vwoF7/lp1CkB/tJsNl0gz3cpJ8B564bnnyHleMLj1ZUPbc1uNOGe4250nv27nr
SwmmdEGjM6W+J8S0nC0AmSsv0umcrYHEEAzw2f3Mc0Xa/LokBrWRJTScw/SYQ87L
nxzMxvnViHhFye+kkzSx5EDlJ+mV2znRXFOlM6mCBhO10oLz5KsjRnetx2C4bMHp
0Hi9ja+B1dNYTr9mUcfCMQI2CZniIbzspmIy5yfsk5gs/0egndAwnauXc3QgEOrV
i91WFHk7F26ZoAtnq09vy1tOjgVBmLObxCrlABEBAAH/AwMCbT4Ps2DpaGxg6flo
o92JzZqjmkEaSDblW/kOwjL6ocYWxbqX/UrbCPpBZHghhi+E6B0YN67KEKkuiGXs
QX2yjWUv4/QD9veH34VnfpGRvaHlguk01ARmjxxFW8DT4s1udAScHLIm1ED1iVn3
MVsb5UfENck1Xx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JXxSD1DR35BhhImoxyHa6qCnfDkUWTnN9
7LiduXTnqa7c+qYyts5VY0KU/kONOqObirwxWgJxPwU=
=0hsu
-----END PGP PRIVATE KEY BLOCK-----`;
var passphrase = ''; //what the privKey is encrypted with
pgpEncrypt('Hello world 你好', pubkey, passphrase)
.then(function(ciphertext) {
return pgpDecrypt(ciphertext, privkey, passphrase);
})
.then(function(plaintext) {
});
});