Exemplo n.º 1
0
        static public byte[] Encrypt(byte[] plainText, string passphrase)
        {
            byte[] encrypted;

            using (Rijndael rijAlg = Rijndael.Create())
            {
                rijAlg.KeySize = 256;
                rijAlg.Key     = sha256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(passphrase));
                rijAlg.IV      = md5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(IVPhrase));

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(plainText, 0, plainText.Length);
                        csEncrypt.FlushFinalBlock();
                        encrypted = msEncrypt.ToArray();
                    }
                }
                return(encrypted);
            }
        }
        static void Main(string[] args)
        {
            MD5Cng      cn         = new MD5Cng();
            string      input      = "ugkcyxxp";
            List <char> charsFound = new List <char>();
            int         counter    = 0;

            do
            {
                var newStr = $"{input}{counter}";
                var bytes  = cn.ComputeHash(Encoding.ASCII.GetBytes(newStr));
                var output = BitConverter.ToString(bytes).Replace("-", string.Empty);
                if (output.StartsWith("00000"))
                {
                    charsFound.Add(output[6]);
                    Console.WriteLine($"Match found! {output[5]}");
                }

                counter++;
            } while (charsFound.Count < 8);
            Console.WriteLine($"P1 Pass: {charsFound}");

            int p2counter = 0;

            char[] p2Password   = new char[8];
            int    p2CharsFound = 0;

            do
            {
                var newStr = $"{input}{p2counter}";
                var bytes  = cn.ComputeHash(Encoding.ASCII.GetBytes(newStr));
                var output = BitConverter.ToString(bytes).Replace("-", string.Empty);
                if (output.StartsWith("00000"))
                {
                    if (output[5] >= 48 && output[5] <= 55)
                    {
                        int idx = output[5] - 48;
                        if (p2Password[idx] == '\0')
                        {
                            p2Password[idx] = output[6];
                            Console.WriteLine($"Match found! {output[6]}");
                            p2CharsFound++;
                        }
                    }
                }

                p2counter++;
            } while (p2CharsFound < 8);

            Console.WriteLine($"PW: {new string(p2Password)}");
        }
Exemplo n.º 3
0
        public byte[] ComputeHash(byte[] abData, HashAlgorithmName HashAlgorithm)
        {
            byte[] abReturn = null;

            if (HashAlgorithm == HashAlgorithmName.MD5)
            {
                abReturn = _MD5Services.ComputeHash(abData);
            }
            else if (HashAlgorithm == HashAlgorithmName.SHA1)
            {
                abReturn = _SHA1Services.ComputeHash(abData);
            }
            else if (HashAlgorithm == HashAlgorithmName.SHA256)
            {
                abReturn = _SHA256Services.ComputeHash(abData);
            }
            else if (HashAlgorithm == HashAlgorithmName.SHA384)
            {
                abReturn = _SHA384Services.ComputeHash(abData);
            }
            else if (HashAlgorithm == HashAlgorithmName.SHA512)
            {
                abReturn = _SHA512Services.ComputeHash(abData);
            }

            return(abReturn);
        }
 static byte[] GetHash(string value)
 {
     using (MD5 hashAlgorithm = new MD5Cng())
     {
         return(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(value)));
     }
 }
Exemplo n.º 5
0
        protected Guid GetUserHash(string userName)
        {
            MD5Cng md5   = new MD5Cng();
            var    bytes = md5.ComputeHash(Encoding.ASCII.GetBytes(userName));

            return(new Guid(bytes));
        }
Exemplo n.º 6
0
 public static byte[] ToMD5(this byte[] s)
 {
     using (var md5 = new MD5Cng())
     {
         return(md5.ComputeHash(s));
     }
 }
Exemplo n.º 7
0
        public static void ComputeHashes(string fileName, out string md5, out string sha1, out string sha256)
        {
            sha1 = sha256 = md5 = null;
            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
                    {
                        var    md5Cng   = new MD5Cng();
                        byte[] checksum = md5Cng.ComputeHash(bufferedStream);
                        md5 = BitConverter.ToString(checksum).Replace("-", String.Empty);

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        var sha1Cng = new SHA1Cng();
                        checksum = sha1Cng.ComputeHash(bufferedStream);
                        sha1     = BitConverter.ToString(checksum).Replace("-", String.Empty);

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        var sha256Cng = new SHA256Cng();
                        checksum = sha256Cng.ComputeHash(bufferedStream);
                        sha256   = BitConverter.ToString(checksum).Replace("-", String.Empty);
                    }
                }
            }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }
        }
Exemplo n.º 8
0
        public Respone Login(LoginRequest request)
        {
            var res = DBServer.Instance.GetPassword(request.Account);

            if (res.IsSuccess)
            {
                var    oldPass  = res.Data.Rows[0]["password"].ToString();
                var    salt     = Convert.FromBase64String(res.Data.Rows[0]["salt"].ToString());
                var    saltpass = Encoding.UTF8.GetBytes(request.Password).Concat(salt).ToArray();
                MD5Cng md5      = new MD5Cng();
                var    crpPass  = Convert.ToBase64String(md5.ComputeHash(saltpass));

                if (oldPass == crpPass)
                {
                    Context.Login(request.Account);
                    return(StandResult(StandRespone.SuccessResult("登录成功")));
                }
                else
                {
                    return(StandResult(StandRespone.FailResult("登录失败,密码错误")));
                }
            }
            else
            {
                return(StandResult(res));
            }
        }
Exemplo n.º 9
0
 public byte[] Calculate(byte[] data)
 {
     using (var md5 = new MD5Cng())
     {
         return(md5.ComputeHash(data));
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// md5 hash
        /// </summary>
        public static string MD5(string input, int iterations, bool lowerCase = true)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(string.Empty);
            }

            if (iterations < 1)
            {
                iterations = 1;
            }

            var bytes1 = Encoding.UTF8.GetBytes(input);
            var bytes2 = new byte[] { 9, 81 };

            using (var hashAlgorithm = new MD5Cng())
            {
                for (var i = 0; i < iterations; i++)
                {
                    var bytes = new byte[bytes1.Length + bytes2.Length];
                    bytes1.CopyTo(bytes, 0);
                    bytes2.CopyTo(bytes, bytes1.Length);
                    bytes2 = hashAlgorithm.ComputeHash(bytes);
                }
                var hexString = BinaryUtil.ToHex(bytes2);
                return(lowerCase ? hexString.ToLowerInvariant() : hexString);
            }
        }
Exemplo n.º 11
0
 public static byte[] ToMD5Cng(this string s)
 {
     using (var md5 = new MD5Cng())
     {
         return(md5.ComputeHash(s.GetBytes()));
     }
 }
Exemplo n.º 12
0
        static void day4(int part = 1)
        {
            decimal i   = 0;
            MD5Cng  md5 = new MD5Cng();

            md5.Initialize();
            string hash;

            while (true)
            {
                hash = ByteArrayToString(md5.ComputeHash(Encoding.ASCII.GetBytes("iwrupvqb" + i.ToString())));
                if (part == 1)
                {
                    if (hash.StartsWith("00000"))
                    {
                        break;
                    }
                }
                else
                {
                    if (hash.StartsWith("000000"))
                    {
                        break;
                    }
                }
                ++i;
            }
            System.Console.WriteLine(hash + " | " + i);
        }
Exemplo n.º 13
0
    private static string GetHash(string s)
    {
        MD5           sec = new MD5Cng();
        ASCIIEncoding enc = new ASCIIEncoding();

        byte[] bt = enc.GetBytes(s);
        return(GetHexString(sec.ComputeHash(bt)));
    }
Exemplo n.º 14
0
 public static String ComputeMD5(byte[] bytes)
 {
     using (var hashAlgorithmImpl = new MD5Cng())
     {
         var hashBytes = hashAlgorithmImpl.ComputeHash(bytes);
         return(String.Concat(hashBytes.Select(b => b.ToString("x2"))));
     }
 }
Exemplo n.º 15
0
 private string GetETag(Stream stream)
 {
     using (var md5 = new MD5Cng())
     {
         md5.ComputeHash(stream);
         return(Convert.ToBase64String(md5.Hash));
     }
 }
Exemplo n.º 16
0
        public static string Md5Converter(string str)
        {
            var md5    = new MD5Cng();
            var data   = md5.ComputeHash(Encoding.Default.GetBytes(str));
            var pwdmd5 = data.Aggregate(string.Empty, (current, t) => current + t.ToString("x2"));

            return(pwdmd5);
        }
Exemplo n.º 17
0
 public static Guid ComputeGuidMD5(byte[] bytes)
 {
     using (var hashAlgorithmImpl = new MD5Cng())
     {
         var hashBytes = hashAlgorithmImpl.ComputeHash(bytes);
         return(new Guid(hashBytes));
     }
 }
Exemplo n.º 18
0
        public static byte[] Md5EncodeByte(string text)
        {
            var md5 = new MD5Cng();

            var oldbytes = Encoding.UTF8.GetBytes(text);

            return(md5.ComputeHash(Encoding.UTF8.GetBytes(text)));
        }
Exemplo n.º 19
0
        public byte[] MD5(string pwd)
        {
            byte[] encryptPWD = new ASCIIEncoding().GetBytes(pwd);
            //调用MD5类加密数据
            MD5Cng m = new MD5Cng();

            return(m.ComputeHash(encryptPWD));
        }
Exemplo n.º 20
0
 public byte[] CalculateHash(string filePath)
 {
     using (var file = File.OpenRead(filePath))
         using (var md5 = new MD5Cng())
         {
             return(md5.ComputeHash(file));
         }
 }
Exemplo n.º 21
0
 // 获得Md5字符串
 public static string GetMD5String(string str)
 {
     using (MD5 md5 = new MD5Cng())
     {
         byte[] values = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
         return(BitConverter.ToString(values).Replace("-", ""));
     }
 }
Exemplo n.º 22
0
        /**
         * Encrypt user password before storing it in the database
         */
        public static String EncryptPassword(TextBox txtPass)
        {
            // Encrypt password Start
            MD5Cng hashEnc = new MD5Cng();

            byte[] password   = Encoding.ASCII.GetBytes(txtPass.Text);
            byte[] hashedPass = hashEnc.ComputeHash(password);
            return(Encoding.ASCII.GetString(hashedPass));
        }
Exemplo n.º 23
0
            public static string Encrypt(string value)
            {
                HashAlgorithm algorithm = new MD5Cng();

                using (algorithm)
                {
                    return(BinaryToHex(algorithm.ComputeHash(Encoding.UTF8.GetBytes(value))));
                }
            }
Exemplo n.º 24
0
 private string FileChecksum(string fileName)
 {
     using (var stream = new BufferedStream(File.OpenRead(fileName), 1200000))
     {
         MD5Cng md5      = new MD5Cng();
         byte[] checksum = md5.ComputeHash(stream);
         return(BitConverter.ToString(checksum).Replace("-", string.Empty));
     }
 }
Exemplo n.º 25
0
 /// <summary>
 /// Creates an md5 hash of a file stored on disk.
 /// </summary>
 /// <param name="filename">The filename to generate hash for.</param>
 /// <returns>Base64 string representing the md5 hash generated.</returns>
 public string GetBase64EncodedMD5Hash(string filename)
 {
     using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         using (MD5Cng md5 = new MD5Cng())
         {
             return(hexStringFromBytes(md5.ComputeHash(fs)));
         }
     }
 }
Exemplo n.º 26
0
            public static string GetFileMd5(string filePath)
            {
                var md5  = new MD5Cng();
                var file = File.Open(filePath, FileMode.Open);
                var by   = md5.ComputeHash(file);

                file.Close();
                file.Dispose();
                return(BitConverter.ToString(by));
            }
Exemplo n.º 27
0
        public static string MD5Encode(string encodeable)
        {
            UnicodeEncoding uEncode = new UnicodeEncoding();

            byte[] bytD2e = uEncode.GetBytes(encodeable);
            MD5Cng sha    = new MD5Cng();

            byte[] hash = sha.ComputeHash(bytD2e);
            return(Convert.ToBase64String(hash));
        }
Exemplo n.º 28
0
        private static Int64 MD5HashWorker(Byte[] input)
        {
            Byte[] hashCode = null;

            using (MD5Cng md5 = new MD5Cng())
            {
                hashCode = md5.ComputeHash(input);
            }

            return(BitConverter.ToInt64(hashCode, 0));
        }
Exemplo n.º 29
0
    private static string GetMD5(byte[] file)
    {
        MD5Cng mD5 = new MD5Cng();

        byte[] code = mD5.ComputeHash(file);
        string md5  = BitConverter.ToString(code);

        md5 = md5.Replace("-", "");

        return(md5);
    }
Exemplo n.º 30
0
        public Respone Register(RegistrRequest request)
        {
            //加密密码
            byte[] salt = new byte[20];
            new Random().NextBytes(salt);
            MD5Cng md5      = new MD5Cng();
            var    saltpass = Encoding.UTF8.GetBytes(request.Password).Concat(salt).ToArray();
            var    crpPass  = md5.ComputeHash(saltpass);

            return(StandResult(DBServer.Instance.Regeister(request.Account, Convert.ToBase64String(crpPass), Convert.ToBase64String(salt))));
        }