Exemple #1
0
        private static SecurityKey ReadOrCreateAesKey()
        {
            var filePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                @"Microsoft\Crypto\Aes\MachineKeys\{0}".FmtInv(KeyContainerName));

            if (File.Exists(filePath))
            {
                return(SecurityKey.Read(filePath));
            }
            else
            {
                var dir = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                var sk = SecurityKey.Generate();
                sk.Save(filePath);

                return(sk);
            }
        }