// MyClass.cs // // Copyright (C) 2009 [José Antonio Martínez Torres | http://www.antoniomtz.org] // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // using System; using System.Security.Cryptography; using System.Text; namespace criptologia { public class Cifrado { private string key; public Cifrado() { /* Establecer una clave. La misma clave debe ser utilizada para descifrar los datos que son cifrados con esta clave. pueden ser los caracteres que uno desee*/ key = "ABCDEFGHIJKLMÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz"; } public string Encriptar(string texto) { //arreglo de bytes donde guardaremos la llave byte[] keyArray; //arreglo de bytes donde guardaremos el texto //que vamos a encriptar byte[] Arreglo_a_Cifrar = UTF8Encoding.UTF8.GetBytes(texto); //se utilizan las clases de encriptación //provistas por el Framework //Algoritmo MD5 MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); //se guarda la llave para que se le realice //hashing keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); //Algoritmo 3DAS TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; //se empieza con la transformación de la cadena ICryptoTransform cTransform = tdes.CreateEncryptor(); //arreglo de bytes donde se guarda la //cadena cifrada byte[] ArrayResultado = cTransform.TransformFinalBlock(Arreglo_a_Cifrar,0, Arreglo_a_Cifrar.Length); tdes.Clear(); //se regresa el resultado en forma de una cadena return Convert.ToBase64String(ArrayResultado,0, ArrayResultado.Length); } public string Desencriptar(string textoEncriptado) { byte[] keyArray; //convierte el texto en una secuencia de bytes byte[] Array_a_Descifrar = Convert.FromBase64String(textoEncriptado); //se llama a las clases que tienen los algoritmos //de encriptación se le aplica hashing //algoritmo MD5 MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(Array_a_Descifrar,0, Array_a_Descifrar.Length); tdes.Clear(); //se regresa en forma de cadena return UTF8Encoding.UTF8.GetString(resultArray); } } }