Tuesday, June 17, 2014

Simple encryption in .NET using XOR technique

Passwords are essential thing in virtual world, it is the thing which keeps you safe, If someone else gains access to your account, they may cause you a great deal of trouble - perhaps deleting your files,  hack other systems or may stolen crucial data from your system.
so while development a big/small and web/windows application you need to keep password for your account. A password with a plain text is always a danger since it can be easily Move stealthily by someone so, we need to make our plain text password to some tricky thing and thus here we need some type of 'ENCRYPTION'.
         
Encryption

Many times we need to encrypt the string in our application, it may be used for login password, transaction password, secret key etc.
In this post, I will explain how to encrypt a string using C# with XOR encryption technique
.NET provide inbuilt encryption technique using 'System.Cryptography' namespace and various in-built encryption algorithms like,
1.AES
2.Blowfish
3.DES
4.Triple DES
5.Serpent
6.Twofish
7.Camellia
8.CAST-128
9.IDEA,RC2,RC5,SEED,Skipjack,TEA,XTEA etc

Here we go for some different technique apart from above algorithms

XOR encryption (exclusive disjunction (XOR) operation):
The XOR operator is extremely common and execute with the help of binary operations.
One of the cool things about XOR encryption is that when you apply it twice, you get back the original string
In this encryption we convert our Plain text and key to 8-bit ASCII format and then apply X-OR operation on them and same thing is done for decryption.

Let's Cook:
Things we need : C# windows/web application
Write a function in C# which accept a plain text and a key to encrypt the text
see below snippet
 
     public string EncryptDecrypt(string szPlainText, int szEncryptionKey)  
     {  
       StringBuilder szInputStringBuild = new StringBuilder(szPlainText);  
       StringBuilder szOutStringBuild = new StringBuilder(szPlainText.Length);  
       char Textch;  
       for (int iCount = 0; iCount < szPlainText.Length; iCount++)  
       {  
         Textch = szInputStringBuild[iCount];  
         Textch = (char)(Textch ^ szEncryptionKey);  
         szOutStringBuild.Append(Textch);  
       }  
       return szOutStringBuild.ToString();  
     }  

Now we have ready with the code in which we have accept the plain text and key to encrypt the text. Finally we have apply XOR operation on plain text
Above function will give you Encrypted string and Same function with same key will return your Decrypted string
I have call above function and pass plain text and a key to it.

 txtEncryptedText.Text = EncryptDecrypt(txtPlainText.Text, 200);  

I got following result, see below snaps

Excryption

Now to Decrypt the encrypted string just call same function and pass the encrypted string to it (Do not change encryption key)
Now this simple encryption is ready to use anywhere you want.
 
4107687

Monday, June 2, 2014

How to resolved error 'Make sure AjaxControlToolkit. Properties. Resources. NET4.resources was correctly embedded'

Do you want to use AJAXControltoolkit 4 ? Are you stuck with following error occurred when you run your code

Error Message:
Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "AjaxControlToolkit.Properties.Resources.NET4.resources" was correctly embedded or linked into assembly "AjaxControlToolkit" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Cause:
This error occurred when An unhandled exception was generated during the execution of the current web request, basically when you use any AJAX control but does not refer a ajax library in your page then Ajaxcontoltoolkit unable to load reference of that control from System.Qeb.UI.Contol namespace and hence it produce such error

Resolutions

Add "Script Manager" in ASPX page as below

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </cc1:ToolkitScriptManager>

OR

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>


Hope it Helps to resolve the issue

New Features of C# 7.0

Introduction C# 7.0 is planned to release in parts, Microsoft has release Most of its features with Visual Studio “15” Preview 4 , it wa...