Onlife/common/jiemi.js

71 lines
2.5 KiB
JavaScript
Raw Normal View History

2025-04-19 15:38:48 +08:00
const crypto = require('crypto');
const NodeRSA = require('node-rsa');
import DB from "@/common/sqlite";
// 解密助记词AES + RSA 解密)
function decryptMnemonicWithAES(encryptedData, password) {
try {
// 使用 node-rsa 解密 RSA 加密的数据
const rsaKey = new NodeRSA(encryptedData.privateKey);
// 将十六进制字符串转换为Buffer再解密
const buffer = Buffer.from(encryptedData.rsaEncryptedMnemonic, 'hex');
const decryptedWithRSA = rsaKey.decrypt(buffer, 'utf8');
// AES解密
const aesKey = crypto.createHash('sha256').update(password).digest();
const iv = Buffer.from(encryptedData.iv, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
let decrypted = decipher.update(decryptedWithRSA, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
} catch (error) {
console.error("解密过程中出错:", error);
throw new Error("解密失败: " + error.message);
}
}
export const publicjiemi = async(type, password, moneyAdress) => {
try {
DB.openSqlite();
const searchCondition = `WHERE moneyAdress = '${moneyAdress}'`;
const searchResulets = await DB.selectTableData(DB.regTable, searchCondition);
if (!searchResulets || searchResulets.length === 0) {
throw new Error("未找到匹配的记录");
}
let encryptedData = {
rsaEncryptedMnemonic: "",
privateKey: "",
iv: ""
};
if(type == 1) {
encryptedData = {
rsaEncryptedMnemonic: searchResulets[0].mnemonic,
privateKey: searchResulets[0].privateKeyMne,
iv: searchResulets[0].mnemonicIV
}
}
if(type == 2) {
encryptedData = {
rsaEncryptedMnemonic: searchResulets[0].privateKeyMoney,
privateKey: searchResulets[0].privateKeyPre,
iv: searchResulets[0].privateIV
}
}
// 检查数据完整性
if (!encryptedData.rsaEncryptedMnemonic || !encryptedData.privateKey || !encryptedData.iv) {
throw new Error("加密数据不完整");
}
// 解密
const decryptedMnemonic = decryptMnemonicWithAES(encryptedData, password);
return decryptedMnemonic;
} catch (error) {
console.error("解密过程出错:", error);
throw error;
}
};