begin process at 2008 08 22 04:54:24
1 229 775 membres
46 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

SAUVEGARDER DES DONNÉES SENSIBLES DANS LE FICHIER DE CONFIGURATION (CRYPTAGE) (.NET2)


Information sur la source

Description

J'ai une application de gestion de caméra IP pour laquelle je dois sauvegarder les paramètre de l'utilisateur.
Comme je dois sauvegarder des mots de passe, je sus obliger de protéger le fichier de sauvegarde.
On m'avait éguillé vers les IsolatedStorage (
Puis on m'a aiguiller vers le fichier de configuration de l'appli (http://www.csharpfr.com/infomsg_OU-COMMENT-SOUVEGARDER-MOTS-PASSE-TOUTE-SECURITE_752886.aspx et http://www.csharpfr.com/infomsg_APP-CONFIG_750223.aspx?p=2).
Après avoir trouver mon bonheur sur les MSDN ( http://msdn2.microsoft.com/fr-fr/library/system.configuration.dpapiprotectedconfigurationprovider.aspx ), je vous en fait également profiter.
Mon code n'est qu'une pale copie de celui des MSDN, a part que je crypte le AppSettings plutot que ConnectionStrings.

Source

  • void EncryptConfigFile()
  • {
  • if (appSetting != null)
  • {
  • if (!appSetting.SectionInformation.IsProtected)
  • {
  • if (!appSetting.ElementInformation.IsLocked)
  • {
  • // Protect the section.
  • appSetting.SectionInformation.ProtectSection(provider);
  • appSetting.SectionInformation.ForceSave = true;
  • config.Save(ConfigurationSaveMode.Full);
  • MessageBox.Show("Section "+appSetting.SectionInformation.Name+" is now protected by "+appSetting.SectionInformation.ProtectionProvider.Name);
  • }
  • else
  • MessageBox.Show(
  • "Can't protect, section {0} is locked",
  • appSetting.SectionInformation.Name);
  • }
  • else
  • MessageBox.Show(
  • "Section " + appSetting.SectionInformation.Name + " is already protected by " + appSetting.SectionInformation.ProtectionProvider.Name);
  • }
  • else
  • MessageBox.Show("Can't get the section "+ appSetting.SectionInformation.Name);
  • }
  • void DecryptConfigFile()
  • {
  • if (appSetting != null)
  • {
  • if (appSetting.SectionInformation.IsProtected)
  • {
  • if (!appSetting.ElementInformation.IsLocked)
  • {
  • // Unprotect the section.
  • appSetting.SectionInformation.UnprotectSection();
  • appSetting.SectionInformation.ForceSave = true;
  • config.Save(ConfigurationSaveMode.Full);
  • MessageBox.Show("Section " + appSetting.SectionInformation.Name + " is now unprotected.");
  • }
  • else
  • MessageBox.Show(
  • "Can't unprotect, section " + appSetting.SectionInformation.Name + " is locked");
  • }
  • else
  • MessageBox.Show(
  • "Section "+appSetting.SectionInformation.Name+" is already unprotected.");
  • }
  • else
  • MessageBox.Show("Can't get the section {"+appSetting.SectionInformation.Name);
  • }
        void EncryptConfigFile()
        {
            if (appSetting != null)
            {
                if (!appSetting.SectionInformation.IsProtected)
                {
                    if (!appSetting.ElementInformation.IsLocked)
                    {
                        // Protect the section.
                        appSetting.SectionInformation.ProtectSection(provider);

                        appSetting.SectionInformation.ForceSave = true;
                        config.Save(ConfigurationSaveMode.Full);

                        MessageBox.Show("Section "+appSetting.SectionInformation.Name+" is now protected by "+appSetting.SectionInformation.ProtectionProvider.Name);
                    }
                    else
                        MessageBox.Show(
                             "Can't protect, section {0} is locked",
                             appSetting.SectionInformation.Name);
                }
                else
                    MessageBox.Show(
                        "Section " + appSetting.SectionInformation.Name + " is already protected by " + appSetting.SectionInformation.ProtectionProvider.Name);
            }
            else
                MessageBox.Show("Can't get the section "+ appSetting.SectionInformation.Name);
        }

        void DecryptConfigFile()
        {
            if (appSetting != null)
            {
                if (appSetting.SectionInformation.IsProtected)
                {
                    if (!appSetting.ElementInformation.IsLocked)
                    {
                        // Unprotect the section.
                        appSetting.SectionInformation.UnprotectSection();

                        appSetting.SectionInformation.ForceSave = true;
                        config.Save(ConfigurationSaveMode.Full);

                        MessageBox.Show("Section " + appSetting.SectionInformation.Name + " is now unprotected.");

                    }
                    else
                        MessageBox.Show(
                             "Can't unprotect, section " + appSetting.SectionInformation.Name + " is locked");
                }
                else
                    MessageBox.Show(
                        "Section "+appSetting.SectionInformation.Name+" is already unprotected.");

            }
            else
                MessageBox.Show("Can't get the section {"+appSetting.SectionInformation.Name);

        }

Conclusion

Après avoir compilé le projet (j'espère que ca va fonctionner), lancez directement l'application depuis ./bin/debug/ sinon le fichier de configuration sera supprimé autmatiquement par visual studio.
Dans l'appli, entrez des enregistrement et fermer l'appli.
On vous demande si vous voulez crypter ou pas. A vous de voir les deux cas.
Ouvrez avec votre éditeur de texte le fichier de config et visualisez la différence.
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de SharpMao le 19/06/2006 09:43:03

    Génial, il ma fallait justement quelque chose comme ça, mais je n'avais pas encore eu le temps de chercher.

    Juste un détail, j'ai oté deux lignes au début :

    if (appSetting.SectionInformation.IsProtected)
    DecryptConfigFile();

    Avec ces deux lignes, le fichier de config était en clair durant le temps d'éxécution de l'application, alors que ce n'est pas nécessaire pour lire ou éditer les valeurs.

    Merci pour ton aide,

    Sharpmao

  • signaler à un administrateur
    Commentaire de fcolo le 19/06/2006 19:10:28

    :) merci, je ne savais pas que l'on pouvait modifier les valeur cryptées.
    Dans le source au moment de la lecture et de la sauvegarde des données, il y a les mêmes lignes, si tu veux les virer également ...

    f.colo

  • signaler à un administrateur
    Commentaire de woot6768 le 20/06/2006 09:56:09

    Merci pour ton travail!
    Bonne source.
    Woot

  • signaler à un administrateur
    Commentaire de the_revival le 31/10/2006 20:46:55

    Merci pour ce code !

    Même une fois crypté, l'appli en C# arrive a lire le contenu et à extraire les valeurs protégées avec un simple : "String css = ConfigurationSettings.AppSettings["myConnection"];"

    Ce qui est impeccable car ma chaine de connexion MySQL n'est plus lisible :)

  • signaler à un administrateur
    Commentaire de madebyhisto le 17/07/2008 16:17:36 9/10

    Super ton projet, je sais que ce projet est là depuis longtemps, mais voici un information intéressante.

    Si tu veux que le debug puisse voir correctement le fichier de config dans de debug de visual studio il te faut 2 chose.

    1) que ton fichier se nomme SaveAndEncryptConfigFile.exe.config au lieu de SaveAndEncryptConfigFile.config

    2) que tu utilise la commande suivante :
    ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath) au lieu de
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

    Ensuite du lance le debug et ça fonctionne numéro 1

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Boutique

Boutique de goodies CodeS-SourceS