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