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 am 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.
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.
- string output = "";
-
- 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 = "";
-
-
-
-
- ArrayList asciiArr = new ArrayList();
-
- for (int i = 0; i < inputString.Length; i++) {
- asciiArr.Add((int) Convert.ToChar(inputString[i]));
- }
-
-
-
-
- ArrayList atozArr = new ArrayList();
-
- for (int i = 0, j = 65; i < 26; i++, j++) {
- atozArr.Add((char) j);
- }
-
-
-
-
-
- int positionAscii = Convert.ToInt32(atozArr[(new Random()).Next(26)]);
-
-
-
-
- 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;
- }
-
-
-
-
- if (inputString.Length.Equals(cipherText.Length)) {
- cipherText = cipherText + (char) positionAscii;
- }
-
-
-
- return cipherText;
- }
-
- private static string decryption(string cipherText) {
-
-
-
- int positionAscii = Convert.ToInt32(cipherText[cipherText.Length - 1]);
-
-
-
-
- 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 = "";
-
- 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();
-
- }
- }
- }
For more updates visit on BlogSpot.