我需要使一个文本框的内容加密成一个不同的文本框

本文关键字:文本 一个 加密 | 更新日期: 2023-09-27 18:13:13

所以我要做的是在WPF中创建一个基本的字符替换密码,但由于某种原因,当我按下enter键时,结果框不会更新。有人能看到我做错了我的c#代码下面?

public partial class MainWindow : Window
{
    public static string PassingEncrypt;
    public static string PassingDecrypt;
    string plainText;
    public MainWindow()
    {
        InitializeComponent();
        string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
        plainText = EncryptBox.Text;
        string cipherText = Encrypt(plainText, key);
        string decryptedText = Decrypt(cipherText, key);
        EncryptResult.Text = cipherText;
        DecryptResult.Text = decryptedText;
    }
    private void EncryptBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        this.EncryptBox = sender as TextBox;
        plainText = EncryptBox.Text;
    }
    static string Encrypt(string plainText, string key)
    {
        char[] chars = new char[plainText.Length];
        for (int i = 0; i < plainText.Length; i++)
        {
            if (plainText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else
            {
                int j = plainText[i] - 97;
                chars[i] = key[j];
            }
        }
        return new string(chars);
    }
    static string Decrypt(string cipherText, string key)
    {
        char[] chars = new char[cipherText.Length];
        for (int i = 0; i < cipherText.Length; i++)
        {
            if (cipherText[i] == ' ')
            {
                chars[i] = ' ';
            }
            else
            {
                int j = key.IndexOf(cipherText[i]) - 97;
                chars[i] = (char)j;
            }
        }
        return new string(chars);
    }

}

如果你找不到问题,这里是我的xaml代码

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="EncryptBox" HorizontalAlignment="Left" Height="50" Margin="0,81,0,0" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="250" TextChanged="EncryptBox_TextChanged"/>
        <TextBox x:Name="DecryptBox" HorizontalAlignment="Left" Height="50" Margin="269,81,-0.6,0" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="250"/>
        <TextBox x:Name="EncryptResult" HorizontalAlignment="Left" Height="43" Margin="0,238,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
        <TextBox x:Name="DecryptResult" HorizontalAlignment="Left" Height="43" Margin="269,238,-0.6,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/>
    </Grid>
</Window>

我需要使一个文本框的内容加密成一个不同的文本框

您可以在后面的代码中处理TextChangedEvent,但您真的不应该这样做。这就是Converters的目的,保持你的视图干净,没有后面的代码。

Encrypt-DecriptBoxText绑定到其前身的Text上,让Converters完成工作。

MainWindow.xaml

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:EncryptConverter x:Key="EncryptConverter"/>
        <local:DecryptConverter x:Key="DecryptConverter"/>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Margin" Value="20,0"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Label>Plain Text</Label>
        <TextBox x:Name="PlainBox" MaxLength="26"/>
        <Label>Encripted Text</Label>
        <TextBox x:Name="EncriptedBox" IsEnabled="False"
                 Text="{Binding Path=Text, ElementName=PlainBox, Converter={StaticResource EncryptConverter}}"/>
        <Label>Decripted Text</Label>
        <TextBox x:Name="DecriptedBox" IsEnabled="False"
                 Text="{Binding Path=Text, ElementName=EncriptedBox, Converter={StaticResource DecryptConverter}}"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs

namespace WpfApplication3
{
    // View with no code-behind
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    // From plain to encripted Text
    public class EncryptConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
        {
            string plainText = (string)value;
            char[] chars = new char[plainText.Length];
            string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
            for (int i = 0; i < plainText.Length; i++)
            {
                if (plainText[i] == ' ')
                {
                    chars[i] = ' ';
                }
                else
                {
                    int j = plainText[i] - 97;
                    chars[i] = key[j];
                }
            }
            return new string(chars);
        }
        public object ConvertBack(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    // From encripted to plain Text
    public class DecryptConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
        {
            string cipherText = (string)value;
            char[] chars = new char[cipherText.Length];
            string key = "=2/3E*45-`~6<>!,.7+8[]9|:0";
            for (int i = 0; i < cipherText.Length; i++)
            {
                if (cipherText[i] == ' ')
                {
                    chars[i] = ' ';
                }
                else
                {
                    int j = key.IndexOf(cipherText[i]) + 97;
                    chars[i] = (char)j;
                }
            }
            return new string(chars);
        }
        public object ConvertBack(object value, Type targetType, object parameter,
                System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}