Rutina creada en c# bajo el entorno monodevelop para cifrar las contraseñas de un usuario antes de grabarlas en la base de datos.
Aquí os dejo el código fuente programado en c#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | using System; using System.Security.Cryptography; using System.Text; using System.IO; namespace AesCrypt { public static class Cifrado { // se define el tamaño del key y del vector private static readonly int tamanyoClave = 32; private static readonly int tamanyoVector = 16; // Define la palabra clave para el cifrado y private static readonly string Clave = "Juan Sin Miedo"; private static readonly string Vector = "Estoes una prueba837723"; // se convierte el vector y la key a bytes public static byte[] Key = UTF8Encoding.UTF8.GetBytes(Clave); public static byte[] IV = UTF8Encoding.UTF8.GetBytes(Vector); public static string CifradoTexto(String txtPlano) { Array.Resize(ref Key, tamanyoClave); Array.Resize(ref IV, tamanyoVector); // se instancia el Rijndael Rijndael RijndaelAlg = Rijndael.Create(); // se establece cifrado MemoryStream memoryStream = new MemoryStream(); // se crea el flujo de datos de cifrado CryptoStream cryptoStream = new CryptoStream(memoryStream, RijndaelAlg.CreateEncryptor(Key, IV), CryptoStreamMode.Write); // se obtine la información a cifrar byte[] txtPlanoBytes = UTF8Encoding.UTF8.GetBytes(txtPlano); // se cifran los datos cryptoStream.Write(txtPlanoBytes, 0, txtPlanoBytes.Length); cryptoStream.FlushFinalBlock(); // se obtinen los datos cifrados byte[] cipherMessageBytes = memoryStream.ToArray(); // se cierra todo memoryStream.Close(); cryptoStream.Close(); // Se devuelve la cadena cifrada return Convert.ToBase64String(cipherMessageBytes); } /** * Descifra una cadena texto con el algoritmo de Rijndael * * @param txtCifrado mensaje cifrado * @return string texto descifrado (plano) */ public static string DescifradoTexto(String txtCifrado) { Array.Resize(ref Key, tamanyoClave); Array.Resize(ref IV, tamanyoVector); // se obtienen los bytes para el cifrado byte[] cipherTextBytes = Convert.FromBase64String(txtCifrado); // se almacenan los datos descifrados byte[] plainTextBytes = new byte[cipherTextBytes.Length];Cifrado Rijndael AES con C# // se crea una instancia del Rijndael Rijndael RijndaelAlg = Rijndael.Create(); // se crean los datos cifrados MemoryStream memoryStream = new MemoryStream(cipherTextBytes); // se descifran los datos CryptoStream cryptoStream = new CryptoStream(memoryStream, RijndaelAlg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); // se obtienen datos descifrados int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); // se cierra todo memoryStream.Close(); cryptoStream.Close(); // se devuelve los datos descifrados return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); } } } |