1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
static string EncryptFile(string chaineCharactereEntree,
string sKey)
{
string chaineEncrypt=null;
try
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] bytearrayinput = new byte[chaineCharactereEntree.Length];
//memory stream utilisé en mémoire pour capter la chaine cryptée.
MemoryStream ms = new MemoryStream(bytearrayinput);
ms.Position = 0;
CryptoStream cryptostream = new CryptoStream(ms,
desencrypt,
CryptoStreamMode.Write);
//recupere la chaine de caractère et met dans un tableau de byte
byte[] bytearrayinput1 = new byte[chaineCharactereEntree.Length];
bytearrayinput1 = System.Text.Encoding.ASCII.GetBytes(chaineCharactereEntree);
//ecrit dans le ms le tableau de byte dans le memory stream
cryptostream.Write(bytearrayinput1, 0, bytearrayinput1.Length);
cryptostream.Flush();
ms.Close();
//récupère le tableau de byte rempli et le converti en string
byte[] byteFin = new byte[bytearrayinput.Length];
ms.Write(byteFin, 0, byteFin.Length);
//on ferme le memory stream
return System.Text.Encoding.ASCII.GetString(byteFin);
}
catch (Exception ex)
{
throw ex;
}
return chaineEncrypt;
} |
Partager