Advertisement
Flavio1234

Programa de login/ e encriptação de de senha

Sep 20th, 2020
1,804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace ProgramadeLogin
  12. {
  13.     public partial class FrmMenu : Form
  14.     {
  15.         public FrmMenu()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void FrmMenu_FormClosed(object sender, FormClosedEventArgs e)
  21.         {
  22.             //fecha a aplicação por completo
  23.             Application.Exit();
  24.         }
  25.  
  26.    
  27.  
  28.        
  29.     }
  30. }
  31. using System;
  32. using System.Collections.Generic;
  33. using System.ComponentModel;
  34. using System.Data;
  35. using System.Drawing;
  36. using System.Linq;
  37. using System.Text;
  38. using System.Threading.Tasks;
  39. using System.Windows.Forms;
  40. using System.Security.Cryptography;
  41.  
  42. namespace ProgramadeLogin
  43. {
  44.     public partial class frmLogin : Form
  45.     {
  46.         //dados do utilizador
  47.         string utilizador = "Flavio";
  48.         string password = "1234";
  49.  
  50.  
  51.  
  52.         public frmLogin()
  53.         {
  54.             InitializeComponent();
  55.         }
  56.  
  57.        
  58.  
  59.         private void btn_cancelar_Click(object sender, EventArgs e)
  60.         {
  61.             //fechar a aplicação
  62.             Application.Exit();
  63.         }
  64.  
  65.         private void btn_entrar_Click(object sender, EventArgs e)
  66.         {
  67.             //verificar se o login é correto
  68.             if (text_utilizador.Text == "" || text_password.Text == "")
  69.                 return;
  70.  
  71.             bool entrar = true;
  72.  
  73.    
  74.            
  75.             string codigo = CriarMD5(text_password.Text);
  76.      
  77.  
  78.  
  79.             //verifica se houver falhas de introdução dos dados
  80.             if (text_utilizador.Text != utilizador || codigo != CriarMD5(password))
  81.             {
  82.                 MessageBox.Show("Dados de login invalidos.");
  83.                 entrar = false;
  84.             }
  85.            
  86.  
  87.  
  88.             //se o login é correto, abre o menu principal
  89.             if (entrar)
  90.             {
  91.                 this.Hide();
  92.                 FrmMenu f = new FrmMenu();
  93.                 f.ShowDialog();
  94.             }
  95.         }
  96.  
  97.         private void check_visualizar_pasasword_CheckedChanged(object sender, EventArgs e)
  98.         {
  99.             //visualiza ou esconde o texto da senha
  100.             if (check_visualizar_pasasword.Checked)
  101.             {
  102.                 //mostra a passaword
  103.                 text_password.PasswordChar = '\0';
  104.  
  105.             }
  106.             else
  107.             {
  108.                 //esconde a password
  109.                 text_password.PasswordChar = '*';
  110.  
  111.             }
  112.         }
  113.  
  114.         private string CriarMD5(string texto)
  115.         {
  116.             //encriptar com o algoritimo MD5 o texto fornecido
  117.  
  118.             MD5 criador = MD5.Create();
  119.             byte[] inputs = Encoding.ASCII.GetBytes(texto);
  120.             byte[] hash = criador.ComputeHash(inputs);
  121.  
  122.             StringBuilder final = new StringBuilder();
  123.             for (int i = 0; i < hash.Length; i++)
  124.             {
  125.                 final.Append(hash[i].ToString("X2"));  
  126.  
  127.             }
  128.                 return final.ToString();
  129.         }
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement