Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. public class Example
  5. {
  6. public static void Main()
  7. {
  8. Console.WriteLine("Please input your sentence you want to encode");
  9. string stringA = Console.ReadLine();
  10. string[] strings = { stringA };
  11. Encoding asciiEncoding = Encoding.ASCII;
  12.  
  13. // Create array of adequate size.
  14. byte[] bytes = new byte[49];
  15. // Create index for current position of array.
  16. int index = 0;
  17.  
  18. Console.WriteLine("Strings to encode:");
  19. foreach (var stringValue in strings)
  20. {
  21. Console.WriteLine(" {0}", stringValue);
  22.  
  23. int count = asciiEncoding.GetByteCount(stringValue);
  24. if (count + index >= bytes.Length)
  25. Array.Resize(ref bytes, bytes.Length + 50);
  26.  
  27. int written = asciiEncoding.GetBytes(stringValue, 0,
  28. stringValue.Length,
  29. bytes, index);
  30.  
  31. index = index + written;
  32. }
  33. Console.WriteLine("\nASCII values of your sentence:");
  34. Console.WriteLine("{0}", ShowByteValues(bytes, index));
  35. Console.WriteLine();
  36.  
  37.  
  38. Console.WriteLine("What is your cipher key?");
  39. var cipherKey = Console.ReadLine();
  40. Console.WriteLine("Your cipher key is " + cipherKey);
  41.  
  42. // Decode Unicode byte array to a string.
  43. string newString = asciiEncoding.GetString(bytes, 0, index);
  44. Console.WriteLine("Your sentence is {0}", newString);
  45. Console.WriteLine("The cipher key is " + cipherKey);
  46. Console.ReadLine();
  47. }
  48.  
  49.  
  50. private static string ShowByteValues(byte[] bytes, int last)
  51. {
  52. string returnString = " ";
  53. for (int ctr = 0; ctr <= last - 1; ctr++)
  54. {
  55. if (ctr % 20 == 0)
  56. returnString += "\n ";
  57. returnString += String.Format("{0:X2} ", bytes[ctr]);
  58. }
  59. return returnString;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement