新版仿ok交易所-后端
peter
2025-10-12 08386c031119ce38a66e84335042e3f26075e96d
trading-order-security-common/src/main/java/com/yami/trading/security/common/util/LocalKeyStorageAESUtil.java
@@ -48,8 +48,6 @@
                return Result.failed("密钥文件丢失,请联系管理员");
            }
//            //重置密钥   客户更新完密码之后要重新打包提交
//            reset(password);
            // 2. 加载现有密钥材料
            KeyMaterial existingKey = loadKeyMaterial();
@@ -231,81 +229,4 @@
        return bytes;
    }
    public static void reset(String password) throws Exception {
        // 生成新的salt和密钥
        byte[] newSalt = generateRandomBytes(SALT_LENGTH);
        SecretKey newSecretKey = deriveKey(password.toCharArray(), newSalt);
        // 重新加密所有现有文件
        List<CryptoCurrencyEnum> currencyEnums = CryptoCurrencyEnum.getAll();
        for (CryptoCurrencyEnum currency : currencyEnums) {
            Path encryptedDataFile = getEncryptedDataFile(FILE, currency.getName(), FILENAME );
            if (Files.exists(encryptedDataFile)) {
                try {
                    // 读取原有加密数据
                    String encryptedContent = new String(Files.readAllBytes(encryptedDataFile), StandardCharsets.UTF_8);
                    String[] parts = encryptedContent.split(":");
                    if (parts.length == 2) {
                        //获取地址
                        String address = loadAndDecrypt(currency.getName());
                        // 使用新密钥重新加密
                        byte[] newIv = generateRandomBytes(IV_LENGTH);
                        String newCiphertext = encryptData(address, newSecretKey, newIv);
                        String newEncryptedData = Base64.getEncoder().encodeToString(newIv) + ":" + newCiphertext;
                        // 写回文件
                        Files.write(encryptedDataFile, newEncryptedData.getBytes(StandardCharsets.UTF_8));
                        logger.info("已重置加密文件: " + encryptedDataFile);
                    }
                } catch (Exception e) {
                    logger.warning("重置文件失败: " + encryptedDataFile + " - " + e.getMessage());
                }
            }
        }
        // 保存新的密钥材料
        saveKeyMaterial(newSalt, newSecretKey.getEncoded());
        logger.info("密钥已更新");
    }
    // 测试主方法
    public static void main(String[] args) throws Exception {
//
            String password = "123456";
//
           String USDT_TRC20 = "usdt_trc20";
           String USDT_ERC20 = "usdt_erc20";
           String ETH = "eth";
           String BTC = "btc";
//           String USDC_TRC20 = "usdc_trc20";
//           String USDC_ERC20 = "usdc_erc20";
           String USDC_ERC20 = "usdc_erc20(1)";
           // 加密并保存
//        encryptAndStore(password, "1111111111", USDT_TRC20);
//        encryptAndStore(password,"222222222",USDT_ERC20);
        encryptAndStore(password,"3333333333",USDC_ERC20);
//        encryptAndStore(password,"我是BTC",BTC);
//        encryptAndStore(password,"44444444444444",USDC_TRC20);
//        encryptAndStore(password,"55555555555555",USDC_ERC20);
//
            List<CryptoCurrencyEnum> cryptoCurrencyEnums = CryptoCurrencyEnum.getAll();
            cryptoCurrencyEnums.forEach(f->{
                try {
                    String address = LocalKeyStorageAESUtil.loadAndDecrypt(f.getName());
                    System.out.println(address);
                } catch (Exception ex) {
                    logger.info("------------"+f.getName()+"地址文件被更改-----------------");
                }
            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    }
}