Conceptos importantes:
- The ConvertFrom-SecureString cmdlet converts a secure string (System.Security.SecureString) into an encrypted standard string (System.String). Unlike a secure string, an encrypted standard string can be saved in a file for later use. The encrypted standard string can be converted back to its secure string format by using the
ConvertTo-SecureString
cmdlet. - If an encryption key is specified by using the Key or SecureKey parameters, the Advanced Encryption Standard (AES) encryption algorithm is used. The specified key must have a length of 128, 192, or 256 bits because those are the key lengths supported by the AES encryption algorithm. If no key is specified, the Windows Data Protection API (DPAPI) is used to encrypt the standard string representation.
- The
Export-Clixml
cmdlet creates a Common Language Infrastructure (CLI) XML-based representation of an object or objects and stores it in a file. You can then use theImport-Clixml
cmdlet to recreate the saved object based on the contents of that file. For more information about CLI, see Language independence. - SecureStringToBSTR (SecureString): Asigna una cadena binaria (BSTR) no administrada y copia en ella el contenido de un objeto SecureString administrado.
- PtrToStringAuto (IntPtr): Asigna un String administrado y copia en él todos los caracteres hasta el primer carácter nulo de una cadena almacenada en memoria no administrada.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Credencial en una variable $credenciales = (Get-Credential) $credenciales # Mostrar credenciales Get-Credential | Select Username,Password Get-Credential | Select Username,@{n="Password"; e={$_.password | ConvertFrom-SecureString}} # Ver password seguro $var = (Get-Credential).Password | ConvertFrom-SecureString $var | ConvertTo-SecureString # Exportar credenciales $credenciales | Export-Clixml .\credenciales.xml # Importar credenciales $credenciales = Import-Clixml .\credenciales.xml $credenciales.Password # Ver credenciales almacenados $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR(($credenciales.Password)) $PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) $PlainPassword |