// 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;
}
}
}