Search code examples
c#asp.netpassword-encryption

What is the encryption used in Asp.Net?


Good day!

I am new at Asp.Net and I want to re-create the registration of Asp.Net. I am trying to find the encryption code and insert command used by Asp.Net default. As the title suggests, I want to know what is the encryption method used? Is it a hash or md5 or other method and where exactly can I find the code?

Usually, the Register backend code has this command

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Username.Text, Email = Username.Text };
IdentityResult result = manager.Create(user, Password.Text);

I know that the signInManager is not the command, I tried inspecting IdentityResult but found nothing.

Thanks in advance.


Solution

  • To Encrypt the Password

    string Password = EncryptString(Password.Text);
    

    Here I'm Encrypting the Password Using UTF8... So You can Store the Encrypted Password In Database...

    public static string EncryptString(string str)
    {
        return Encrypt(str, "!#$a54?3");
    }
    
    private static string Encrypt(string stringToEncrypt, string sEncryptionKey)
    {
        
        byte[] key = { };
        byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
        byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)
        try
        {
            key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    

    To Decrypt the Password

    Same Way you can Decrypt the Password

    public static string DecryptString(string str)
    {
        return Decrypt(str, "!#$a54?3");
    }
    
    private static string Decrypt(string stringToDecrypt, string sEncryptionKey)
    {
        
        byte[] key = { };
        byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 }; //try {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        byte[] inputByteArray = new byte[stringToDecrypt.Length];
        try
        {
            key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(stringToDecrypt.Replace(" ", "+"));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            Encoding encoding = Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
    
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    

    Note:- Use the Same Key for Encrypt and Decrypt the Password ...Here I'm using !#$a54?3