using System.Security.Cryptography;
using Microsoft.VisualBasic;
public static void Main()
Console.WriteLine(Criptografia.Criptografar("banana", "minhachave123"));
public static class Criptografia
private static string Hex(string value)
uint uiDecimal = checked(Convert.ToUInt32(value));
return String.Format("{0:x2}", uiDecimal);
catch(OverflowException oExp)
throw new ApplicationException(oExp.Message);
private static uint UndoHex(string value)
uint uiHex = Convert.ToUInt32(value, 16);
catch(OverflowException oExp)
throw new ApplicationException(oExp.Message);
public static string Decriptografar(string text, string key)
string sTextoTratado = string.Empty;
int iTamChave = key.Length;
uint iCompoeCod = UndoHex(Microsoft.VisualBasic.Strings.Left(text, 2));
for(iTextoPos = 3; iTextoPos <= text.Length; iTextoPos += 2)
uint iTextoAsc = UndoHex(Strings.Mid(text, iTextoPos, 2));
if(iChavePos < iTamChave) iChavePos++;
uint iTmpTextoAsc = iTextoAsc ^ (uint) Strings.Asc(Strings.Mid(key, iChavePos, 1));
if(iTmpTextoAsc <= iCompoeCod) iTmpTextoAsc = 255 + iTmpTextoAsc - iCompoeCod;
else iTmpTextoAsc -= iCompoeCod;
sTextoTratado += Strings.Chr((int) iTmpTextoAsc);
public static string Criptografar(string text, string key)
var oRandom = new Random(DateTime.Now.Millisecond);
int iTamChave = key.Length;
int iMultiplication = ((DateTime.Now.Year*5) + ((int) DateTime.Now.DayOfWeek*DateTime.Now.Hour) + DateTime.Now.Minute + DateTime.Now.Second);
while(iMultiplication < 10000)
iMultiplication += DateTime.Now.Year;
double a = oRandom.NextDouble();
var iCompoeCod = (uint) ((a*iMultiplication%255) + 1);
iCompoeCod += (uint) DateTime.Now.Second;
string sTextoTratado = Hex(iCompoeCod.ToString());
for(iTextoPos = 0; iTextoPos <= text.Length - 1; iTextoPos++)
string sChar = text.Substring(iTextoPos, 1);
iTextoAsc = (uint) ((Strings.Asc(sChar) + iCompoeCod)%255);
if(iChavePos < iTamChave) iChavePos = iChavePos + 1;
iTextoAsc = (uint) (iTextoAsc ^ Strings.Asc(Strings.Mid(key, iChavePos, 1)));
sTemp = Hex(iTextoAsc.ToString());
if(sTemp.Length == 1) sTemp = "0" + sTemp;
sTextoTratado = sTextoTratado + sTemp;
public static string MD5Hash(string text)
byte[] inputBytes = Encoding.ASCII.GetBytes(text);
byte[] hash = md5.ComputeHash(inputBytes);
var sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("X2"));