Simple Encryption And Decryption in C#

Simple Encryption and Decryption using C#.
Encryption is used to convert the text into non-meaning full text. Dot Net provides different algorithms to encrypt text. Varies (64bit,128bit,256bit and 512bit). in this article i used 64bit (8 byte) encryption by using
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
DESCryptoServiceProvider requires Key and initialization Vector (IV) both (8 byte) to encrypt text and decrypt it back. This class provides access to the Windows CryptoAPI cryptography services.
It can be used to encrypt text in different scenarios. For example you can encrypt the text you want to use as query string in web applications. because it is clearly visible to user. for security purposes we can encrypt the id's used in query strings.
For example the URL
http://localhost/Employee/Salary?EmployeeID=767877686
can be converted to
http://localhost/Employee/Salary?EmployeeID=LhuJsI79G5E4DNnN2GxdqQ==
The EmployeeID=767877686 is Equal to LhuJsI79G5E4DNnN2GxdqQ==

Implemention of DESCryptoServiceProvider

Add the following class to your project

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;


   public class SimpleSecurity
    {
       public string Encrypt(string textToEncrypt)
       {
           try
           {
               string ToReturn = "";
               string _key = "ay$a5%&jwrtmnh;lasjdf98787";
               string _iv = "abc@98797hjkas$&asd(*$%";
               byte[] _ivByte = { };
               _ivByte = System.Text.Encoding.UTF8.GetBytes(_iv.Substring(0, 8));
               byte[] _keybyte = { };
               _keybyte = System.Text.Encoding.UTF8.GetBytes(_key.Substring(0, 8));
               MemoryStream ms = null; CryptoStream cs = null;
               byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(textToEncrypt);
               using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
               {
                   ms = new MemoryStream();
                   cs = new CryptoStream(ms, des.CreateEncryptor(_keybyte, _ivByte), CryptoStreamMode.Write);
                   cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                   cs.FlushFinalBlock();
                   ToReturn = Convert.ToBase64String(ms.ToArray());
               }
               return ToReturn;
           }
           catch(Exception ae)
           {
               throw new Exception(ae.Message, ae.InnerException);
           }
       }
       public string Decrypt(string textToDecrypt)
       {
           try
           {
               string ToReturn = "";
               string _key = "ay$a5%&jwrtmnh;lasjdf98787";
               string _iv = "abc@98797hjkas$&asd(*$%";
               byte[] _ivByte = { };
               _ivByte = System.Text.Encoding.UTF8.GetBytes(_iv.Substring(0, 8));
               byte[] _keybyte = { };
               _keybyte = System.Text.Encoding.UTF8.GetBytes(_key.Substring(0, 8));
               MemoryStream ms = null; CryptoStream cs = null;
               byte[] inputbyteArray = new byte[textToDecrypt.Replace(" ", "+").Length];
               inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ", "+"));
               using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
               {
                   ms = new MemoryStream();
                   cs = new CryptoStream(ms, des.CreateDecryptor(_keybyte, _ivByte), CryptoStreamMode.Write);
                   cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                   cs.FlushFinalBlock();
                   Encoding encoding = Encoding.UTF8;
                   ToReturn = encoding.GetString(ms.ToArray());
               }
               return ToReturn;
           }
           catch(Exception ae)
           {
               throw new Exception(ae.Message, ae.InnerException);
           }
       }
    }
Using the code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            SimpleSecurity ss = new SimpleSecurity();
            txtEncryptedText.Text = ss.Encrypt(txtTextToEncrypt.Text);
        }

        private void btnDecryptBack_Click(object sender, EventArgs e)
        {
            SimpleSecurity ss = new SimpleSecurity();
            txtOriginalText.Text = ss.Decrypt(txtEncryptedText.Text);
        }

5 Comments

  1. How to decode generated string in android studio?

    ReplyDelete
  2. how decrypt this url
    i want id ...
    https://sbs.beinsports.net/MP/MP/Renew/yIGqmFMj7XG%20VIGchB4qB4GYulF618SdoLiTG4R8WN6mdSG18W8SbWqz37bHeBxd7KT8kdQnglLDHVL6x5b_BFhzoTQAsnWXbgL46H913B2PDgddZwUSx6EYti824Zr6i4ljS3nZSiEgZyGdPxwXUtcySEb9RWVvJyPFPuM0hK%20m5G_jNKE1Yg%3d%3d

    ReplyDelete
  3. how decrypt this url
    i want id ...
    https://sbs.beinsports.net/MP/MP/Renew/yIGqmFMj7XG%20VIGchB4qB4GYulF618SdoLiTG4R8WN6mdSG18W8SbWqz37bHeBxd7KT8kdQnglLDHVL6x5b_BFhzoTQAsnWXbgL46H913B2PDgddZwUSx6EYti824Zr6i4ljS3nZSiEgZyGdPxwXUtcySEb9RWVvJyPFPuM0hK%20m5G_jNKE1Yg%3d%3d

    ReplyDelete

Post a Comment