Created
August 28, 2013 21:08
-
-
Save bobixaka/6371313 to your computer and use it in GitHub Desktop.
Encrypt and decrypt any text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace Task_7_Encryption | |
{ | |
class Encryption | |
{ | |
static void Main() | |
{ | |
string message = "Write a program that encodes and decodes a string using given encryption key (cipher)."; | |
string key = "key"; | |
Console.WriteLine(Encrypt(message, key)); | |
Console.WriteLine(); | |
Console.WriteLine(Decrypt(Encrypt(message,key), key)); | |
Console.WriteLine(); | |
} | |
static string Encrypt(string message, string key) | |
{ | |
var encryptedMessage = new StringBuilder(message.Length); | |
for (int i = 0; i < message.Length; i++) | |
encryptedMessage.Append((char)(message[i] ^ key[i % key.Length])); | |
return encryptedMessage.ToString(); | |
} | |
static string Decrypt(string message, string key) | |
{ | |
return Encrypt(message, key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment