-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryption.js
More file actions
125 lines (88 loc) · 3.52 KB
/
Copy pathcryption.js
File metadata and controls
125 lines (88 loc) · 3.52 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
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const secretKey = 'secretKey'
const text = 'سلام بر روح پرفتوح عضما'
const generatekeypare = (secretKey)=>{
let genNewPubPrivKeys =crypto.generateKeyPair;
genNewPubPrivKeys('rsa',{
modulusLength:2048,
publicKeyEncoding:{type:'spki',format:'pem'},
privateKeyEncoding:{type:'pkcs8',format:'pem',cipher:'aes-256-cbc',passphrase:"secretKey"},
},(err,publicKey,privateKey)=>{
console.log(publicKey)
console.log(privateKey)
fs.writeFileSync('peivate.pem',privateKey)
fs.writeFileSync('public.pem',publicKey)
})
}
//generatekeypare(secretKey)
const encriptByPublic =(toEncrypt,addreesPublicKeyFile)=>{
const publicKey =fs.readFileSync(path.join(__dirname,addreesPublicKeyFile),'utf8')
const buffer = Buffer.from(toEncrypt)
const encrypted = crypto.publicEncrypt(publicKey,buffer);
return encrypted.toString('base64')
};
const enc = encriptByPublic(text,`public.pem`)
// console.log(x)
const decryptByPrivate = (toDencrypt,addressPrivateKeyFile)=>{
const privateKey = fs.readFileSync(path.join(__dirname,addressPrivateKeyFile),'utf8')
const buffer = Buffer.from(toDencrypt,'base64')
const decrypted = crypto.privateDecrypt({
key: privateKey.toString(),
passphrase:secretKey},
buffer)
return decrypted.toString('utf8')
}
const y = decryptByPrivate(enc,`peivate.pem`)
console.log(y)
// var crypto = require("crypto");
// var path = require("path");
// var fs = require("fs");
// const passphrase = "mySecret"
// var encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
// var absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
// var publicKey = fs.readFileSync(absolutePath, "utf8");
// var buffer = new Buffer(toEncrypt);
// var encrypted = crypto.publicEncrypt(publicKey, buffer);
// return encrypted.toString("base64");
// };
// var decryptStringWithRsaPrivateKey = function(toDecrypt, relativeOrAbsolutePathtoPrivateKey) {
// var absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
// var privateKey = fs.readFileSync(absolutePath, "utf8");
// var buffer = new Buffer(toDecrypt, "base64");
// //var decrypted = crypto.privateDecrypt(privateKey, buffer);
// const decrypted = crypto.privateDecrypt(
// {
// key: privateKey.toString(),
// passphrase: passphrase,
// },
// buffer,
// )
// return decrypted.toString("utf8");
// };
// const { writeFileSync } = require('fs')
// const { generateKeyPairSync } = require('crypto')
// function generateKeys() {
// const { publicKey, privateKey } = generateKeyPairSync('rsa',
// {
// modulusLength: 4096,
// namedCurve: 'secp256k1',
// publicKeyEncoding: {
// type: 'spki',
// format: 'pem'
// },
// privateKeyEncoding: {
// type: 'pkcs8',
// format: 'pem',
// cipher: 'aes-256-cbc',
// passphrase: passphrase
// }
// });
// writeFileSync('private.pem', privateKey)
// writeFileSync('public.pem', publicKey)
// }
// generateKeys();
// let a = encryptStringWithRsaPublicKey("hello", "public.pem")
// let b = decryptStringWithRsaPrivateKey(a, "private.pem");
// console.log(b)