Les Snippets

Connexion

Encrypt Class

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 17/01/2008 21:00:41 et initié par DanMor498 [Liste]
Vue : 1323
Catégorie(s) : Sécurité
Langage sélectionné : C# 1.x
Langages dispo pour ce code :
- C# 1.x, C# 2.x
- Voir tous les langages pour ce code snippet



Langage : C# 1.x , C# 2.x
Date ajout : 17/01/2008
Posté par DanMor498 [Liste]

// Une Classe qui peut être utiliser avec vos projets.
// il s’agit  d'un hacheur Pour Mot de passe  [ sha1encrypt ] 
// d'un Encodeur / Décodeur de texte en sha512 [ GetEncryptedData | GetDecryptedData ]
// Et aussi le moyen de créer un fichier xml pour sauvegarder vos mot de passe encrypter
// a peut pres n'import-ou sur votre machine. et de vérifier votre mot de passe.

//****************************************************************//

// Pour utiliser le hacheur -  encrypt.sha1encrypt("Votre Mot de passe.trim()") 
// pour l'encodeur - rtftext.Text = encrypt.GetEncryptedData(rtftext.Rtf)
// pour le décodeur - rtftext.rtf = encrypt.GetDecryptedData(rtftext.Text)
// pour le XML - encrypt.WriteXMLT("Emplacement Voulu votre fichier", "Votre Mot de passe")
// pour le vérifier - if (encrypt.sha1encrypt(txtPassword.Text) == Value{}
// Bien sure il va vous falloir utiliser vos propre mot et textbox pour vous facilité la tache

//****************************************************************//
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace WindowsApplication2
{
    public class EncDec
    {

        ///'******* Encrypt the Data *******
        public string GetEncryptedData(string Data)
        {
            SHA512Managed shaM = new SHA512Managed();
            Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)));
            byte[] eNC_data = ASCIIEncoding.ASCII.GetBytes(Data);
            string eNC_str = Convert.ToBase64String(eNC_data);
            return eNC_str;  
        }

        ///'******* Decrypt the Data *******
        public string GetDecryptedData(string Data)
        {
            byte[] dEC_data = Convert.FromBase64String(Data);
            string dEC_Str = ASCIIEncoding.ASCII.GetString(dEC_data);
            return dEC_Str; 
        }

        public string sha1encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }
        public string byteArrayToString(byte[] inputArray)
        {
            StringBuilder output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }

        public string WriteXMLT(string ADR, string pwd)
        {
            // Creates XML file.
            // string ADR = "C:\\Windows\\System\\Edit";
            XmlTextWriter myXMLFileWriter = new XmlTextWriter(@ADR, null);
            myXMLFileWriter.WriteStartDocument();
            myXMLFileWriter.WriteStartElement("ENC_PWD");
            myXMLFileWriter.WriteString(pwd);
            myXMLFileWriter.WriteEndElement();
            myXMLFileWriter.Close();
            return pwd;
        }

        public string ReadXMLT(string ADR)
        {
            // Read the xml File. 
            string pwd = "";
             // string ADR = "C:\\Windows\\System\\Edit";
            XmlTextReader reader = new XmlTextReader(@ADR);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(reader);
            reader.Close();
            pwd = xmlDoc.InnerText;
            return pwd;
        }

    }
}


Remarque :
Soyer pas trop dure avec moi c'est ma premiere class en c#