Click here to Skip to main content
15,892,674 members
Articles / Security / Cryptography

Encrypt & Decrypt Strings in Silverlight

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
31 Jul 2012CPOL2 min read 25.3K   803   5  
A very brief article about the implementation of encryption and decryption of a string in Silverlight Business Application
namespace Encrypt_Decrypt_SBA
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Encrypt_Decrypt_SBA.Helpers;
    using System.Windows;

    /// <summary>
    /// Página de inicio de la aplicación.
    /// </summary>
    public partial class Home : Page
    {
        /// <summary>
        /// Crea una nueva instancia de <see cref="Home"/>.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Se ejecuta cuando el usuario navega a esta page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnEncrypt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Encrypt the text inside simpleText and empty the textbox to show functionality
            this.EncryptedText.Text = this.simpleText.Text.Encrypt();
            this.simpleText.Text = string.Empty;
        }

        private void btnDencrypt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Decrypt the string 
            MessageBox.Show(this.EncryptedText.Text.Decrypt());
            this.EncryptedText.Text = string.Empty;
        }

        private void btnEncryptService_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Instanciate the proxy for ServiceTest
            ServiceTest.ServiceTestClient proxy = new ServiceTest.ServiceTestClient();
            //Call to the completed event of that proxy
            proxy.EncryptStringCompleted += (s, args) =>
            {
                this.simpleTextService.Text = string.Empty;
                this.EncryptedTextService.Text = args.Result;
            };
            //Call the async method and pass the parameter
            proxy.EncryptStringAsync(this.simpleTextService.Text);
        }

        private void btnDencryptService_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Instanciate the proxy for ServiceTest
            ServiceTest.ServiceTestClient proxy = new ServiceTest.ServiceTestClient();
            //Call to the completed event of that proxy
            proxy.DecryptStringCompleted += (s, args) =>
            {
                MessageBox.Show("Via service: "+args.Result);
                this.EncryptedTextService.Text = string.Empty;
            };
            //Call the async method and pass the parameter
            proxy.DecryptStringAsync(this.EncryptedTextService.Text);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Paraguay Paraguay
hristian Amado is a professional software engineer, professional developer and trainer with over 18 years of experience building applications for Windows Desktop and the Web. Located in Asuncion, Paraguay, He's well involved in many Microsoft's technologies including XAML, C#, X++, WCF, ADO.NET and ASP.NET.

He holds a several Microsoft certifications including Microsoft Certified Professional Developer (MCPD), Microsoft Certified IT Professional, Microsoft Certified Technology Specialist and Microsoft Office Specialist.

Comments and Discussions