using System; using System.Security.Cryptography; using System.Text; namespace Developpez.Dotnet.Security { /// /// Fournit des services de protection de données /// public class DataProtectionProvider { private readonly DataProtectionScope _scope; private readonly Encoding _encoding; private readonly byte[] _entropy; /// /// Crée une instance de DataProtectionProvider avec les paramètres par /// défaut. /// public DataProtectionProvider() : this(default(DataProtectionScope)) { } /// /// Crée une instance de DataProtectionProvider avec la portée spécifiée. /// /// Portée de la protection public DataProtectionProvider(DataProtectionScope scope) : this(scope, null) { } /// /// Crée une instance de DataProtectionProvider avec la portée et /// l'encodage spécifiés. /// /// Portée de la protection /// Encodage utilisé pour le texte public DataProtectionProvider(DataProtectionScope scope, Encoding encoding) : this(scope, encoding, null) { } /// /// Crée une instance de DataProtectionProvider avec la portée, /// l'encodage et l'entropie spécifiés. /// /// Portée de la protection /// Encodage utilisé pour le texte /// Entropie (optionnelle) pour augmenter la /// complexité du chiffrage public DataProtectionProvider(DataProtectionScope scope, Encoding encoding, byte[] entropy) { _scope = scope; _entropy = entropy; _encoding = encoding ?? Encoding.UTF8; } /// /// Portée de la protection /// /// /// Une valeur de DataProtectionScope indiquant si les données sont /// protégées pour l'utilisateur courant ou pour la machine locale. La /// valeur par défaut est CurrentUser. /// public DataProtectionScope Scope { get { return _scope; } } /// /// Encodage utilisé pour le texte /// /// /// L'encodage utilisé pour le texte. La valeur par défaut est /// Encoding.UTF8. /// public Encoding Encoding { get { return _encoding; } } /// /// Entropie du chiffrage /// /// /// Un tableau d'octets qui sera combiné à la clé principale pour /// augmenter la complexité du chiffrage. La valeur par défaut est null. /// public byte[] Entropy { get { return _entropy; } } /// /// Protège une chaine de caractères. /// /// La chaine à protéger /// La chaine protégée, encodée en base64 public string ProtectString(string s) { byte[] bytes = _encoding.GetBytes(s); byte[] protectedBytes = ProtectBytes(bytes); return Convert.ToBase64String(protectedBytes); } /// /// Déchiffre une chaine de caractères protégée. /// /// La chaine protégée à déchiffrer /// La chaine déchiffrée public string UnprotectString(string protectedString) { byte[] protectedBytes = Convert.FromBase64String(protectedString); byte[] bytes = UnprotectBytes(protectedBytes); return _encoding.GetString(bytes); } /// /// Protège des données binaires. /// /// Les données à protéger /// Les données protégées public byte[] ProtectBytes(byte[] bytes) { return ProtectedData.Protect(bytes, _entropy, _scope); } /// /// Déchiffre des données binaires protégées. /// /// Les données protégées à déchiffrer /// Les données déchiffrées public byte[] UnprotectBytes(byte[] protectedBytes) { return ProtectedData.Unprotect(protectedBytes, _entropy, _scope); } } }