Advertisement
minusa71

Task&/cipher

Feb 4th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. //Task7:Write a program that encodes and decodes a string using given encryption key (cipher).
  6. //The key consists of a sequence of characters. The encoding/decoding is done by performing XOR (exclusive or)
  7. //operation over the first letter of the string with the first of the key, the second – with the second, etc.
  8. //When the last key character is reached, the next is the first.
  9.  
  10.  
  11. namespace Task7Cipher
  12. {
  13. class Cipher
  14. {
  15. static void Crypt(string text)
  16. {
  17. string t = text.Trim();
  18. string[] words = t.Split(' ', ',', '.');
  19. string key = "ab";
  20. int L = 0;
  21. ushort k = 0;
  22. List<ushort> crypted = new List<ushort>();
  23. ushort d, c;
  24. for (int i = 0; i < words.Length; i++)
  25. {
  26. L = words[i].Length;
  27. char[] dm = new char[L];
  28. for (int j = 0; j < L; j++)
  29. {
  30. dm = words[i].ToCharArray();
  31. d = (ushort)dm[j];
  32. c = (ushort)(d ^ (ushort)key[k]);
  33. crypted.Add(c);
  34. if (k == key.Length - 1)
  35. {
  36. k = (ushort)(k - key.Length);
  37. }
  38. k++;
  39. }
  40.  
  41. }
  42. for (int element = 0; element < crypted.Count; element++)
  43. {
  44. Console.Write("\\u{1:x4}", crypted[element].ToString(), crypted[element]);
  45. }
  46. Console.WriteLine();
  47. }
  48.  
  49. static void Main(string[] args)
  50. {
  51. string text = "Nakov this is a text for encoding ";
  52. Console.WriteLine(text);
  53. Crypt(text);
  54.  
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement