[C#] Créer un logiciel de gestion de mot de passe

Paradise GTP

Premium
Inscription
30 Juin 2013
Messages
4 271
Réactions
4 414
Points
24 105
RGCoins
554
1

Salut tout le monde.
Donc suite à mon tutoriel pour faire des mots de passe " crypter " je vous propose un tutoriel, ou je vais vous apprendre à créer un logiciel qui vous permettra d'y stocker vos nom de compte, mot de passe etc sur les site ou vous êtes inscrit.
Bien sur ce logiciel est protéger par un mot de passe que seulement vous connaissez !
2

Donc vous aurez besoin de 3 form :

Form 1 :

3

Sur cette form :
- 1 textbox
- 1 linklabel
- 1 bouton
- 1 label

Form2 :

4

Sur elle :
- 3 label
- 3 textbox
textbox 1 : Haut
textbox 2 : Milieu
textbox 3 : Bas
- 1 bouton

Form3 :
5

Pour finir sur elle :
- 2 listBox
- 5 textBox
Elles vont de haut en bas
- 4 boutons
Bouton 1 : Nouveau Compte
Bouton 2 : Supprimer compte
Bouton 3 : Sauvegarder ( Perso j'ai mis une image vous pouvez mettre un texte )
Bouton 4 : Quitter l'application
6

Il vous faudra donc ensuite 3 autres classes.
Donc voila la présentation étant faites on va passer au tutoriel


Donc avant de commencé à vous donner le code je vais vous montrez comment ajouter des form & des classe.
Il vous faut vous rendre dans projet :

7

Ensuite ' Ajouter un formulaire Windows '
Puis
8

Vous aurez des form & classe.

9

Donc je vais commencer par les classes.
La première, vous l'appelez : Compte, cela donnera Compte.cs
Donc le code est :
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsFormsApplication6
{
public class Compte
{
public int _identifiant;
public String _Nom_Du_Site;
public String _adresse_Mail;
public String _nom_Utilisateur;
public String _mot_De_Passe;
public String _info_Complémentaires;

public Compte()
{

}

public Compte(int id)
{
_Nom_Du_Site = "";
_adresse_Mail = "";
_mot_De_Passe = "";
_nom_Utilisateur = "";
_info_Complémentaires = "";
_identifiant = id;
}

public Compte(String nomsite, String mail, String mdp, String nomuser, String info, int id)
{
_Nom_Du_Site = nomsite;
_adresse_Mail = mail;
_mot_De_Passe = mdp;
_nom_Utilisateur = nomuser;
_info_Complémentaires = info;
_identifiant = id;
}
}
}
Donc vous n'avez que une chose à modifier, le namespace par le votre :

10

Donc ensuite on passe à la seconde que vous appelez : Crypto
Le code
using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication6
{
public enum CryptAlgo
{
DES = 1,
TripleDES = 2,
RC2 = 3,
Rijndael = 4
}
public class Crypto2
{
private CryptAlgo prvAlgorithme;
private string prvPass;

#region "Constructeurs et Destructeurs"
public Crypto2()
: this(CryptAlgo.DES)
{
}

public Crypto2(CryptAlgo Algo)
{
prvAlgorithme = Algo;
}

public Crypto2(string Algo)
: this(GetAlgo(Algo))
{
}

public Crypto2(CryptAlgo Algo, string pass)
{
prvAlgorithme = Algo;
prvPass = pass;
}

public Crypto2(string Algo, string pass)
: this(GetAlgo(Algo), pass)
{
}
#endregion

#region "Méthodes statiques"
static public string[] GetAlgoEnum()
{
string[] Algo = { "DES", "3DES", "RC2", "Rijndael" };
return Algo;
}

static public string GetAlgo(CryptAlgo Alg)
{
switch (Alg)
{
case CryptAlgo.DES:
return ("DES");
case CryptAlgo.RC2:
return ("RC2");
case CryptAlgo.Rijndael:
return ("Rijndael");
case CryptAlgo.TripleDES:
return ("3DES");
default:
return ("DES");
}
}

static public CryptAlgo GetAlgo(string Alg)
{
switch (Alg.ToUpper())
{
case "DES":
return (CryptAlgo.DES);
case "RC2":
return (CryptAlgo.RC2);
case "RIJNDAEL":
return (CryptAlgo.Rijndael);
case "3DES":
return (CryptAlgo.TripleDES);
default:
return (CryptAlgo.DES);
}
}
#endregion

#region "Propriétés publiques"
public string Password
{
set
{
prvPass = value;
}
}

public CryptAlgo Algorithme
{
get
{
return prvAlgorithme;
}
set
{
prvAlgorithme = value;
}
}
#endregion

#region "Méthodes publiques"
public void Crypter(string SourceFile, string DestinationFile)
{
System.IO.Stream Dest = null;
System.IO.FileStream Src = null;
System.Security.Cryptography.CryptoStream StreamCrypt = null;
System.Security.Cryptography.SymmetricAlgorithm Crypt = null;

if (prvPass == null)
throw new Exception("Le pass pour encryptage n'est pas renseigné!");

try
{
string pass;
switch (prvAlgorithme)
{
case (CryptAlgo.RC2):
Crypt = new System.Security.Cryptography.RC2CryptoServiceProvider();
break;
case (CryptAlgo.Rijndael):
Crypt = new System.Security.Cryptography.RijndaelManaged();
break;
case (CryptAlgo.TripleDES):
Crypt = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
break;
default:
Crypt = new System.Security.Cryptography.DESCryptoServiceProvider();
break;
}

Dest = new System.IO.FileStream(
DestinationFile,
System.IO.FileMode.Create);

Src = new System.IO.FileStream(
SourceFile,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);

pass = prvPass;

if (pass.Length < (Crypt.LegalKeySizes[0].MinSize / 8))
{
MessageBox.Show("Clé trop petite.");
}

if (pass.Length > (Crypt.LegalKeySizes[0].MaxSize / 8))
pass = pass.Substring(0, Crypt.LegalKeySizes[0].MaxSize / 8);

string entete = "T:";
switch (prvAlgorithme)
{
case CryptAlgo.DES:
entete += "01";
break;
case CryptAlgo.TripleDES:
entete += "02";
break;
case CryptAlgo.RC2:
entete += "03";
break;
case CryptAlgo.Rijndael:
entete += "04";
break;
}
entete += ";P:" + pass.Length.ToString("00");
System.Diagnostics.Debug.WriteLine(entete);

Dest.Write(System.Text.Encoding.ASCII.GetBytes(entete), 0, 9);
Dest.Flush();

StreamCrypt = new System.Security.Cryptography.CryptoStream(
Dest,
Crypt.CreateEncryptor(
System.Text.Encoding.ASCII.GetBytes(pass),
System.Text.Encoding.ASCII.GetBytes(pass)
),
System.Security.Cryptography.CryptoStreamMode.Write);

byte[] Data = new byte[1024];
int count;
while ((count = Src.Read(Data, 0, 1024)) > 0)
{
StreamCrypt.Write(Data, 0, count);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
finally
{
if (StreamCrypt != null)
{
StreamCrypt.Flush();
StreamCrypt.Close();
}
if (Src != null)
Src.Close();
if (Dest != null)
Dest.Close();
}
}

public void Decrypter(string SourceFile, string DestinationFile)
{
System.IO.Stream Dest = null;
System.IO.FileStream Src = null;
System.Security.Cryptography.CryptoStream StreamCrypt = null;
System.Security.Cryptography.SymmetricAlgorithm Crypt = null;

if (prvPass == null)
MessageBox.Show("Le pass pour décryptage n'est pas renseigné!");

try
{
string pass;
switch (prvAlgorithme)
{
case (CryptAlgo.RC2):
Crypt = new System.Security.Cryptography.RC2CryptoServiceProvider();
break;
case (CryptAlgo.Rijndael):
Crypt = new System.Security.Cryptography.RijndaelManaged();
break;
case (CryptAlgo.TripleDES):
Crypt = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
break;
default:
Crypt = new System.Security.Cryptography.DESCryptoServiceProvider();
break;
}

Dest = new System.IO.FileStream(
DestinationFile,
System.IO.FileMode.Create);

Src = new System.IO.FileStream(
SourceFile,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);

pass = prvPass;

if (pass.Length < (Crypt.LegalKeySizes[0].MinSize / 8))
{
MessageBox.Show("Clé trop petite.");
}

if (pass.Length > (Crypt.LegalKeySizes[0].MaxSize / 8))
pass = pass.Substring(0, Crypt.LegalKeySizes[0].MaxSize / 8);

byte[] entete = new byte[9];
Src.Read(entete, 0, 9);
switch (prvAlgorithme)
{
case CryptAlgo.DES:
if (System.Text.Encoding.ASCII.GetString(entete, 0, 4) != "T:01")
MessageBox.Show("Type de cryptage incompatible!");
break;
case CryptAlgo.TripleDES:
if (System.Text.Encoding.ASCII.GetString(entete, 0, 4) != "T:02")
MessageBox.Show("Type de cryptage incompatible!");
break;
case CryptAlgo.RC2:
if (System.Text.Encoding.ASCII.GetString(entete, 0, 4) != "T:03")
MessageBox.Show("Type de cryptage incompatible!");
break;
case CryptAlgo.Rijndael:
if (System.Text.Encoding.ASCII.GetString(entete, 0, 4) != "T:04")
MessageBox.Show("Type de cryptage incompatible!");
break;
}
if (System.Text.Encoding.ASCII.GetString(entete, 5, 4) != ("P:" + pass.Length.ToString("00")))
MessageBox.Show("Type de cryptage incompatible!");

System.Diagnostics.Debug.WriteLine(System.Text.Encoding.ASCII.GetString(entete));

StreamCrypt = new System.Security.Cryptography.CryptoStream(
Dest,
Crypt.CreateDecryptor(
System.Text.Encoding.ASCII.GetBytes(pass),
System.Text.Encoding.ASCII.GetBytes(pass)
),
System.Security.Cryptography.CryptoStreamMode.Write);

byte[] Data = new byte[1024];
int count;
while ((count = Src.Read(Data, 0, 1024)) > 0)
{
StreamCrypt.Write(Data, 0, count);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
finally
{
if (StreamCrypt != null)
{
StreamCrypt.Flush();
try { StreamCrypt.Close(); }
catch { }
}
if (Src != null)
Src.Close();
if (Dest != null)
Dest.Close();
}
}

public bool is_pass_too_longer()
{
System.IO.Stream Dest = null;
System.Security.Cryptography.CryptoStream StreamCrypt = null;
System.Security.Cryptography.SymmetricAlgorithm Crypt = null;
switch (prvAlgorithme)
{
case (CryptAlgo.RC2):
Crypt = new System.Security.Cryptography.RC2CryptoServiceProvider();
break;
case (CryptAlgo.Rijndael):
Crypt = new System.Security.Cryptography.RijndaelManaged();
break;
case (CryptAlgo.TripleDES):
Crypt = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
break;
default:
Crypt = new System.Security.Cryptography.DESCryptoServiceProvider();
break;
}
if (prvPass.Length < (Crypt.LegalKeySizes[0].MinSize / 8))
{
return false;
}
try
{
Dest = new System.IO.FileStream(
"toto.tmp",
System.IO.FileMode.Create);
StreamCrypt = new System.Security.Cryptography.CryptoStream(
Dest,
Crypt.CreateEncryptor(
System.Text.Encoding.ASCII.GetBytes(prvPass),
System.Text.Encoding.ASCII.GetBytes(prvPass)
),
System.Security.Cryptography.CryptoStreamMode.Write);
}
catch
{
try
{
Dest.Close();
File.Delete("toto.tmp");
}
catch
{

}
return false;
}
return true;

}
#endregion
}
}

Ensuite la dernière : GestionCompte

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsFormsApplication6
{
public class GestionCompte
{
public List<Compte> _comptes;

public GestionCompte()
{
_comptes = new List<Compte>();
}
public int getNewIdentifiant()
{
int max = -1;
foreach (Compte c in _comptes)
{
if (c._identifiant > max) { max = c._identifiant; }
}
return max + 1;
}

public Compte getCompte(int id)
{
Compte retour = new Compte(getNewIdentifiant());
foreach (Compte c in _comptes)
{
if (c._identifiant ==id) { retour=c; }
}
return retour;
}

public void supprimerCompte(int id)
{
Compte c;
for (int i = 0; i < _comptes.Count; i++)
{
c = _comptes;
if (c._identifiant == id) { _comptes.RemoveAt(i); break; }
}
}

public void ajouterCompte(String nomsite, String mail, String mdp, String nomuser, String info)
{
_comptes.Add(new Compte(nomsite, mail, mdp, nomuser, info, getNewIdentifiant()));
}
}
}

Donc voila on passe au form.
Donc la form 1 pour rappeler :

11

Vous commencez par ajouter :
using System.IO;
Ensuite
12

public static string _path_with_mdp = Application.StartupPath + "/" + "donotRemove.pwd";
public static String _mdp_for_file = "unsupermotdepasse";
public static String _mdp;

Ensuite

13

Crypto2 cp = new Crypto2(CryptAlgo.RC2, _mdp_for_file);
if (File.Exists(_path_with_mdp))
{
cp.Decrypter(_path_with_mdp, "tmp.tmp");
StreamReader sr = new StreamReader("tmp.tmp");
_mdp = sr.ReadToEnd();
sr.Close();
File.Delete("tmp.tmp");
}
else
{
MessageBox.Show("vous n'avez pas encore de mot de passe, vous allez le définir", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
Form2 forme = new Form2();
forme.Show(this);
}

Voila pour la Form1, on passe donc à la form 2 pour rappel :

14

Donc comme l'autre
using System.IO;

Ensuite on passe au ' code '

15

[private Boolean _new;
Puis

16

public Form2(Boolean isNew)
{
InitializeComponent();
_new = isNew;
if (isNew)
{
textBox1.Enabled = false;
label1.Enabled = false;
}
}

Après on passe au code du bouton :

17

if(!_new && textBox1.Text!=Form1._mdp)
{
MessageBox.Show("L'ancien mot de passe n'est pas valide");
textBox3.Clear();
textBox2.Clear();
textBox1.Clear();
return;
}
else if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("Les mots de passe ne correspondent pas");
textBox3.Clear();
textBox2.Clear();
textBox1.Clear();
return;
}
else
{
Crypto2 cr3=new Crypto2(CryptAlgo.RC2,textBox2.Text);
if(!cr3.is_pass_too_longer())
{
MessageBox.Show("La taille du nouveau mot de passe est incorrecte");
textBox3.Clear();
textBox2.Clear();
textBox1.Clear();
return;
}
else if(!_new)
{
if (File.Exists(Application.StartupPath + "/" + "comptes2.xml"))
{
Crypto2 cr = new Crypto2(CryptAlgo.RC2, Form1._mdp);
cr.Decrypter(Application.StartupPath + "/" + "comptes2.xml", Application.StartupPath + "/" + "comptes.xml");
File.Delete(Application.StartupPath + "/" + "comptes2.xml");
cr = new Crypto2(CryptAlgo.RC2, textBox2.Text);
cr.Crypter(Application.StartupPath + "/" + "comptes.xml", Application.StartupPath + "/" + "comptes2.xml");
try
{
File.Delete(Application.StartupPath + "/" + "comptes.xml");
}
catch { }
writeNewMDP();
MessageBox.Show("opération effectuée");
}
Form1._mdp = textBox2.Text;
Close();
}
else if (_new)
{
writeNewMDP();
MessageBox.Show("Opération effectuée!");
Close();
}
}

Enfin pour finir sur elle :

18

private void writeNewMDP()
{
try
{
StreamWriter sw = new StreamWriter("tmp.tmp", false);
sw.Write(textBox2.Text);
sw.Flush();
sw.Close();
Crypto2 cr4 = new Crypto2(CryptAlgo.RC2, Form1._mdp_for_file);
cr4.Crypter("tmp.tmp", Form1._path_with_mdp);
File.Delete("tmp.tmp");
Form1._mdp = textBox2.Text;
}
catch { MessageBox.Show("Erreur de réécriture du pass"); }
}
}

Voila donc maintenant on passe à la dernière form

19

On commence :
using System.Xml.Serialization;
using System.IO;

20

public GestionCompte _gestion_Comptes;
_gestion_Comptes = new GestionCompte();

21

load_xml();
remplir_ListBox();

22

listBox1.Items.Clear();
listBox2.Items.Clear();
foreach (Compte c in _gestion_Comptes._comptes)
{
listBox1.Items.Add(c._Nom_Du_Site);
listBox2.Items.Add(c._identifiant);
}
tb_NomSite.Clear();
tb_Mdp.Clear();
tb_Mail.Clear();
tb_Info.Clear();
tb_Utilisateur.Clear();

public void load_xml()
{

if(File.Exists(Application.StartupPath + "/" + "comptes2.xml"))
{
Crypto2 cr = new Crypto2(CryptAlgo.RC2, Form1._mdp);
cr.Decrypter(Application.StartupPath + "/" + "comptes2.xml", Application.StartupPath + "/" + "comptes.xml");
}
try
{
XmlSerializer serialiseur = new XmlSerializer(typeof(GestionCompte));
TextReader reader = new StreamReader(Application.StartupPath + "/" + "comptes.xml");
_gestion_Comptes = (GestionCompte)serialiseur.Deserialize(reader);
reader.Close();
}
catch (Exception e)
{
try
{
XmlSerializer serialiseur = new XmlSerializer(typeof(GestionCompte));
StreamWriter writer = new StreamWriter(Application.StartupPath + "/" + "comptes.xml");
serialiseur.Serialize(writer, _gestion_Comptes);
writer.Close();
}
catch (Exception e2) { MessageBox.Show(e2.Message); }
}
try
{
File.Delete(Application.StartupPath + "/" + "comptes.xml");
}
catch { }
}

23
_gestion_Comptes.ajouterCompte("Nouveau compte", "", "", "", "");
remplir_ListBox();

You must be registered for see images attach


You must be registered for see images attach

if (listBox1.SelectedItems.Count != 0)
{
int id = Convert.ToInt32(listBox2.Items[listBox1.SelectedIndex]);
Compte c = _gestion_Comptes.getCompte(id);
c._adresse_Mail = tb_Mail.Text;
c._info_Complémentaires = tb_Info.Text;
c._mot_De_Passe = tb_Mdp.Text;
c._Nom_Du_Site = tb_NomSite.Text;
c._nom_Utilisateur = tb_Utilisateur.Text;
save_xml();
encrypter_Donnes();
remplir_ListBox();
}

You must be registered for see images attach

public void encrypter_Donnes()
{
Crypto2 cr = new Crypto2(CryptAlgo.RC2, Form1._mdp);
cr.Crypter(Application.StartupPath + "/" + "comptes.xml", Application.StartupPath + "/" + "comptes2.xml");
File.Delete(Application.StartupPath + "/" + "comptes.xml");
}

Et donc enfin pour finir !

You must be registered for see images attach

public void save_xml()
{
try
{
XmlSerializer serialiseur = new XmlSerializer(typeof(GestionCompte));
StreamWriter writer = new StreamWriter(Application.StartupPath + "/" + "comptes.xml");
serialiseur.Serialize(writer, _gestion_Comptes);
writer.Close();
}
catch (Exception e) { MessageBox.Show(e.Message); }
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(listBox2.Items[listBox1.SelectedIndex]);
Compte c = _gestion_Comptes.getCompte(id);
tb_Mail.Text = c._adresse_Mail;
tb_Info.Text = c._info_Complémentaires;
tb_Mdp.Text = c._mot_De_Passe;
tb_NomSite.Text = c._Nom_Du_Site;
tb_Utilisateur.Text = c._nom_Utilisateur;
}
catch { }
}
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Attention vous êtes sur le point de supprimer le compte " + tb_NomSite.Text + " Voulez vous continuer? ", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
int id = Convert.ToInt32(listBox2.Items[listBox1.SelectedIndex]);
_gestion_Comptes.supprimerCompte(id);
remplir_ListBox();
save_xml();
}
}
You must be registered for see images attach

Voila donc le code est fini.
C'est long hein ? Allez comme je suis gentil :)

30



You must be registered for see images attach

Envie de plus ? Retrouver mes vidéos sur
You must be registered for see images attach

J'espère que ce tutoriel vous aura fait plaisir.
Je vous souhaite une bonne journée ou soirée !

You must be registered for see images attach

Tutoriel écrit par Boosterz GTP spécialement pour le premium de RealityGaming.
Aucune reproduction sur un autre forum, partie publique, par une autre personne ne sera acceptée.
 
Dernière édition:
Je ne sais pas si c'est moi mais il y a quelques images qui ne s'affiche pas, comme par exemple celle-ci:
  • QN8ogSR.png
 
Moi aussi la plupart des images apparaissent comme l'a dit @YAM' D:
 
Image qui s'affichent pas x)
 
Moi aussi, certaine image ne fonctionne pas dommage ton logiciel me serait utile :P
 
Je trouve sa nul de ne pas mettre sa dans la section programmation
 
Salut , j'ai un problème...
Je lance l'appli , il dise qu'il n'y a pas de mot de passe , il m'emmene vers la Form2.
Je rentre le nouveau mdp , il dise que l'ancien mot de passe n'est pas valide...
Comment ajouté le mot de passe?

Merci
 
ou on télécharge le programme en C# ?
 
Les images sont plus là :(
 
Retour
Haut