private static TrialData CheckRegistryData(String regBranch, String regKey, String regParam, String keyFileName) { object obj; using (var rk = Registry.CurrentUser.CreateSubKey(String.Format("{0}{1}{2}", regBranch, regBranch.EndsWith("\\", StringComparison.Ordinal) ? String.Empty : "\\", regKey))) { obj = rk.GetValue(regParam); } if (obj == null) { return new TrialData(DateTime.Now) { LastStart = DateTime.Now } } ; byte[] encryptedObject = obj as byte[]; if (encryptedObject == null) { throw new CryptographicException("Unexpected data type"); } return((TrialData)ClmEncryptor.DecryptObject(keyFileName, encryptedObject)); }
private static void SaveToFile(TrialData data, String fileName, String keyFileName) { var encryptedObject = ClmEncryptor.EncryptObject(keyFileName, data); using (var f = File.Open(fileName, FileMode.Create, FileAccess.Write)) { f.Write(encryptedObject, 0, encryptedObject.Length); } }
private static void SaveToRegistry(TrialData data, String regBranch, String regKey, String regParam, String keyFileName) { byte[] encryptedObject = ClmEncryptor.EncryptObject(keyFileName, data); using (var rk = Registry.CurrentUser.CreateSubKey(String.Format("{0}{1}{2}", regBranch, regBranch.EndsWith("\\", StringComparison.Ordinal) ? String.Empty : "\\", regKey))) { rk.SetValue(regParam, encryptedObject, RegistryValueKind.Binary); } }
private static TrialData CheckFileData(String fileName, String keyFileName) { if (!File.Exists(fileName)) { return new TrialData(DateTime.Now) { LastStart = DateTime.Now } } ; byte[] buffer = new byte[1024]; using (MemoryStream mstr = new MemoryStream()) { using (var f = File.Open(fileName, FileMode.Open, FileAccess.Read)) { int n; while ((n = f.Read(buffer, 0, buffer.Length)) > 0) { mstr.Write(buffer, 0, n); } } return((TrialData)ClmEncryptor.DecryptObject(keyFileName, mstr.ToArray())); } }