Tuesday, 22 March 2016

String encryption and decryption in C# console application

Encryption Like Student thought


Hi all engineering students and professionals who are currently interested in information security related topics. 

An algorithm may be similar to basic algorithms of Information Security books.

I am describe an algorithm with practical steps proves with C# code or you can perform it in any language it's your choice.

Graphic layout of tool works on this algorithm is depends on your thoughts.

I am programing with Console screen.



Plain Text :
Hello I am now developing an encryption program in C# 
 
Cipher Text :
”™¶½½Àqšq²¾q¿ÀÈqµ¶Ç¶½ÀÁº¿¸q²¿q¶¿´ÃÊÁźÀ¿qÁÃÀ¸Ã²¾qº¿q”tQ


Description of algorithm:
A simple algorithm works with & play with an ascii value of characters.

Algorithm works within 6 different steps.

Step : 1

Convert input string characters in respected ascii codes & store it in array like below mentioned example of C# code.

ArrayList asciiArr = new ArrayList();
            

for (int i = 0; i < inputString.Length; i++){
                asciiArr.Add((int)Convert.ToChar(inputString[i]));                
            }or ( i  =  0;  i  <  inputString.length;  i++ ) {

      asciiArr[i]  =  inputString[i].charCodeAt( 0 ); 

 

  

Step : 2

Fill A to Z array in capital or small letters

ArrayList atozArr = new ArrayList();
            
for (int i = 0, j = 65; i < 26; i++, j++)
{
   atozArr.Add((char) j); 

}

 

Step : 3

Choose randomly single character index from A to Z and differentiate it's position in one variable & it's respected ascii value in second variable

Note: Here i'm referencing an function return a random value from minimum - maximum range.

int positionAscii = Convert.ToInt32(atozArr[(new Random()).Next(26)]);

 

Step : 4

Addition of every input String each element to positionAscii

ArrayList outputArr = new ArrayList();
            
            for (int i = 0; i < inputString.Length; i++)
            {
                outputArr.Add(Convert.ToInt32(asciiArr[i]) + positionAscii);
            }
            
            int[] k = (int[])outputArr.ToArray(System.Type.GetType("System.Int32"));
            
            Console.Write("\nCipher Text: \n\n");
            
            foreach (int i in k)
            {
                cipherText = cipherText + (char)i;
            }

 

Step : 5

Attachment of key to encrypted string or not it's your choice & it's your modification scenario, here i'm attaching a key with an encrypted string

 

if (inputString.Length.Equals(cipherText.Length))
            {
                cipherText = cipherText + (char)positionAscii;
            }

 

Step : 6

Finally your encryption is ready to send

 

return cipherText;

Output Screenshot of Console screen



For decryption process reverse steps apply. 

Note: Every time cypher text will change due to selection of random position from A to Z

 

Below mention code is print your output cipher text to text file.

string output = "";
            // Write each directory name to a file. 
            using (StreamWriter sw = new StreamWriter("output.txt"))
            {
                output = encryption(input);   
                sw.WriteLine(output);
            } 
 

 

C# Code for Practice
function encryption(){                
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace Codeblank
{
    class Program
    {
        private static string encryption(string inputString)
        {
            string cipherText = "";
            
            //Step : 1
            //Convert input string characters in respected ascii codes & store it in array like below mentioned example of C# code.
            
            ArrayList asciiArr = new ArrayList();
            
            for (int i = 0; i < inputString.Length; i++)
            {
                asciiArr.Add((int)Convert.ToChar(inputString[i]));                
            }
            
            //Step : 2
            //Fill A to Z array in capital or small letters
            
            ArrayList atozArr = new ArrayList();
            
            for (int i = 0, j = 65; i < 26; i++, j++)
            {
                atozArr.Add((char) j); 
            }
            
            //Step : 3
            // Choose randomly single character index from A to Z and differentiate it's position in one variable & it's respected ascii value in second variable
            //Note: Here i'm referencing an function return a random value from minimum - maximum range. 
            
            int positionAscii = Convert.ToInt32(atozArr[(new Random()).Next(26)]);

            //Step : 4
            //Addition of every input String each element to positionAscii
            
            ArrayList outputArr = new ArrayList();
            
            for (int i = 0; i < inputString.Length; i++)
            {
                outputArr.Add(Convert.ToInt32(asciiArr[i]) + positionAscii);
            }
            
            int[] k = (int[])outputArr.ToArray(System.Type.GetType("System.Int32"));
            
            Console.Write("\nCipher Text: \n\n");
            
            foreach (int i in k)
            {
                cipherText = cipherText + (char)i;
            }

            //Step : 5
            //Attachment of key to encrypted string or not it's your choice & it's your modification scenario, here i'm attaching a key with an encrypted string 

            if (inputString.Length.Equals(cipherText.Length))
            {
                cipherText = cipherText + (char)positionAscii;
            }

            //Step : 6
            //Finally your encryption is ready to send
            return cipherText;
        }

        private static string decryption(string cipherText)
        {
            //Step : 1
            //Take decryption key from cipherText
            
            int positionAscii = Convert.ToInt32(cipherText[cipherText.Length - 1]);
            
            //Step : 2
            //Substract value of key from each characters of cipherText
            
            string plainText = "";
            
            foreach (int i in cipherText)
            {
                plainText = plainText + (char)(Convert.ToInt32(i) - positionAscii);
            }

            return plainText;
        }

        static void Main(string[] args)
        {
            Console.Write("Plain Text: \n\n");
            string input = Console.ReadLine();
            string output = "";
            // Write each directory name to a file. 
            using (StreamWriter sw = new StreamWriter("output.txt"))
            {
                output = encryption(input);   
                sw.WriteLine(output);
            }            
            Console.Write(output);
            Console.WriteLine("\n\nAfter Decryption Plain Text: \n\n" + decryption(output));
            Console.ReadKey();
            
        }
    }
}

     

2 comments:

  1. Ahh. Simple and interesting. Beautifully Plot. I like start and end of this article. I will visit again this site. Just keep Posting.

    encryption

    ReplyDelete