WizzyWodich
WizzyWodich
CC#
Created by WizzyWodich on 3/11/2025 in #help
How to store keys
Guys, hello, I asked myself this question. How to store keys as securely as possible? Simulating the situation, I have an application and it has certain files encrypted using a certain key, how to store the key itself. I refuse that the client will have the key and will enter it. The application will not have access to any external database until it passes this step with encrypted files, and the application, for example, will be available on the network, in which case I refuse to store it in env. How and where are keys stored in such cases?
4 replies
CC#
Created by WizzyWodich on 2/16/2025 in #help
PasswordBox how to pass data to viewModel
This is a piece of code from View:
private void PasswordInput_PasswordChanged(object sender, RoutedEventArgs e)
{
if (DataContext is RegistrationVM vm && sender is PasswordBox passwordBox)
{
vm.Password = passwordBox.SecurePassword;
MessageBox.Show($"Password {passwordBox.SecurePassword} | {passwordBox.Password}");
}
}
private void PasswordInput_PasswordChanged(object sender, RoutedEventArgs e)
{
if (DataContext is RegistrationVM vm && sender is PasswordBox passwordBox)
{
vm.Password = passwordBox.SecurePassword;
MessageBox.Show($"Password {passwordBox.SecurePassword} | {passwordBox.Password}");
}
}
This is a piece of code from ViewModel:
public ICommand RegisterCommand { get; }

private async Task Register()
{
if (IsPasswordValid())
{
string password = ConvertSecureStringToString(Password);
bool success = await _tcpClientService.RegisterAsync(UserLogin, password);
if (success)
{
MessageBox.Show("Регистрация успешна!");
NavigateToLogin();
}
else
{
MessageBox.Show("Ошибка регистрации!");
}
}
else
{
MessageBox.Show("Пароли не совпадают или слишком короткие!");
}
}

private string ConvertSecureStringToString(SecureString secureString)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
if (unmanagedString != IntPtr.Zero)
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
}
public ICommand RegisterCommand { get; }

private async Task Register()
{
if (IsPasswordValid())
{
string password = ConvertSecureStringToString(Password);
bool success = await _tcpClientService.RegisterAsync(UserLogin, password);
if (success)
{
MessageBox.Show("Регистрация успешна!");
NavigateToLogin();
}
else
{
MessageBox.Show("Ошибка регистрации!");
}
}
else
{
MessageBox.Show("Пароли не совпадают или слишком короткие!");
}
}

private string ConvertSecureStringToString(SecureString secureString)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
if (unmanagedString != IntPtr.Zero)
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
}
5 replies