Click here to Skip to main content
15,897,032 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
//------------------------------------------------------------------------------
// <auto-generated>
//     Este código fue generado por una herramienta.
//     Versión de runtime:4.0.30319.269
//
//     Los cambios en este archivo podrían causar un comportamiento incorrecto y se perderán si
//     se vuelve a generar el código.
// </auto-generated>
//------------------------------------------------------------------------------

// 
// This code was auto-generated by Microsoft.Silverlight.ServiceReference, version 5.0.61118.0
// 
namespace Encrypt_Decrypt_SBA.ServiceTest {
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="", ConfigurationName="ServiceTest.ServiceTest")]
    public interface ServiceTest {
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="urn:ServiceTest/DoWork", ReplyAction="urn:ServiceTest/DoWorkResponse")]
        System.IAsyncResult BeginDoWork(System.AsyncCallback callback, object asyncState);
        
        void EndDoWork(System.IAsyncResult result);
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="urn:ServiceTest/EncryptString", ReplyAction="urn:ServiceTest/EncryptStringResponse")]
        System.IAsyncResult BeginEncryptString(string unencrypted, System.AsyncCallback callback, object asyncState);
        
        string EndEncryptString(System.IAsyncResult result);
        
        [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="urn:ServiceTest/DecryptString", ReplyAction="urn:ServiceTest/DecryptStringResponse")]
        System.IAsyncResult BeginDecryptString(string encrypted, System.AsyncCallback callback, object asyncState);
        
        string EndDecryptString(System.IAsyncResult result);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface ServiceTestChannel : Encrypt_Decrypt_SBA.ServiceTest.ServiceTest, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class EncryptStringCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        public EncryptStringCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        public string Result {
            get {
                base.RaiseExceptionIfNecessary();
                return ((string)(this.results[0]));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class DecryptStringCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
        
        private object[] results;
        
        public DecryptStringCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
                base(exception, cancelled, userState) {
            this.results = results;
        }
        
        public string Result {
            get {
                base.RaiseExceptionIfNecessary();
                return ((string)(this.results[0]));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class ServiceTestClient : System.ServiceModel.ClientBase<Encrypt_Decrypt_SBA.ServiceTest.ServiceTest>, Encrypt_Decrypt_SBA.ServiceTest.ServiceTest {
        
        private BeginOperationDelegate onBeginDoWorkDelegate;
        
        private EndOperationDelegate onEndDoWorkDelegate;
        
        private System.Threading.SendOrPostCallback onDoWorkCompletedDelegate;
        
        private BeginOperationDelegate onBeginEncryptStringDelegate;
        
        private EndOperationDelegate onEndEncryptStringDelegate;
        
        private System.Threading.SendOrPostCallback onEncryptStringCompletedDelegate;
        
        private BeginOperationDelegate onBeginDecryptStringDelegate;
        
        private EndOperationDelegate onEndDecryptStringDelegate;
        
        private System.Threading.SendOrPostCallback onDecryptStringCompletedDelegate;
        
        private BeginOperationDelegate onBeginOpenDelegate;
        
        private EndOperationDelegate onEndOpenDelegate;
        
        private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
        
        private BeginOperationDelegate onBeginCloseDelegate;
        
        private EndOperationDelegate onEndCloseDelegate;
        
        private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
        
        public ServiceTestClient() {
        }
        
        public ServiceTestClient(string endpointConfigurationName) : 
                base(endpointConfigurationName) {
        }
        
        public ServiceTestClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ServiceTestClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ServiceTestClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {
        }
        
        public System.Net.CookieContainer CookieContainer {
            get {
                System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null)) {
                    return httpCookieContainerManager.CookieContainer;
                }
                else {
                    return null;
                }
            }
            set {
                System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null)) {
                    httpCookieContainerManager.CookieContainer = value;
                }
                else {
                    throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
                            "ookieContainerBindingElement.");
                }
            }
        }
        
        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> DoWorkCompleted;
        
        public event System.EventHandler<EncryptStringCompletedEventArgs> EncryptStringCompleted;
        
        public event System.EventHandler<DecryptStringCompletedEventArgs> DecryptStringCompleted;
        
        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
        
        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.IAsyncResult Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.BeginDoWork(System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginDoWork(callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        void Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.EndDoWork(System.IAsyncResult result) {
            base.Channel.EndDoWork(result);
        }
        
        private System.IAsyncResult OnBeginDoWork(object[] inValues, System.AsyncCallback callback, object asyncState) {
            return ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).BeginDoWork(callback, asyncState);
        }
        
        private object[] OnEndDoWork(System.IAsyncResult result) {
            ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).EndDoWork(result);
            return null;
        }
        
        private void OnDoWorkCompleted(object state) {
            if ((this.DoWorkCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.DoWorkCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void DoWorkAsync() {
            this.DoWorkAsync(null);
        }
        
        public void DoWorkAsync(object userState) {
            if ((this.onBeginDoWorkDelegate == null)) {
                this.onBeginDoWorkDelegate = new BeginOperationDelegate(this.OnBeginDoWork);
            }
            if ((this.onEndDoWorkDelegate == null)) {
                this.onEndDoWorkDelegate = new EndOperationDelegate(this.OnEndDoWork);
            }
            if ((this.onDoWorkCompletedDelegate == null)) {
                this.onDoWorkCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnDoWorkCompleted);
            }
            base.InvokeAsync(this.onBeginDoWorkDelegate, null, this.onEndDoWorkDelegate, this.onDoWorkCompletedDelegate, userState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.IAsyncResult Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.BeginEncryptString(string unencrypted, System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginEncryptString(unencrypted, callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        string Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.EndEncryptString(System.IAsyncResult result) {
            return base.Channel.EndEncryptString(result);
        }
        
        private System.IAsyncResult OnBeginEncryptString(object[] inValues, System.AsyncCallback callback, object asyncState) {
            string unencrypted = ((string)(inValues[0]));
            return ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).BeginEncryptString(unencrypted, callback, asyncState);
        }
        
        private object[] OnEndEncryptString(System.IAsyncResult result) {
            string retVal = ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).EndEncryptString(result);
            return new object[] {
                    retVal};
        }
        
        private void OnEncryptStringCompleted(object state) {
            if ((this.EncryptStringCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.EncryptStringCompleted(this, new EncryptStringCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void EncryptStringAsync(string unencrypted) {
            this.EncryptStringAsync(unencrypted, null);
        }
        
        public void EncryptStringAsync(string unencrypted, object userState) {
            if ((this.onBeginEncryptStringDelegate == null)) {
                this.onBeginEncryptStringDelegate = new BeginOperationDelegate(this.OnBeginEncryptString);
            }
            if ((this.onEndEncryptStringDelegate == null)) {
                this.onEndEncryptStringDelegate = new EndOperationDelegate(this.OnEndEncryptString);
            }
            if ((this.onEncryptStringCompletedDelegate == null)) {
                this.onEncryptStringCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnEncryptStringCompleted);
            }
            base.InvokeAsync(this.onBeginEncryptStringDelegate, new object[] {
                        unencrypted}, this.onEndEncryptStringDelegate, this.onEncryptStringCompletedDelegate, userState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        System.IAsyncResult Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.BeginDecryptString(string encrypted, System.AsyncCallback callback, object asyncState) {
            return base.Channel.BeginDecryptString(encrypted, callback, asyncState);
        }
        
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        string Encrypt_Decrypt_SBA.ServiceTest.ServiceTest.EndDecryptString(System.IAsyncResult result) {
            return base.Channel.EndDecryptString(result);
        }
        
        private System.IAsyncResult OnBeginDecryptString(object[] inValues, System.AsyncCallback callback, object asyncState) {
            string encrypted = ((string)(inValues[0]));
            return ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).BeginDecryptString(encrypted, callback, asyncState);
        }
        
        private object[] OnEndDecryptString(System.IAsyncResult result) {
            string retVal = ((Encrypt_Decrypt_SBA.ServiceTest.ServiceTest)(this)).EndDecryptString(result);
            return new object[] {
                    retVal};
        }
        
        private void OnDecryptStringCompleted(object state) {
            if ((this.DecryptStringCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.DecryptStringCompleted(this, new DecryptStringCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void DecryptStringAsync(string encrypted) {
            this.DecryptStringAsync(encrypted, null);
        }
        
        public void DecryptStringAsync(string encrypted, object userState) {
            if ((this.onBeginDecryptStringDelegate == null)) {
                this.onBeginDecryptStringDelegate = new BeginOperationDelegate(this.OnBeginDecryptString);
            }
            if ((this.onEndDecryptStringDelegate == null)) {
                this.onEndDecryptStringDelegate = new EndOperationDelegate(this.OnEndDecryptString);
            }
            if ((this.onDecryptStringCompletedDelegate == null)) {
                this.onDecryptStringCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnDecryptStringCompleted);
            }
            base.InvokeAsync(this.onBeginDecryptStringDelegate, new object[] {
                        encrypted}, this.onEndDecryptStringDelegate, this.onDecryptStringCompletedDelegate, userState);
        }
        
        private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
            return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
        }
        
        private object[] OnEndOpen(System.IAsyncResult result) {
            ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
            return null;
        }
        
        private void OnOpenCompleted(object state) {
            if ((this.OpenCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void OpenAsync() {
            this.OpenAsync(null);
        }
        
        public void OpenAsync(object userState) {
            if ((this.onBeginOpenDelegate == null)) {
                this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
            }
            if ((this.onEndOpenDelegate == null)) {
                this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
            }
            if ((this.onOpenCompletedDelegate == null)) {
                this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
            }
            base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
        }
        
        private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
            return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
        }
        
        private object[] OnEndClose(System.IAsyncResult result) {
            ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
            return null;
        }
        
        private void OnCloseCompleted(object state) {
            if ((this.CloseCompleted != null)) {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }
        
        public void CloseAsync() {
            this.CloseAsync(null);
        }
        
        public void CloseAsync(object userState) {
            if ((this.onBeginCloseDelegate == null)) {
                this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
            }
            if ((this.onEndCloseDelegate == null)) {
                this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
            }
            if ((this.onCloseCompletedDelegate == null)) {
                this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
            }
            base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
        }
        
        protected override Encrypt_Decrypt_SBA.ServiceTest.ServiceTest CreateChannel() {
            return new ServiceTestClientChannel(this);
        }
        
        private class ServiceTestClientChannel : ChannelBase<Encrypt_Decrypt_SBA.ServiceTest.ServiceTest>, Encrypt_Decrypt_SBA.ServiceTest.ServiceTest {
            
            public ServiceTestClientChannel(System.ServiceModel.ClientBase<Encrypt_Decrypt_SBA.ServiceTest.ServiceTest> client) : 
                    base(client) {
            }
            
            public System.IAsyncResult BeginDoWork(System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[0];
                System.IAsyncResult _result = base.BeginInvoke("DoWork", _args, callback, asyncState);
                return _result;
            }
            
            public void EndDoWork(System.IAsyncResult result) {
                object[] _args = new object[0];
                base.EndInvoke("DoWork", _args, result);
            }
            
            public System.IAsyncResult BeginEncryptString(string unencrypted, System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[1];
                _args[0] = unencrypted;
                System.IAsyncResult _result = base.BeginInvoke("EncryptString", _args, callback, asyncState);
                return _result;
            }
            
            public string EndEncryptString(System.IAsyncResult result) {
                object[] _args = new object[0];
                string _result = ((string)(base.EndInvoke("EncryptString", _args, result)));
                return _result;
            }
            
            public System.IAsyncResult BeginDecryptString(string encrypted, System.AsyncCallback callback, object asyncState) {
                object[] _args = new object[1];
                _args[0] = encrypted;
                System.IAsyncResult _result = base.BeginInvoke("DecryptString", _args, callback, asyncState);
                return _result;
            }
            
            public string EndDecryptString(System.IAsyncResult result) {
                object[] _args = new object[0];
                string _result = ((string)(base.EndInvoke("DecryptString", _args, result)));
                return _result;
            }
        }
    }
}

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