online encrypt desencriptar decrypt c# md5

desencriptar - md5 encrypt c#



Calcula un hash MD5 a partir de una cadena (10)

Depende completamente de lo que estás tratando de lograr. Técnicamente, podrías tomar los primeros 12 caracteres del resultado del hash MD5, pero la especificación de MD5 es generar uno de 32 caracteres.

Reducir el tamaño del hash reduce la seguridad y aumenta las posibilidades de que se produzcan colisiones y se rompa el sistema.

Quizás si nos informa más acerca de lo que está tratando de lograr, podamos ayudarlo más.

Uso el siguiente código C # para calcular un hash MD5 a partir de una cadena. Funciona bien y genera una cadena hexagonal de 32 caracteres como esta: 900150983cd24fb0d6963f7d28e17f72

string sSourceData; byte[] tmpSource; byte[] tmpHash; sSourceData = "MySourceData"; //Create a byte array from source data. tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData); tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource); // and then convert tmpHash to string...

¿Hay alguna forma de usar código como este para generar una cadena hexagonal de 16 caracteres (o una cadena de 12 caracteres)? Una cadena hexadecimal de 32 caracteres es buena, pero creo que será aburrido para el cliente ingresar el código.


Encontré un método para crear un hash MD5

public static string CreateMD5(string input) { // Use input string to calculate MD5 hash using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); // Convert the byte array to hexadecimal string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); } return sb.ToString(); } }

La fuente: Clase MD5


Estaba tratando de crear una representación de cadena de hash MD5 utilizando LINQ, sin embargo, ninguna de las respuestas fueron soluciones LINQ, por lo tanto, agregar esto a la mezcla heterogénea de soluciones disponibles.

string result; using (MD5 hash = MD5.Create()) { result = String.Join ( "", from ba in hash.ComputeHash ( Encoding.UTF8.GetBytes(observedText) ) select ba.ToString("x2") ); }


Puede usar Convert.ToBase64String para convertir una salida de 16 bytes de MD5 a una cadena de caracteres ~ 24. Un poco mejor sin reducir la seguridad. ( j9JIbSY8HuT89/pwdC8jlw== para su ejemplo)


Soporte cadena y secuencia de archivos.

ejemplos

string hashString = EasyMD5.Hash("My String"); string hashFile = EasyMD5.Hash(System.IO.File.OpenRead("myFile.txt"));

-

class EasyMD5 { private static string GetMd5Hash(byte[] data) { StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) sBuilder.Append(data[i].ToString("x2")); return sBuilder.ToString(); } private static bool VerifyMd5Hash(byte[] data, string hash) { return 0 == StringComparer.OrdinalIgnoreCase.Compare(GetMd5Hash(data), hash); } public static string Hash(string data) { using (var md5 = MD5.Create()) return GetMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data))); } public static string Hash(FileStream data) { using (var md5 = MD5.Create()) return GetMd5Hash(md5.ComputeHash(data)); } public static bool Verify(string data, string hash) { using (var md5 = MD5.Create()) return VerifyMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data)), hash); } public static bool Verify(FileStream data, string hash) { using (var md5 = MD5.Create()) return VerifyMd5Hash(md5.ComputeHash(data), hash); } }


Supongo que es mejor usar codificación UTF-8 en la cadena MD5.

public static string MD5(this string s) { using (var provider = System.Security.Cryptography.MD5.Create()) { StringBuilder builder = new StringBuilder(); foreach (byte b in provider.ComputeHash(Encoding.UTF8.GetBytes(s))) builder.Append(b.ToString("x2").ToLower()); return builder.ToString(); } }


Un hash MD5 es de 128 bits, por lo que no puede representarlo en hex con menos de 32 caracteres ...


// given, a password in a string string password = @"1234abcd"; // byte array representation of that string byte[] encodedPassword = new UTF8Encoding().GetBytes(password); // need MD5 to calculate the hash byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword); // string representation (similar to UNIX format) string encoded = BitConverter.ToString(hash) // without dashes .Replace("-", string.Empty) // make lowercase .ToLower(); // encoded contains the hash you are wanting


StringBuilder sb= new StringBuilder(); for (int i = 0; i < tmpHash.Length; i++) { sb.Append(tmpHash[i].ToString("x2")); }


System.Text.StringBuilder hash = new System.Text.StringBuilder(); System.Security.Cryptography.MD5CryptoServiceProvider md5provider = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytes = md5provider.ComputeHash(new System.Text.UTF8Encoding().GetBytes(YourEntryString)); for (int i = 0; i < bytes.Length; i++) { hash.Append(bytes[i].ToString("x2")); //lowerCase; X2 if uppercase desired } return hash.ToString();