Data Encryption - Decryption [Unity3d - C#]



What is Encryption?

- In cryptography, encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Ideally, only authorized parties can decipher a ciphertext back to plaintext and access the original information.


What is Decryption?

- The conversion of encrypted data into its original form is called Decryption. It is generally a reverse process of encryption. It decodes the encrypted information so that an authorized user can only decrypt the data because decryption requires a secret key or password.


There are many techniques and algorithms for data Encryption and Decryption in Unity(C#).

Here Describe 4 most popular algorithms to Encrypt or Decrypt process in C#.

1) AES

2) DES

3) MD5

4) XOR

 

Download Unity Demo Project: 

https://github.com/sudhirkotila/Data-Security-Unity-Encryption-Decryption-Demo


1) AES:

- AES – Advanced Encryption Standard.

- This is more popular with a strong security level encryption-decryption data algorithm.

- The Advanced Encryption Standard, also known by its original name Rijndael is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology in 2001.

-  It is found at least six times faster than triple DES.


Example:

  
  using System;
  using System.Text;
  using System.Security.Cryptography;   

  string key = "A60A5770FE5E7AB200BA9CFC94E4E8B0"//set any string of 32 chars
  string iv = "1234567887654321"//set any string of 16 chars
 
  //AES - Encription 
  public string AESEncryption(string inputData)
    {
        AesCryptoServiceProvider AEScryptoProvider = new AesCryptoServiceProvider();
        AEScryptoProvider.BlockSize = 128;
        AEScryptoProvider.KeySize = 256;
        AEScryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(key);
        AEScryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(iv);
        AEScryptoProvider.Mode = CipherMode.CBC;
        AEScryptoProvider.Padding = PaddingMode.PKCS7;
 
        byte[] txtByteData = ASCIIEncoding.ASCII.GetBytes(inputData);
        ICryptoTransform trnsfrm = AEScryptoProvider.CreateEncryptor(AEScryptoProvider.KeyAEScryptoProvider.IV);
 
        byte[] result = trnsfrm.TransformFinalBlock(txtByteData0txtByteData.Length);
        return Convert.ToBase64String(result);
    }
 
    //AES -  Decryption
    public string AESDecryption(string inputData)
    {
        AesCryptoServiceProvider AEScryptoProvider = new AesCryptoServiceProvider();
        AEScryptoProvider.BlockSize = 128;
        AEScryptoProvider.KeySize = 256;
        AEScryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(key);
        AEScryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(iv);
        AEScryptoProvider.Mode = CipherMode.CBC;
        AEScryptoProvider.Padding = PaddingMode.PKCS7;
 
        byte[] txtByteData = Convert.FromBase64String(inputData);
        ICryptoTransform trnsfrm = AEScryptoProvider.CreateDecryptor();
 
        byte[] result = trnsfrm.TransformFinalBlock(txtByteData0txtByteData.Length);
        return ASCIIEncoding.ASCII.GetString(result);
    }
 

Input: This is AES Demo

Output: Pwsx645kDk6jTLZOOLjcYzRn//JwVdPu9zqH07rdUEY= 


2) DES:

- DES – Data Encryption Standard

- The Data Encryption Standard (DES) is a symmetric-key block cipher published by the National Institute of Standards and Technology (NIST).

- DES is a symmetric-key algorithm for the encryption of digital data. Although its short key length of 56 bits makes it too insecure for applications, it has been highly influential in the advancement of cryptography.

- For more info: https://en.wikipedia.org/wiki/Data_Encryption_Standard

Example:


   using System;
   using System.Text;
   using System.Security.Cryptography; 
   using System.IO;

   string DESKey = "ABCDEFGH"//set any string of 8 chars
 
    //DES - Encription 
    public string DESEncryption(string inputData)
    {
        byte[] txtByteData = ASCIIEncoding.ASCII.GetBytes(inputData);
        byte[] keyByteData = ASCIIEncoding.ASCII.GetBytes(DESKey);
 
        DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider();
        ICryptoTransform trnsfrm = DEScryptoProvider.CreateEncryptor(keyByteDatakeyByteData);
        CryptoStreamMode mode = CryptoStreamMode.Write;
 
        //Set up Stream & Write Encript data
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStreamtrnsfrmmode);
        cStream.Write(txtByteData0txtByteData.Length);
        cStream.FlushFinalBlock();
 
        //Read Ecncrypted Data From Memory Stream
        byte[] result = new byte[mStream.Length];
        mStream.Position = 0;
        mStream.Read(result0result.Length);
 
        return Convert.ToBase64String(result);
    }
 
    //DES -  Decryption
    public string DESDecryption(string inputData)
    {
        byte[] txtByteData = Convert.FromBase64String(inputData);
        byte[] keyByteData = ASCIIEncoding.ASCII.GetBytes(DESKey);
 
        DESCryptoServiceProvider DEScryptoProvider = new DESCryptoServiceProvider();
        ICryptoTransform trnsfrm = DEScryptoProvider.CreateDecryptor(keyByteDatakeyByteData);
        CryptoStreamMode mode = CryptoStreamMode.Write;
 
        //Set up Stream & Write Encript data
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStreamtrnsfrmmode);
        cStream.Write(txtByteData0txtByteData.Length);
        cStream.FlushFinalBlock();
 
        //Read Ecncrypted Data From Memory Stream
        byte[] result = new byte[mStream.Length];
        mStream.Position = 0;
        mStream.Read(result0result.Length);
 
        return ASCIIEncoding.ASCII.GetString(result);
    }

 Input: This is DES Demo

Output: sUCnq2SAMpO7racUGDO85ovr9i5cFI4h


3) MD5:

- The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value.

- Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities

For more info: https://en.wikipedia.org/wiki/MD5

Example:


    using System; 
    using System.Text;
    using System.Security.Cryptography; 

    //MD5 - Encryption 
    public string MD5Encryption(string inputData)
    {
        string hasKey = "Password@SudhirKotila12:34"//You can use any string here as haskey
        byte[] bData = UTF8Encoding.UTF8.GetBytes(inputData);
 
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        TripleDESCryptoServiceProvider tripalDES = new TripleDESCryptoServiceProvider();
 
        tripalDES.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hasKey));
        tripalDES.Mode = CipherMode.ECB;
 
        ICryptoTransform trnsfrm = tripalDES.CreateEncryptor();
        byte[] result = trnsfrm.TransformFinalBlock(bData0bData.Length);
 
        return Convert.ToBase64String(result);
    }
 
    //MD5 -  Decryption
    public string MD5Decryption(string inputData)
    {
        string hasKey = "Password@SudhirKotila12:34"//You can use any string here as haskey
        byte[] bData = Convert.FromBase64String(inputData);
 
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        TripleDESCryptoServiceProvider tripalDES = new TripleDESCryptoServiceProvider();
 
        tripalDES.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hasKey));
        tripalDES.Mode = CipherMode.ECB;
 
        ICryptoTransform trnsfrm = tripalDES.CreateDecryptor();
        byte[] result = trnsfrm.TransformFinalBlock(bData0bData.Length);
 
        return UTF8Encoding.UTF8.GetString(result);
    }

Input: This is MD5 Demo

Output: VYnMMSoeuy3jLwl3d6otyX33NBd7NibG


4) XOR:

- XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating encryption keys to match with the correct one

- This operation is sometimes called modulus 2 addition (or subtraction) process.

- The Logic is, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.

For More info: https://en.wikipedia.org/wiki/XOR_cipher

Example:


    using System.Text;

    //XOR - Encription & Decryption
    public string XOREncryptDecrypt(string inputData)
    {
        StringBuilder outSB = new StringBuilder(inputData.Length);
        for (int i = 0i < inputData.Lengthi++)
        {     
            //Here 1234 is key for Encrypt/Decrypt, You can use any int number
            char ch = (char(inputData[i] ^ 1234); 
            outSB.Append(ch);
        }
        return outSB.ToString();
    }

 Input: This is XOR Demo

Output :  ҆ҺһҡӲһҡӲҊҝҀӲҖҷҿҽ


Download Unity Demo Project: https://drive.google.com/drive/folders/1oedqWl7skJO88UeQnZVnik7_l-i6vGhn?usp=sharing

Comments

Popular posts from this blog

GDLC [Game Development Life Cycle]

DEVELOPER’S GUIDE TO UNITY3D MMO WITH NODE.JS USING SOCKET.IO

Unity Tiny | Tiny Mode | Instant Games | Playable Ads

NAMING CONVENTIONS | CODING CONVENTIONS | PROGRAMMING GUID

DOTS - Unity3d

Unity3d – Attributes

Unity's Back-end System

ANDROID GO - LIGHTER, SMARTER & OPTIMIZED ANDROID OS