/// <summary> /// Store an encrypted key file for user or machine level usage /// </summary> /// <param name="scope">Scope</param> /// <returns>RSA key</returns> public static RSA RSAFromFile(DataProtectionScope scope) { byte[] esp = new byte[] { 69, 155, 31, 254, 7, 18, 99, 187 }; byte[] esl = new byte[] { 101, 5, 79, 221, 48, 42, 26, 123 }; string xmlFile = (scope == DataProtectionScope.CurrentUser ? Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "esku_123_abc.bin") : Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "eskm_123_abc.bin")); RSACryptoServiceProvider rsa; if (File.Exists(xmlFile)) { byte[] xmlBytes = File.ReadAllBytes(xmlFile); xmlBytes = CryptoUtility.AesDecryption(xmlBytes, esp, esl); rsa = new RSACryptoServiceProvider(); RSAKeyExtensions.FromXmlString(rsa, CryptoUtility.UTF8EncodingNoPrefix.GetString(xmlBytes)); } else { rsa = new RSACryptoServiceProvider(4096); byte[] xmlBytes = CryptoUtility.UTF8EncodingNoPrefix.GetBytes(RSAKeyExtensions.ToXmlString(rsa, true)); xmlBytes = CryptoUtility.AesEncryption(xmlBytes, esp, esl); File.WriteAllBytes(xmlFile, xmlBytes); } return(rsa); }