Encryption-Decryption Like a Student Thought

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.

  1. ArrayList asciiArr = new ArrayList();  
  2.   
  3. for (int i = 0; i < inputString.Length; i++) {  
  4.     asciiArr.Add((int) Convert.ToChar(inputString[i]));  
  5. }  
  6. or(i = 0; i < inputString.length; i++) {  
  7.     asciiArr[i] = inputString[i].charCodeAt(0);  
  8. }  
Step 2: Fill A to Z array in capital or small letters.
  1. ArrayList atozArr = new ArrayList();  
  2.   
  3. for (int i = 0, j = 65; i < 26; i++, j++)  
  4. {  
  5.    atozArr.Add((char) j);  
  6. }  
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.
  1. ArrayList outputArr = new ArrayList();  
  2.   
  3. for (int i = 0; i < inputString.Length; i++) {  
  4.     outputArr.Add(Convert.ToInt32(asciiArr[i]) + positionAscii);  
  5. }  
  6.   
  7. int[] k = (int[]) outputArr.ToArray(System.Type.GetType("System.Int32"));  
  8.   
  9. Console.Write("\nCipher Text: \n\n");  
  10.   
  11. foreach(int i in k) {  
  12.     cipherText = cipherText + (char) i;  
  13. }  
Step 5: Attachment of key to encrypted string or not it's your choice & it's your modification scenario, here i am attaching a key with an encrypted string:
  1. if (inputString.Length.Equals(cipherText.Length))  
  2. {  
  3.    cipherText = cipherText + (char)positionAscii;  
  4. }  
Step 6: Finally your encryption is ready to send.
  1. return cipherText;  
cmd

For decryption process reverse steps apply.

Note: Every time cypher text will change due to selection of randomposition from A to Z.

Below mention code is print your output cipher text to text file.
  1. string output = "";  
  2. // Write each directory name to a file.   
  3. using (StreamWriter sw = new StreamWriter("output.txt"))  
  4. {  
  5.    output = encryption(input);   
  6.    sw.WriteLine(output);  
  7. }   
C# Code for Practice
  1. function encryption() {  
  2.     using System;  
  3.     using System.Collections.Generic;  
  4.     using System.Linq;  
  5.     using System.Text;  
  6.     using System.Collections;  
  7.     using System.IO;  
  8.   
  9.     namespace Codeblank {  
  10.         class Program {  
  11.             private static string encryption(string inputString) {  
  12.                 string cipherText = "";  
  13.   
  14.                 //Step : 1  
  15.                 //Convert input string characters in respected ascii codes & store it in array like below mentioned example of C# code.  
  16.   
  17.                 ArrayList asciiArr = new ArrayList();  
  18.   
  19.                 for (int i = 0; i < inputString.Length; i++) {  
  20.                     asciiArr.Add((int) Convert.ToChar(inputString[i]));  
  21.                 }  
  22.   
  23.                 //Step : 2  
  24.                 //Fill A to Z array in capital or small letters  
  25.   
  26.                 ArrayList atozArr = new ArrayList();  
  27.   
  28.                 for (int i = 0, j = 65; i < 26; i++, j++) {  
  29.                     atozArr.Add((char) j);  
  30.                 }  
  31.   
  32.                 //Step : 3  
  33.                 // 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  
  34.                 //Note: Here i'm referencing an function return a random value from minimum - maximum range.   
  35.   
  36.                 int positionAscii = Convert.ToInt32(atozArr[(new Random()).Next(26)]);  
  37.   
  38.                 //Step : 4  
  39.                 //Addition of every input String each element to positionAscii  
  40.   
  41.                 ArrayList outputArr = new ArrayList();  
  42.   
  43.                 for (int i = 0; i < inputString.Length; i++) {  
  44.                     outputArr.Add(Convert.ToInt32(asciiArr[i]) + positionAscii);  
  45.                 }  
  46.   
  47.                 int[] k = (int[]) outputArr.ToArray(System.Type.GetType("System.Int32"));  
  48.   
  49.                 Console.Write("\nCipher Text: \n\n");  
  50.   
  51.                 foreach(int i in k) {  
  52.                     cipherText = cipherText + (char) i;  
  53.                 }  
  54.   
  55.                 //Step : 5  
  56.                 //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   
  57.   
  58.                 if (inputString.Length.Equals(cipherText.Length)) {  
  59.                     cipherText = cipherText + (char) positionAscii;  
  60.                 }  
  61.   
  62.                 //Step : 6  
  63.                 //Finally your encryption is ready to send  
  64.                 return cipherText;  
  65.             }  
  66.   
  67.             private static string decryption(string cipherText) {  
  68.                 //Step : 1  
  69.                 //Take decryption key from cipherText  
  70.   
  71.                 int positionAscii = Convert.ToInt32(cipherText[cipherText.Length - 1]);  
  72.   
  73.                 //Step : 2  
  74.                 //Substract value of key from each characters of cipherText  
  75.   
  76.                 string plainText = "";  
  77.   
  78.                 foreach(int i in cipherText) {  
  79.                     plainText = plainText + (char)(Convert.ToInt32(i) - positionAscii);  
  80.                 }  
  81.   
  82.                 return plainText;  
  83.             }  
  84.   
  85.             static void Main(string[] args) {  
  86.                 Console.Write("Plain Text: \n\n");  
  87.                 string input = Console.ReadLine();  
  88.                 string output = "";  
  89.                 // Write each directory name to a file.   
  90.                 using(StreamWriter sw = new StreamWriter("output.txt")) {  
  91.                     output = encryption(input);  
  92.                     sw.WriteLine(output);  
  93.                 }  
  94.                 Console.Write(output);  
  95.                 Console.WriteLine("\n\nAfter Decryption Plain Text: \n\n" + decryption(output));  
  96.                 Console.ReadKey();  
  97.   
  98.             }  
  99.         }  
  100.     }  
For more updates visit on BlogSpot.