← Tutti gli articoli

ASP.NET MEMBERSHIP CRYPTOGRAPHY PASSWORDSALT SHA1

26 novembre 2010  · N/A · Article  ·  12 visite

ASP.NET MEMBERSHIP CRYPTOGRAPHY PASSWORDSALT SHA1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string password="prova";

           // HashAlgorithm ha = HashAlgorithm.Create("SHA1");//Membership.HashAlgorithmType);
            string dbEncodedPassword = "pVIoR4xjUsgiIUsYvj3vj2xEJXU=";
            string oldEncodedSalt = "Zo657DZKBmXMyekzee9yGw==";
            string NewEncodedPassword = EncodePassword("prova", dbEncodedPassword);
            string r = "";

            //byte[] oldSalt = Convert.FromBase64String(oldEncodedSalt);
            //byte[] bytePassword = Encoding.Unicode.GetBytes(password);
            //byte[] inputBuffer = new byte[bytePassword.Length + 16];

            //Buffer.BlockCopy(bytePassword, 0, inputBuffer, 0, bytePassword.Length);
            //Buffer.BlockCopy(oldSalt, 0, inputBuffer, bytePassword.Length, 16);

            //byte[] bhashedPassword = ha.ComputeHash(inputBuffer);
            //string hashedPassword = Convert.ToBase64String(bhashedPassword);
            //if (hashedPassword == oldEncodedPassword)
            //    password = "";
            ////////HashAlgorithm passwordHasher = HashAlgorithm.Create("SHA1");

            ////////byte[] saltBytes = Convert.FromBase64String(oldEncodedSalt);
            ////////byte[] passwordBytes = Encoding.UTF8.GetBytes(oldEncodedPassword);
            ////////byte[] bytesToHash = new byte[saltBytes.Length + passwordBytes.Length];
            ////////saltBytes.CopyTo(bytesToHash, 0);
            ////////passwordBytes.CopyTo(bytesToHash, saltBytes.Length);
            ////////byte[] hash = passwordHasher.ComputeHash(bytesToHash);
            ////////string base64Hash = Convert.ToBase64String(hash);
            //return user.Membership.Password == base64Hash :


        }

        public string EncodePassword2(string pass, string saltBase64)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(pass);
            byte[] src = Convert.FromBase64String(saltBase64);
            byte[] dst = new byte[src.Length + bytes.Length];
            Buffer.BlockCopy(src, 0, dst, 0, src.Length);
            Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
            HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
            byte[] inArray = algorithm.ComputeHash(dst);
            return Convert.ToBase64String(inArray);
        }

        private static string GetSalt()
        {
            byte[] buf = new byte[16];
            (new RNGCryptoServiceProvider()).GetBytes(buf);
            return Convert.ToBase64String(buf);
        }

        private static string EncodePassword(string pass, string salt)
        {
            byte[] bIn = Encoding.Unicode.GetBytes(pass);
            byte[] bSalt = Convert.FromBase64String(salt);
            byte[] bAll = new byte[bSalt.Length + bIn.Length];
            byte[] bRet;

            Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
            Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
            HashAlgorithm s = HashAlgorithm.Create("SHA1");
            bRet = s.ComputeHash(bAll);

            return Convert.ToBase64String(bRet);
        }
     


    }
}