在Node.js中使用node-rsa加密数据,可以按照以下步骤进行:
const NodeRSA = require('node-rsa');
const key = new NodeRSA({ b: 512 }); // 选择合适的密钥长度
const publicKey = key.exportKey('public');
const privateKey = key.exportKey('private');
const encryptedData = key.encrypt('要加密的数据', 'base64');
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;
import android.util.Base64;
public class RSAUtils {
private static final String PRIVATE_KEY = "这里填写你的私钥";
public static String decryptData(String encryptedData) {
try {
byte[] privateKeyBytes = Base64.decode(PRIVATE_KEY, Base64.DEFAULT);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedBytes = Base64.decode(encryptedData, Base64.DEFAULT);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
decryptData
方法,传入加密的数据进行解密操作。需要注意的是,以上代码仅为示例,实际使用时需要根据具体情况进行适当的修改和优化。
关于node-rsa的更多信息和使用方法,可以参考腾讯云的产品介绍链接地址:node-rsa
领取专属 10元无门槛券
手把手带您无忧上云