Initialize encryption and JavaScript reverse-engineering study repository
Document the project structure and setup while excluding local dependencies and IDE settings. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
37
非对称加密-2026-08-31/node环境实现rsa加解密.js
Normal file
37
非对称加密-2026-08-31/node环境实现rsa加解密.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// JSEncrypt() node-rsa 两个包
|
||||
// npm install node-rsa
|
||||
|
||||
var node_rsa = require("node-rsa").NodeRSA; // node-rsa 2.x 导出的是 { NodeRSA, default }
|
||||
|
||||
|
||||
class RsaEncryptDecrypt {
|
||||
constructor(key_size = 1024) {
|
||||
this.node_rsa = new node_rsa({b: key_size});
|
||||
this.public_key = this.node_rsa.exportKey("pkcs8-public");
|
||||
this.private_key = this.node_rsa.exportKey("pkcs8-private");
|
||||
|
||||
console.log("公钥:", this.public_key);
|
||||
console.log("私钥", this.private_key);
|
||||
}
|
||||
|
||||
encrypt(data) {
|
||||
const public_instance = new node_rsa(this.public_key, "pkcs8-public");
|
||||
return public_instance.encrypt(data, "base64");
|
||||
}
|
||||
|
||||
decrypt(data) {
|
||||
const private_instance = new node_rsa(this.private_key, "pkcs8-private");
|
||||
return private_instance.decrypt(data, "utf8");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const rsa = new RsaEncryptDecrypt();
|
||||
const source_data = "你好,世界";
|
||||
|
||||
const enc_data = rsa.encrypt(source_data);
|
||||
console.log("base", enc_data);
|
||||
|
||||
const dec_data = rsa.decrypt(enc_data);
|
||||
console.log("解密结果", dec_data);
|
||||
Reference in New Issue
Block a user