The .NET Framework provides implementations of many standard cryptographic algorithms. These algorithms are easy to use and have the safest possible default properties.you can use encryption decryption in your .net project using ready-made class cryptography It is password protected
Create your User
Interface like this..
XAML file
EncryptionDecryption.xaml
<Window x:Class="abc.EncryptionDecryption"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EncryptionDecryption" Height="300" Width="500" WindowStartupLocation="CenterScreen">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="26,60,0,0" TextWrapping="Wrap" Text="Encryption" VerticalAlignment="Top"/>
<TextBox x:Name="txtencrypt"
HorizontalAlignment="Left" Height="23" Margin="122,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="322"/>
<TextBlock HorizontalAlignment="Left" Margin="26,103,0,0"
TextWrapping="Wrap" Text="Decryption" VerticalAlignment="Top"/>
<TextBox x:Name="txtDecrypt"
HorizontalAlignment="Left" Height="23" Margin="122,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="322"/>
<Button x:Name="btnsubmit" Content="Submit"
HorizontalAlignment="Left" Margin="216,174,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
Now Add new class to
your project name it Encrypt
using System;
using System.Security.Cryptography;
namespace abc
{
public static class Encrypt
{
public static string getEncryptedString(string input)
{
CryptoProvider c = new CryptoProvider();
return c.EncryptData(input.GetBytes(), "yourpassword").GetString();
}
public static string getDecryptedString(string input)
{
CryptoProvider c = new CryptoProvider();
return c.DecryptData(input.GetBytes(), "yourpassword").GetString();
}
public static byte[] GetBytes(this string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(),
0, bytes, 0, bytes.Length);
return bytes;
}
public static string GetString(this byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0,
chars, 0, bytes.Length);
return new string(chars);
}
}
public class CryptoProvider
{
AesManaged _algorithm = new AesManaged();
public byte[] EncryptData(byte[] data, string password)
{
GetKey(password);
ICryptoTransform encrypter = _algorithm.CreateEncryptor();
byte[] cryptToData = encrypter.TransformFinalBlock(data, 0,
data.Length);
return cryptToData;
}
public byte[] DecryptData(byte[] data, string password)
{
GetKey(password);
ICryptoTransform decrypter = _algorithm.CreateDecryptor();
byte[] decryptToData = decrypter.TransformFinalBlock(data, 0,
data.Length);
return decryptToData;
}
private void GetKey(string password)
{
byte[] salt = new byte[8];
byte[] passwordbyte = System.Text.Encoding.Unicode.GetBytes(password);
int length = Math.Min(passwordbyte.Length, salt.Length);
for (int i = 0; i < length; i++)
{
salt[i] = passwordbyte[i];
}
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
_algorithm.Key =
key.GetBytes(_algorithm.KeySize / 8);
_algorithm.IV =
key.GetBytes(_algorithm.BlockSize / 8);
}
}
}
Now come to your
EncryptionDecryption.xaml
using System.Windows;
namespace abc
{
/// <summary>
/// Interaction logic for EncryptionDecryption.xaml
/// </summary>
public partial class EncryptionDecryption
{
public EncryptionDecryption()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrWhiteSpace(txtencrypt.Text))
{
txtDecrypt.Text = Encrypt.getEncryptedString(txtencrypt.Text);
}
else if (!string.IsNullOrWhiteSpace(txtDecrypt.Text))
{
txtencrypt.Text = Encrypt.getDecryptedString(txtDecrypt.Text);
}
else
{
MessageBox.Show("Enter Decryption value to encrypt");
}
}
catch
{
MessageBox.Show("Something
wrong Re-check values");
}
}
}
}
Press F5 now Encrypt and decrypt your confidential values ...
No comments:
Post a Comment