Saturday, 13 September 2014

.NET Framework Cryptography for encryption and decryption confidential values

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 ...

Wednesday, 10 September 2014

How to create screen like windows 8 using devexpress controls

Here what I have done is I used tile control of devexpress
I have binded the images on different interval
And video as well


<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="WpfDigital.TilesDemo"
        Title="TilesDemo"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="205*"/>
            <ColumnDefinition Width="308*"/>
            <ColumnDefinition Width="247*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="312*"/>
            <RowDefinition Height="138*"/>
            <RowDefinition Height="55*"/>
        </Grid.RowDefinitions>
        <dxlc:TileLayoutControl x:Name="tile1" Background="#FF111799" Margin="70,40,-70,-40" Grid.RowSpan="3" Grid.ColumnSpan="3" >
            <dxlc:Tile Header="C#" Background="Bisque" Size="ExtraSmall" >
                <Image Name="name" Source="Images\download (1).jpg"></Image>
            </dxlc:Tile>
            <dxlc:Tile Size="Large" ContentSource="{Binding call}" ContentChangeInterval="00:00:02">
                <dxlc:Tile.ContentTemplate>
                    <DataTemplate>
                        <Grid>
                            <TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Name}"></TextBlock>
                            <Image HorizontalAlignment="Right" VerticalAlignment="Top" Source="{Binding Picture}"></Image>
                        </Grid>
                    </DataTemplate>
                </dxlc:Tile.ContentTemplate>
            </dxlc:Tile>
            <dxlc:Tile Header="SILVERLIGHT" Background="Gainsboro" Size="ExtraLarge">
                <Image Name="name1" Source="Images\download.jpg"></Image>
            </dxlc:Tile>
            <dxlc:Tile Header="SQL" >
                <MediaElement Source="D:\Shoeb\Practice\WpfDigital\WpfDigital\Images\abc.mp4" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="250"/>
            </dxlc:Tile>
            <dxlc:Tile Header="ORACLE" Background="Chocolate"/>
            <dxlc:Tile Header="WPF" Background="Beige"/>
            <dxlc:Tile Header="XAML" Background="DarkSeaGreen"/>
            <dxlc:Tile Header="LINQ" Background="Black"/>
            <dxlc:Tile Header="WWF" Background="DeepSkyBlue"/>
            <dxlc:Tile Header="WCF" Background="Red"/>
            <dxlc:Tile Header=".NET" Background="Yellow"/>
        </dxlc:TileLayoutControl>
    </Grid>

</Window>


public List<flag> call { get { return WpfDigital.TilesDemo.flags.DataSource; } }
        public class flag {
            public string Name { get; set; }
            public string Picture { get; set; }
            public ImageSource Image {
                get {
                    return string.IsNullOrEmpty(Picture) ? null : new BitmapImage(new Uri(Picture, UriKind.Relative));
                }
            }
        }
        public static class flags
        {
            public static readonly List<flag> DataSource = new List<flag>
            {
                new flag{Name="Shoeb",Picture="Images/1.jpg"},
                new flag{Name="Jhon",Picture="Images/2.jpg" }
            };
        }