CeoneCrypto
Buenas después de varios dias de estar picando teclar y aprendiendo un poco como mierda funcionan los algoritmos de encriptación he creado una DLL con la cual podreis encriptar y desencryptar contenidos de manera fácil y rápida, por ahora en todas las pruebas que he echo a funciona bien, el unico problema que he detectado es altratar de desencriptar contenidos que no estan encriptados en el formato especifico la dll se cuelga exceptuando Twofish imagino que tiene que ver con el salt... pero bueno la verdad es que es la primera versión y ya estoy pensado en nuevas versiones con mas funciones mas opciones y a fin de cuenta mas y mas y mas cogido...
Codigo:
UnmanagedExports.cs:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using RGiesecke.DllExport;
using System.Collections.Generic;
using ManyMonkeys.Cryptography;
using System.Windows.Forms;
using BlowFishCS;
using CeoneCryptoA;
namespace Crypto
{
internal static class CeoneCrypto
{
/////////////////////////////////////////////////////////////
/////////////////////////// Other Functions from Crypto /////
/////////////////////////////////////////////////////////////
public static string GenerateSimpleSalt(int maxSize = 64)
{
var alphaSet = new char[64];
alphaSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890#!".ToCharArray();
var crypto = new RNGCryptoServiceProvider();
var bytes = new byte[maxSize];
crypto.GetBytes(bytes);
var tempSB = new StringBuilder(maxSize);
foreach (var b in bytes)
{
tempSB.Append(alphaSet[b % (alphaSet.Length)]);
}
return tempSB.ToString();
}
public static byte[] StrToByteArray(string str)
{
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetBytes(str);
}
/////////////////////////////////////////////////////////////
/////////////////////////// Encrypt and Decrypt Strings /////
/////////////////////////////////////////////////////////////
[DllExport("AESEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string AESEncrypt(string value, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("AESDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string AESDecrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
[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)
{
if (saltValue == null | saltValue == "")
{
saltValue = GenerateSimpleSalt(16);
}
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)
{
if (saltValue == null | saltValue == "")
{
saltValue = GenerateSimpleSalt(16);
}
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;
}
[DllExport("RC2Encrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string RC2Encrypt(string value, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
RC2 algorithm = RC2.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("RC2Decrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string RC2Decrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
RC2 algorithm = RC2.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
[DllExport("DESEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string DESEncrypt(string value, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
DES algorithm = DES.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("DESDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string DESDecrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
DES algorithm = DES.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
[DllExport("TripleDESEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string TripleDESEncrypt(string value, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
TripleDES algorithm = TripleDES.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("TripleDESDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string TripleDESDecrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
TripleDES algorithm = TripleDES.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
[DllExport("TwofishEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string TwofishEncrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = Twofish.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(text);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("TwofishDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string TwofishDecrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = Twofish.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
[DllExport("BlowfishEncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string BlowfishEncrypt(string text, string password)
{
BlowFish b = new BlowFish(password);
return b.Encrypt_CBC(text);
}
[DllExport("BlowfishDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string BlowfishDecrypt(string text, string password)
{
BlowFish b = new BlowFish(password);
return b.Decrypt_CBC(text);
}
[DllExport("XOREncrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string XOREncrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16 + 93488);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt + 93488));
SymmetricAlgorithm algorithm = XOR.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(text);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
[DllExport("XORDecrypt", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string XORDecrypt(string text, string password, string salt)
{
if (salt == null | salt == "")
{
salt = GenerateSimpleSalt(16 + 93488);
}
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt + 93488));
SymmetricAlgorithm algorithm = XOR.Create();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
/////////////////////////////////////////////////////////////
/////////////////////////// About Function of Crypto /////
/////////////////////////////////////////////////////////////
[DllExport("About", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string About()
{
Form1 Form1 = new Form1();
Form1.Show();
return "";
}
}
}
En cuanto pueda me pasare por paste2 y copiare el resto de códigos que me falta por poner...
Descarga: