nodejs:encryption/decryption 的节点模块?
nodejs: node module for encryption/decryption?
我希望网站 (A) 仅在访问者来自其他特定网站 (B) 时才有效。
最初,我想到了使用document.referrer
,但显然它很容易被伪造。猜猜,我别无选择,只能将密钥从网站 A 传递到网站 B。
node
服务器上的两个网站 运行。我已经传递了两个参数(比如说 param1
n param2
),所以我认为这个键,以及一些在两个站点之间共享的公共字符串(认为这叫做 private key
)会产生一些在站点 A 上排序加密代码,将其添加为第三个参数并发送到站点 B。
在站点 B 中,我将解密第三个参数(使用 private key
)并检查它是否与 param1
和 param2
匹配,然后允许通过其他 return 401
。
我以前从未做过这样的事情(与安全相关的事情),这种方法正确吗?是否有任何节点模块已经提供了这种加密和解密?最后,关于创建私钥有什么建议吗?
我写了一个 article on how to accomplish this using crypto nodejs 模块:
aes_encryption.js:
var crypto = require('crypto'),
cipher_seed = 'some_random_characters';
var encrypt = function(text) {
var cipher = crypto.createCipher('aes-256-cbc', cipher_seed),
crypted = cipher.update(text, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
};
var decrypt = function(text) {
var decipher = crypto.createDecipher('aes-256-cbc', cipher_seed),
decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
};
module.exports.encrypt = encrypt;
module.exports.decrypt = decrypt;
用法:
var AES = require('path_to_aes_encryption.js');
var data = {
name: 'Catalin',
surname: 'Munteanu',
address: 'Romania'
};
// Encryption
var encrypted_data = AES.encrypt(JSON.stringify(data));
// Decryption
var decrypted_data = JSON.parse(AES.decrypt(encrypted_data));
如果您计划在一台服务器上进行加密并在另一台服务器上进行解密,请不要忘记使用相同的 cipher_seed
。
我希望网站 (A) 仅在访问者来自其他特定网站 (B) 时才有效。
最初,我想到了使用document.referrer
,但显然它很容易被伪造。猜猜,我别无选择,只能将密钥从网站 A 传递到网站 B。
node
服务器上的两个网站 运行。我已经传递了两个参数(比如说 param1
n param2
),所以我认为这个键,以及一些在两个站点之间共享的公共字符串(认为这叫做 private key
)会产生一些在站点 A 上排序加密代码,将其添加为第三个参数并发送到站点 B。
在站点 B 中,我将解密第三个参数(使用 private key
)并检查它是否与 param1
和 param2
匹配,然后允许通过其他 return 401
。
我以前从未做过这样的事情(与安全相关的事情),这种方法正确吗?是否有任何节点模块已经提供了这种加密和解密?最后,关于创建私钥有什么建议吗?
我写了一个 article on how to accomplish this using crypto nodejs 模块:
aes_encryption.js:
var crypto = require('crypto'),
cipher_seed = 'some_random_characters';
var encrypt = function(text) {
var cipher = crypto.createCipher('aes-256-cbc', cipher_seed),
crypted = cipher.update(text, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
};
var decrypt = function(text) {
var decipher = crypto.createDecipher('aes-256-cbc', cipher_seed),
decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
};
module.exports.encrypt = encrypt;
module.exports.decrypt = decrypt;
用法:
var AES = require('path_to_aes_encryption.js');
var data = {
name: 'Catalin',
surname: 'Munteanu',
address: 'Romania'
};
// Encryption
var encrypted_data = AES.encrypt(JSON.stringify(data));
// Decryption
var decrypted_data = JSON.parse(AES.decrypt(encrypted_data));
如果您计划在一台服务器上进行加密并在另一台服务器上进行解密,请不要忘记使用相同的 cipher_seed
。