Skip to content

Instantly share code, notes, and snippets.

@bobixaka
Created August 28, 2013 21:08
Show Gist options
  • Save bobixaka/6371313 to your computer and use it in GitHub Desktop.
Save bobixaka/6371313 to your computer and use it in GitHub Desktop.
Encrypt and decrypt any text
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