借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
1.87 KiB
XCoder
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;

namespace NewLife.XCoder.Tools.Symmetric;

/// <summary>对称加密工具</summary>
public partial class SymmetricToolView : UserControl
{
    /// <summary>实例化</summary>
    public SymmetricToolView()
    {
        InitializeComponent();

        EncryptButton.Click += (_, _) => Run(true);
        DecryptButton.Click += (_, _) => Run(false);
        ClearButton.Click += (_, _) =>
        {
            InputBox.Text = "";
            OutputBox.Text = "";
            StatusText.Text = NewLife.XCoder.Services.LocalizationService.T("等待操作");
            StatusText.Classes.Set("error", false);
            StatusText.Classes.Set("ok", false);
        };
    }

    private CryptoKind Kind => (AlgorithmBox.SelectedItem as ComboBoxItem)?.Content?.ToString() == "DES" ? CryptoKind.Des : CryptoKind.Aes;

    private void Run(Boolean encrypt)
    {
        var input = InputBox.Text ?? "";
        if (input.Length == 0)
        {
            SetStatus("请输入内容", true);
            return;
        }

        try
        {
            var hex = HexBox.IsChecked == true;
            OutputBox.Text = encrypt
                ? SymmetricCrypto.Encrypt(input, Kind, KeyBox.Text ?? "", IvBox.Text ?? "", hex)
                : SymmetricCrypto.Decrypt(input, Kind, KeyBox.Text ?? "", IvBox.Text ?? "", hex);
            SetStatus(encrypt ? "加密完成" : "解密完成", false);
        }
        catch (Exception ex)
        {
            OutputBox.Text = "";
            SetStatus((encrypt ? "加密失败:" : "解密失败:") + ex.Message, true);
        }
    }

    private void SetStatus(String text, Boolean error)
    {
        StatusText.Text = NewLife.XCoder.Services.LocalizationService.Translate(text);
        StatusText.Classes.Set("error", error);
        StatusText.Classes.Set("ok", !error);
    }
}