0

I'm transforming a string into an encrypted standard string in PowerShell, then I'd like to use C# to retrieve this encrypted standard string and transform it back into a plain string, in order to use it as a password.

Thank you for your help. Sincerely, Maxime

Here's my PowerShell code :

$pwd = Read-Host -Prompt "Entrez le mot de passe" 
$securePwd = ConvertTo-SecureString $pwd -AsPlainText -Force 
$encryptedPwd = ConvertFrom-SecureString -SecureString $securePwd -Key (1..16) 
$encryptedPwd | Out-File -FilePath $svcAccountPath 

Here's my C# code :

static SecureString DecryptPassword(byte[] key, string filePath)
        {
            byte[] encryptedData = File.ReadAllBytes(filePath);
            byte[] decryptedData = ProtectedData.Unprotect(encryptedData, key, DataProtectionScope.CurrentUser);

            SecureString decryptedPassword = new SecureString();
            foreach (byte b in decryptedData)
            {
                decryptedPassword.AppendChar((char)b);
            }

            return decryptedPassword;
        }
        static string DecryptSecureString(SecureString secureString)
        {
            IntPtr unmanagedString = IntPtr.Zero;
            try
            {
                unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
                return Marshal.PtrToStringUni(unmanagedString);
            }
            finally
            {
                Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
            }
        }
        private void idk()
        {
            string pwdSeduredFile = "C:/Temp/C01 User Manager/conf/encryptedPwd.txt";
            byte[] key = new byte[16];
            for (int i = 0; i < 16; i++)
            {
                key[i] = (byte)(i + 1);
            }
            SecureString encryptedPassword = DecryptPassword(key, pwdSeduredFile);
            string password1 = DecryptSecureString(encryptedPassword);
            MessageBox.Show("password :" + password1, "Erreur", MessageBoxButton.OK, MessageBoxImage.Information);
        }

I have an error on the ligne "byte[] decryptedData = ProtectedData.Unprotect(encryptedData, key, DataProtectionScope.CurrentUser);" I attach it to this message.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 3
    It doesn't encrypt the output anyway, so you may as well just make the variable a normal string. See also the docs https://learn.microsoft.com/en-us/dotnet/api/system.security.securestring?view=net-7.0 *"We recommend that you don't use the SecureString class for new development on .NET (Core)"* Any case, you might want to consider using `Get-Credential` instead – Charlieface Jun 14 '23 at 14:25
  • [`$pwd` is one of PowerShell automatic variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#pwd) and contains a path object that represents the full path of the current directory location for the current PowerShell runspace. – JosefZ Jun 14 '23 at 15:36

0 Answers0