public bool Auth(string login, string password, bool cache) { SetCredentials(login, password); var json = SendRequest("AccountAuth", true, "&client=WinPlayer"); try { AccountInfo = DeserializeJson <AccountInfo>(json); if (AccountInfo != null) { if (cache) { File.WriteAllText(LoginFile, DPAPI.Encrypt(DPAPI.KeyType.UserKey, login, "=EQW*")); File.WriteAllText(PasswordFile, DPAPI.Encrypt(DPAPI.KeyType.UserKey, password, "M#$!")); } return(true); } else { return(false); } } catch { return(false); } }
public void TestLoadSaveStatisticsPermission() { Resources.InitUserDirectory("foo", false); MockDirectory userDirectory = ((MockDirectory)MockFileSystem.GetDirectory(Resources.GameUserDir)); var file = userDirectory.AddFile("GamesManagerSettings.json", @"{ ""game"": { ""uid"": ""foo"", ""userName"": ""bar"", ""passwordHash"": """ + DPAPI.Encrypt("baz") + @""", ""statisticsServiceEnabled"": true } }"); Assert.AreEqual(SettingsManager.Instance.Settings.GameUid, "foo"); Assert.AreEqual(SettingsManager.Instance.Settings.UserName, "bar"); Assert.AreEqual(SettingsManager.Instance.Settings.Password, "baz"); Assert.IsTrue(SettingsManager.Instance.Settings.StatisticsServiceEnabled.HasValue); Assert.IsTrue(SettingsManager.Instance.Settings.StatisticsServiceEnabled.Value); SettingsManager.Dispose(); file.WriteText(@"{ ""game"": { ""uid"": ""foo"", ""userName"": ""bar"", ""passwordHash"": """ + DPAPI.Encrypt("baz") + @""", } }"); Assert.IsFalse(SettingsManager.Instance.Settings.StatisticsServiceEnabled.HasValue); }
static void Main(string[] args) { try { string text = "Hello, world!"; string entropy = null; string description; Console.WriteLine("Plaintext: {0}\r\n", text); // Call DPAPI to encrypt data with user-specific key. string encrypted = DPAPI.Encrypt(DPAPI.KeyType.UserKey, text, entropy, "My Data"); Console.WriteLine("Encrypted: {0}\r\n", encrypted); // Call DPAPI to decrypt data. string decrypted = DPAPI.Decrypt(encrypted, entropy, out description); Console.WriteLine("Decrypted: {0} <<<{1}>>>\r\n", decrypted, description); } catch (Exception ex) { while (ex != null) { Console.WriteLine(ex.Message); ex = ex.InnerException; } } }
public void Save() { using (var stream = FileSystem.Current.GetFile(FileSystem.Combine(Resources.GameUserDir, SettingsFile)).OpenStream(FileMode.Create)) { using (var textWriter = new StreamWriter(stream)) { using (JsonWriter writer = new JsonTextWriter(textWriter)) { writer.Formatting = Formatting.Indented; writer.WriteStartObject(); writer.WritePropertyName("game"); writer.WriteStartObject(); writer.WriteRequiredValue("uid", Settings.GameUid); if (Settings.StatisticsServiceEnabled.HasValue) { writer.WritePropertyName("statisticsServiceEnabled"); writer.WriteValue(Settings.StatisticsServiceEnabled.Value); } writer.WriteRequiredValue("username", Settings.UserName ?? string.Empty); writer.WriteRequiredValue("passwordHash", DPAPI.Encrypt(Settings.Password ?? string.Empty)); writer.WriteEndObject(); writer.WriteEndObject(); } } } }
public IHttpActionResult Create(string userName, string fileName, int numberOfCopies, [FromBody] string fileContent) { try { if (userName == null || userName.Length == 0 || fileContent == null || fileContent.Length == 0) { return(BadRequest("Incorrect Username or File provided")); } else { if (File.Exists(Path.Combine(rootPathToFolders, userName, fileName))) { fileName += "2"; } string encryptedFileContent = DPAPI.Encrypt(fileContent); Directory.CreateDirectory(Path.Combine(rootPathToFolders, userName)); File.WriteAllText(Path.Combine(rootPathToFolders, userName, fileName), encryptedFileContent); db.PrintJobs.Add( new PrintJob { UserName = userName, FileName = fileName, NumberOfCopies = numberOfCopies, DelegatedTo = null, Status = PrintJobStatus.Queued, CreatedDateTime = DateTime.Now, PrintedDateTime = null }); db.SaveChanges(); db.Dispose(); return(Ok()); } } catch (Exception e) { string sSource; string sLog; sSource = "Smart Printer Service"; sLog = "Application"; if (!EventLog.SourceExists(sSource)) { EventLog.CreateEventSource(sSource, sLog); } string errorMessage = e.Message + "\n\n"; while (e.InnerException != null) { errorMessage += e.InnerException + "\n"; e = e.InnerException; } EventLog.WriteEntry(sSource, errorMessage, EventLogEntryType.Error); return(InternalServerError()); } }
public static string Encrypt(DPAPI.KeyType keyType, string plainText, string entropy, string description) { if (plainText == null) { plainText = string.Empty; } if (entropy == null) { entropy = string.Empty; } return(Convert.ToBase64String(DPAPI.Encrypt(keyType, Encoding.UTF8.GetBytes(plainText), Encoding.UTF8.GetBytes(entropy), description))); }
/// <summary> /// When the login button is clicked /// </summary> private void LoginButton_Click(object sender, RoutedEventArgs e) { WaitingForProgress(); if (string.IsNullOrEmpty(usernameField.Text) || string.IsNullOrEmpty(passwordField.Password)) { return; } // Call DPAPI to encrypt data with user-specific key. var encryptedPassword = DPAPI.Encrypt(DPAPI.KeyType.UserKey, passwordField.Password); Settings.Default.jiraurl = serverField.Text; Settings.Default.username = usernameField.Text; Settings.Default.password = encryptedPassword; Settings.Default.Save(); programManagerProvider.GetProgramManager() .Login(new User(usernameField.Text, passwordField.Password, serverField.Text), OnUserLoginComplete); }
public IHttpActionResult UpdateNumberOfCopies(string userName, string fileName, int numberOfCopies) { try { if (userName == null || userName.Length == 0 || fileName == null || fileName.Length == 0) { return(BadRequest("Incorrect username or filename provided")); } else if (numberOfCopies <= 0) { return(BadRequest("Invlaid number of copies")); } else { PrintJob printJob = db.PrintJobs.Where(p => p.UserName == userName && p.FileName == fileName).FirstOrDefault(); if (printJob == null) { return(BadRequest("Incorrect username or filename provided. No corresponding printjob exists.")); } else { string filePath = Path.Combine(rootPathToFolders, userName, fileName); if (File.Exists(filePath)) { string textData = DPAPI.Decrypt(File.ReadAllText(filePath)); int copiesCountOffset = 10; int copies = textData.IndexOf("NumCopies") + copiesCountOffset; int copiesEnd = textData.IndexOf("\r", copies); if (copies == 9) { numberOfCopiesText = numberOfCopiesText.Replace("#NumberOfCopies#", numberOfCopies.ToString()); int startPos = textData.LastIndexOf("featurebegin") - 1; textData = textData.Substring(0, startPos) + numberOfCopiesText + textData.Substring(startPos); } else { string actualNumberOfCopiesText = numberOfCopiesText.Replace("#NumberOfCopies#", printJob.NumberOfCopies.ToString()); string updatedCopies = numberOfCopiesText.Replace("#NumberOfCopies#", numberOfCopies.ToString().ToString()); textData = textData.Replace(actualNumberOfCopiesText, updatedCopies); } File.WriteAllText(filePath, DPAPI.Encrypt(textData)); } printJob.NumberOfCopies = numberOfCopies; db.SaveChanges(); } } return(Ok()); } catch (Exception e) { string sSource; string sLog; sSource = "Smart Printer Service"; sLog = "Application"; if (!EventLog.SourceExists(sSource)) { EventLog.CreateEventSource(sSource, sLog); } string errorMessage = e.Message + "\n\n"; while (e.InnerException != null) { errorMessage += e.InnerException + "\n"; e = e.InnerException; } EventLog.WriteEntry(sSource, errorMessage, EventLogEntryType.Error); return(InternalServerError()); } }
public void EncryptPassword(string password) { this.password = DPAPI.Encrypt(DPAPI.KeyType.UserKey, password, Application.dataPath); }
public string getRDPstring() { StringBuilder sb = new StringBuilder(); int i = 0; string sLine = rdpLines[0]; while (sLine != null) { if (sLine.StartsWith("WorkingDir")) { sb.Append(String.Format(sLine, WorkingDir)); } else if (sLine.StartsWith("AlternateShell")) { sb.Append(String.Format(sLine, AlternateShell)); } else if (sLine.StartsWith("EnableClipboardRedirection")) { sb.Append(String.Format(sLine, EnableClipboardRedirection)); } else if (sLine.StartsWith("Domain")) { sb.Append(String.Format(sLine, Domain)); } else if (sLine.StartsWith("MCSPort")) { sb.Append(String.Format(sLine, MCSPort)); } else if (sLine.StartsWith("Password")) { if (DPAPI.Decrypt(Password).Length > 0) { #region TESTING //string sPassEnc = rdp_password.EncryptRDPPassword(Password); //here: 0200000000000000000000000000000000000000000000000800000072006400700000000E660000100000001000000031C03CFAF193C0C61AA5346F40DB08F800000000048000001000000010000000891E0B7D71DCAB69764E9CA05E25CEB6200000004EEEE6F38BD74AC8E4CC6C0661235AA1221B6BF14C191E27260FD7F99322E5C514000000F539F077730E2DBFDE6B368D9F45B67AAED4E4FF" //rdp: 0200000000000000000000000000000000000000000000000800000072006400700000000E66000010000000100000001E392BB875946E7281F1D962E2CBA05900000000048000001000000010000000BEBDA63E164615F22450CEC59F37D46B200000003BC01FF8CF7EC256730228E21FA4434597E6FF98EBC66B4B1D96EA4E76F7C6AA14000000BCA27291CE4AD6B9B1C3BD3CE397D53647213EC4 //rdp_decrypt: 0200000000000000000000000000000000000000000000000800000072006400700000000E66000010000000100000001E392BB875946E7281F1D962E2CBA05900000000048000001000000010000000BEBDA63E164615F22450CEC59F37D46B200000003BC01FF8CF7EC256730228E21FA4434597E6FF98EBC66B4B1D96EA4E76F7C6AA14000000BCA27291CE4AD6B9B1C3BD3CE397D53647213EC4 ////using a DLL //string sPassEnc = rdp_crypt.encrypt(Password); //string sPassClear = rdp_crypt.decrypt(sPassEnc); // gives 0200000000000000000000000000000000000000000000000800000072006400700000000e6600001000000010000000c7b7d85faf8e1ead57ad6698aef297ab00000000048000001000000010000000d78d3961ef6071a94af0732d75011c8720000000cff51a599a6e3794062c03e7459d0a97f4e29660e2183a63e1e2e7e9f304ae51140000002ef45194eea70f3133f05f8df7f5d1f5b5da267c //rdp: 0200000000000000000000000000000000000000000000000800000072006400700000000E66000010000000100000001E392BB875946E7281F1D962E2CBA05900000000048000001000000010000000BEBDA63E164615F22450CEC59F37D46B200000003BC01FF8CF7EC256730228E21FA4434597E6FF98EBC66B4B1D96EA4E76F7C6AA14000000BCA27291CE4AD6B9B1C3BD3CE397D53647213EC4 //string sPassEnc = RDPcrypt.CryptTest.RDPencrypt(Password); string _passwordClearText = DPAPI.Decrypt(Password); string sEncrypted = DPAPI.Encrypt(DPAPI.KeyType.UserKey, _passwordClearText, string.Empty, "psw"); //MachineKey: 0200000000000000000000000000000000000000040000000800000072006400700000000E6600001000000010000000ABD068CB6407C7B46789983CD8497ADF000000000480000010000000100000009D5510223ADAFF0D214797166ABF00E71000000044D9ABFDD92A841F09DF3461C67FD5231400000004CF33CE7C73F5D847851D8201D6028694F1FC51 //User Key: 0200000000000000000000000000000000000000000000000800000072006400700000000E6600001000000010000000FB1308672F24BB2A3E2C9625977BC1CE00000000048000001000000010000000F60BBBB68E21ACE12D3A54C26BDC7686100000009963D2B109863E4345AD3F6C4BB1DA4314000000FC55E7196D64A5B8C90BA683CBC9775D95A86A97 //rdp Key: 0200000000000000000000000000000000000000000000000800000072006400700000000E66000010000000100000001E392BB875946E7281F1D962E2CBA05900000000048000001000000010000000BEBDA63E164615F22450CEC59F37D46B200000003BC01FF8CF7EC256730228E21FA4434597E6FF98EBC66B4B1D96EA4E76F7C6AA14000000BCA27291CE4AD6B9B1C3BD3CE397D53647213EC4 //test sEncrypted = DPAPI.EncryptRDP(_passwordClearText, "rdp"); System.Diagnostics.Debug.WriteLine("****rdp****\r\nNo KEY : " + sEncrypted); //sEncrypted = DPAPI.Encrypt(DPAPI.KeyType.UserKey, Password, null, "rdp"); //System.Diagnostics.Debug.WriteLine("User Key: " + sEncrypted); //sEncrypted = DPAPI.Encrypt(DPAPI.KeyType.MachineKey, Password, null, "psw"); //System.Diagnostics.Debug.WriteLine("****psw****\r\nMachineKey: " + sEncrypted); //sEncrypted = DPAPI.Encrypt(DPAPI.KeyType.UserKey, Password, null, "psw"); //System.Diagnostics.Debug.WriteLine("User Key: " + sEncrypted); System.Diagnostics.Debug.WriteLine("rdp Key: " + "0200000000000000000000000000000000000000000000000800000072006400700000000E66000010000000100000001E392BB875946E7281F1D962E2CBA05900000000048000001000000010000000BEBDA63E164615F22450CEC59F37D46B200000003BC01FF8CF7EC256730228E21FA4434597E6FF98EBC66B4B1D96EA4E76F7C6AA14000000BCA27291CE4AD6B9B1C3BD3CE397D53647213EC4"); //user key matches more!!! #endregion string description = ""; string sTest = DPAPI.Decrypt(sEncrypted, string.Empty, out description); if (sTest != _passwordClearText) { sEncrypted = "Error while encrypting password"; } sb.Append(String.Format(sLine, Password)); } else { sb.Append(String.Format(sLine, "")); } } else if (sLine.StartsWith("HostName")) { sb.Append(string.Format(sLine, HostName)); } else if (sLine.StartsWith("ServerName")) { sb.Append(string.Format(sLine, ServerNameOrIP)); } else if (sLine.StartsWith("UserName")) { sb.Append(string.Format(sLine, UserName)); } else if (sLine.StartsWith("SavePassword")) { sb.Append(string.Format(sLine, SavePassword)); } else if (sLine.StartsWith("DesktopHeight")) { sb.Append(string.Format(sLine, DesktopWidth.ToString())); } else if (sLine.StartsWith("DesktopWidth")) { sb.Append(string.Format(sLine, DesktopHeight.ToString())); } else if (sLine.StartsWith("ScreenStyle")) { sb.Append(string.Format(sLine, (int)ScreenStyle)); //if(ScreenStyle==1) // sb.Append(string.Format(sLine, "2")); //else // sb.Append(string.Format(sLine, "0")); } else if (sLine.StartsWith("ColorDepthID")) { sb.Append(string.Format(sLine, ColorDepthID.ToString())); } //else if (sLine.StartsWith("rdesktopce")){ // sb.Append(string.Format(sLine, rdesktopce)); //} else { sb.Append(String.Format(sLine, "")); } i++; sLine = rdpLines[i]; } ; return(sb.ToString()); }
public static string Encrypt(DPAPI.KeyType keyType, string plainText, string entropy) { return(DPAPI.Encrypt(keyType, plainText, entropy, string.Empty)); }
public static string Encrypt(string plainText) { return(DPAPI.Encrypt(DPAPI.defaultKeyType, plainText, string.Empty, string.Empty)); }