Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- //Task7:Write a program that encodes and decodes a string using given encryption key (cipher).
- //The key consists of a sequence of characters. The encoding/decoding is done by performing XOR (exclusive or)
- //operation over the first letter of the string with the first of the key, the second – with the second, etc.
- //When the last key character is reached, the next is the first.
- namespace Task7Cipher
- {
- class Cipher
- {
- static void Crypt(string text)
- {
- string t = text.Trim();
- string[] words = t.Split(' ', ',', '.');
- string key = "ab";
- int L = 0;
- ushort k = 0;
- List<ushort> crypted = new List<ushort>();
- ushort d, c;
- for (int i = 0; i < words.Length; i++)
- {
- L = words[i].Length;
- char[] dm = new char[L];
- for (int j = 0; j < L; j++)
- {
- dm = words[i].ToCharArray();
- d = (ushort)dm[j];
- c = (ushort)(d ^ (ushort)key[k]);
- crypted.Add(c);
- if (k == key.Length - 1)
- {
- k = (ushort)(k - key.Length);
- }
- k++;
- }
- }
- for (int element = 0; element < crypted.Count; element++)
- {
- Console.Write("\\u{1:x4}", crypted[element].ToString(), crypted[element]);
- }
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- string text = "Nakov this is a text for encoding ";
- Console.WriteLine(text);
- Crypt(text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement