Información:
Buenas amigos de AMSSpecialist después de mucho mirar he encontrado una buena función para encriptar y desencriptar contenidos en Rijndael, esta DLL solo trae dos funciones pero cada función con 7 parámetros para llevar a otro nivel nuestros contenidos encriptados, espero que os guste y la disfrutéis.
Funciones:
Código::
Descarga:
Buenas amigos de AMSSpecialist después de mucho mirar he encontrado una buena función para encriptar y desencriptar contenidos en Rijndael, esta DLL solo trae dos funciones pero cada función con 7 parámetros para llevar a otro nivel nuestros contenidos encriptados, espero que os guste y la disfrutéis.
Funciones:
- RijndaelEncrypt
- RijndaelDecrypt
Spoiler: Mostrar
Código::
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using RGiesecke.DllExport;
using System.Collections.Generic;
namespace RijndaelCrypt
{
internal static class RijndaelSimple
{
[DllExport("RijndaelEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string RijndaelEncrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
[DllExport("RijndaelDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string RijndaelDecrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
return plainText;
}
}
}
Descarga: