Geeks With Blogs
DotNet Snippets Silverlight and C#
Hello Guys,
 
I have sample code here on how to create a simple encryption and decryption on Silverlight.
First, I created a PASSWORD from SL Controls. A Three Button for encryption, decryption, clear.
A one textblock and one textbox.
 
Second, I created a simple function for Encryption and Decryption and variables.
        
        byte[] _encryted;
        byte[] _decryted;
        string s;
 
        private string EncrytedString(string str)
        {
            _encryted = System.Text.Encoding.Unicode.GetBytes(str);
            s = Convert.ToBase64String(_encryted);
            return s;
        }
 
        private string DecrytedString(string str)
        {
            _decryted = Convert.FromBase64String(str);
s = System.Text.Encoding.Unicode.GetString(_decryted, 0,  _decryted.ToArray().Length);
            return s;
        }
 
 
Now the snippets.
 
            //For the Encryption Button
        private void btnEncrypt_Click(object sender, RoutedEventArgs e)
        {
            if (passwordBox1.Password == string.Empty)
            {
                MessageBox.Show("Please Enter your Password");
                passwordBox1.Focus();
            }
            else
            {
                btnDecrypt.IsEnabled = true;
                string s = EncrytedString(passwordBox1.Password);
                txtEncrypted.Text = s;
            }
 
        }
 
           //For the Decryption Button
        private void btnDecrypt_Click(object sender, RoutedEventArgs e)
        {
            string s;
            s = DecrytedString(txtEncrypted.Text);
            MessageBox.Show(s);
        }
 
       //For the Clear Button
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtEncrypted.Text = string.Empty;
            passwordBox1.Password = string.Empty;
            passwordBox1.Focus();
            btnDecrypt.IsEnabled = false;
        }
 
 
 
 
Posted on Friday, February 19, 2010 2:59 PM C# , ASP.NET , Silverlight | Back to top


Comments on this post: Simple Encrypt and Decrypt on Silverlight

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
Thanks.
Works fine!
Left by Austrian Guy on Sep 21, 2010 7:24 AM

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
Hi,
BASE 64 encoding has nothing to do with encryption - it's just another encoding -> not secure!!!
Left by Hurz on Feb 03, 2011 3:42 AM

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
Have found this very useful, many thanks
Left by Psychic Telephone Readings on Feb 26, 2011 3:53 AM

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
I have been trying to figure out how to decrypt my asp.net sl4 passwords. thanks. Good article.
Left by Water Damage Restoration on Nov 23, 2011 1:52 PM

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
BASE64 is not an encryption.
Left by Thats on Dec 14, 2011 9:01 PM

# re: Simple Encrypt and Decrypt on Silverlight
Requesting Gravatar...
I agree. Base64 is just a scramble and padding. Try using AesManaged to get started on real encryption.
Left by Mr. Know Nothing on Jan 26, 2012 2:10 AM

Your comment:
 (will show your gravatar)


Copyright © Sayre Collado | Powered by: GeeksWithBlogs.net | Join free