Created
November 25, 2011 16:12
-
-
Save huogerac/1393883 to your computer and use it in GitHub Desktop.
RIJNDAEL - symmetric block cipher
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; | |
using System.Security.Cryptography; | |
using System.IO; | |
namespace Seguranca | |
{ | |
static class ClSeguranca | |
{ | |
static string CONST_CHAVE = "Chave_Exemplo"; | |
public static string Criptografar(string dados) | |
{ | |
string chave = CONST_CHAVE; | |
byte[] b = Encoding.UTF8.GetBytes(dados); | |
byte[] pw = Encoding.UTF8.GetBytes(chave); | |
RijndaelManaged rm = new RijndaelManaged(); | |
PasswordDeriveBytes pdb = new PasswordDeriveBytes(chave, new MD5CryptoServiceProvider().ComputeHash(pw)); | |
rm.Key = pdb.GetBytes(32); | |
rm.IV = pdb.GetBytes(16); | |
rm.BlockSize = 128; | |
rm.Padding = PaddingMode.PKCS7; | |
MemoryStream ms = new MemoryStream(); | |
CryptoStream cryptStream = new CryptoStream(ms, rm.CreateEncryptor(rm.Key, rm.IV), CryptoStreamMode.Write); | |
cryptStream.Write(b, 0, b.Length); | |
cryptStream.FlushFinalBlock(); | |
return System.Convert.ToBase64String(ms.ToArray()); | |
} | |
public static string Descriptografar(string sDados) | |
{ | |
string chave = CONST_CHAVE; | |
byte[] dados = System.Convert.FromBase64String(sDados); | |
byte[] pw = Encoding.UTF8.GetBytes(chave); | |
RijndaelManaged rm = new RijndaelManaged(); | |
PasswordDeriveBytes pdb = new PasswordDeriveBytes(chave, new MD5CryptoServiceProvider().ComputeHash(pw)); | |
rm.Key = pdb.GetBytes(32); | |
rm.IV = pdb.GetBytes(16); | |
rm.BlockSize = 128; | |
rm.Padding = PaddingMode.PKCS7; | |
MemoryStream ms = new MemoryStream(dados, 0, dados.Length); | |
CryptoStream cryptStream = new CryptoStream(ms, rm.CreateDecryptor(rm.Key, rm.IV), CryptoStreamMode.Read); | |
StreamReader sr = new StreamReader(cryptStream); | |
return sr.ReadToEnd(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment