js RAS 2048 加密代码
以下是一个使用RSA 2048位加密算法的示例代码:
const crypto = require('crypto');
// 生成RSA密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
}
});
// 要加密的明文
const plaintext = 'Hello, World!';
// 使用公钥加密明文
const encrypted = crypto.publicEncrypt(publicKey, Buffer.from(plaintext, 'utf-8'));
console.log('加密后的数据:', encrypted.toString('base64'));
// 使用私钥解密密文
const decrypted = crypto.privateDecrypt(privateKey, encrypted);
console.log('解密后的数据:', decrypted.toString('utf-8'));
注意:在运行此代码之前,确保已经安装了Node.js环境,并在代码中使用crypto模块进行加密操作
原文地址: https://www.cveoy.top/t/topic/iR7d 著作权归作者所有。请勿转载和采集!