今天,无聊的我又写了一个无聊的小工具。
常常需要用到数据库字符串的我们,对其加解密的操作还是很有必要的。
小工具的界面如下:
使用的是DES对称加密,网上扒下来加密方法如下:
/// <summary>
/// DES AES Blowfish
/// 对称加密算法的优点是速度快,
/// 缺点是密钥管理不方便,要求共享密钥。
/// 可逆对称加密 密钥长度8
/// </summary>
private static byte[] _rgbKey = ASCIIEncoding.ASCII.GetBytes("sunnysun");
private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes("sunsunny");
/// <summary>
/// DES 加密
/// </summary>
/// <param name="text">需要加密的值</param>
/// <returns>加密后的结果</returns>
public static string DESEncrypt2(string text)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(crypStream);
sWriter.Write(text);
sWriter.Flush();
crypStream.FlushFinalBlock();
memStream.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="encryptText"></param>
/// <returns>解密后的结果</returns>
public static string DESDecrypt2(string encryptText)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
byte[] buffer = Convert.FromBase64String(encryptText);
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
crypStream.Write(buffer, 0, buffer.Length);
crypStream.FlushFinalBlock();
return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
}
}
区别在于,我在加密的基础上,又做了一次字符串翻转的操作。
public static string Reverse(string original)
{
char[] arr = original.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
作为一个简单的工具,项目结构自然也及其简单。
MainView:
<Window x:Class="DbEncrypt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DbEncrypt" d:DataContext="{d:DesignInstance Type=local:MainViewModel}"
mc:Ignorable="d" Icon="/Encrypt.png"
Title="DES加解密工具" Height="450" Width="600" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<DockPanel VerticalAlignment="Stretch" Margin="20 20">
<TextBox FontSize="20" TextWrapping="Wrap" VerticalContentAlignment="Top" Text="{Binding EncryptStr}"/>
</DockPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="加 密" BorderBrush="blue" Command="{Binding EncryptCommand}"/>
<Button Content="解 密" BorderBrush ="Violet" Command="{Binding DecryptCommand}" Margin="100 0"/>
<Button Content="清 空" BorderBrush ="red" Command="{Binding ClearCommand}"/>
</StackPanel>
<DockPanel Grid.Row="2" VerticalAlignment="Stretch" Margin="20 20">
<TextBox FontSize="20" TextWrapping="Wrap" VerticalContentAlignment="Top" Text="{Binding DecryptStr}"/>
</DockPanel>
</Grid>
</Window>
MainViewModel:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors.Core;
namespace DbEncrypt
{
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!(object.Equals(field, newValue)))
{
field = (newValue);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
public MainViewModel()
{
}
private string encryptStr;
public string EncryptStr { get => encryptStr; set => SetProperty(ref encryptStr, value); }
private string decryptStr;
public string DecryptStr { get => decryptStr; set => SetProperty(ref decryptStr, value); }
private ActionCommand decryptCommand;
public ICommand DecryptCommand
{
get
{
if (decryptCommand == null)
{
decryptCommand = new ActionCommand(Decrypt);
}
return decryptCommand;
}
}
public void Decrypt()
{
DecryptStr = DESEncrypt.Reverse(DESEncrypt.DESDecrypt2(EncryptStr));
}
private ActionCommand encryptCommand;
public ICommand EncryptCommand
{
get
{
if (encryptCommand == null)
{
encryptCommand = new ActionCommand(Encrypt);
}
return encryptCommand;
}
}
public void Encrypt()
{
DecryptStr = DESEncrypt.DESEncrypt2(DESEncrypt.Reverse(EncryptStr));
}
private ActionCommand clearCommand;
public ICommand ClearCommand => clearCommand ??= new ActionCommand(Clear);
private void Clear()
{
EncryptStr = "";
DecryptStr = "";
}
}
}
加密:
解密:
-
技术群:添加小编微信并备注进群
小编微信:mm1552923
公众号:dotNet编程大全