使用登录名和用户类型加密/解密

时间:2019-06-20 21:08:05

标签: c# .net winforms encryption

我正在对用户名/密码使用加密和解密并将其保存在数据库中,同时我将其与Usertype一起使用,用户可以使用自己的面板登录。

注册时,Encrypt可以正常工作并保存在数据库中,但是当我尝试登录时,输入的密码不起作用。我认为解密无法调用我输入的密码,并且无法登录界面。

我正在使用System.Security.Cryptograph和System.IO。

类文件

var synth = window.speechSynthesis;

// get voices
var voices = synth.getVoices();

// when voices have finished loading
synth.onvoiceschanged = function() {

  var sayThis = new SpeechSynthesisUtterance('Hi how are you?');
  sayThis.onend = function(event) {
    console.log('SpeechSynthesisUtterance.onend');
  }

  sayThis.onerror = function(event) {
    console.error('SpeechSynthesisUtterance.onerror');
  }

  var selectedVoice = 'Google UK English Female';
  for (var i = 0; i < voices.length; i++) {
    if (voices[i].name === selectedVoice) {
      sayThis.voice = voices[i];
      break;
    }
  }
  sayThis.pitch = 1;
  sayThis.rate = 1;

  synth.speak(sayThis);
}

登录文件

class Cryptography
    {
        public static string Encrypt(string clearText)
        {
            string EncryptionKey = "dk&;=GZ>j6KSev,<dm>cZG's$maAiD";
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }
        public static string Decrypt(string cipherText)
        {
            string EncryptionKey = "dk&;=GZ>j6KSev,<dm>cZG's$maAiD";
            cipherText = cipherText.Replace(" ", "+");
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }
    }

我得到的结果什么都没有。我一直按登录按钮,但仍然没有显示任何内容。由于密码已被加密,如何使用上面的登录代码解密密码?

2 个答案:

答案 0 :(得分:1)

soohoonigan said一样,您应该加密键入的用户密码,并将其与数据库中存储的密码进行比较。

您不需要解密它。

答案 1 :(得分:0)

  1. 是的,不要在屏幕级别解密密码。
  2. 使用屏幕和数据库中的加密密码进行比较。
  3. 如果登录正确,请提供消息。