Applicable to Powershell3.0 and later versions.
Let's say you need to encrypt your files, and here's how to encrypt your files:
$Path = "$env: Temp\secret.txt"
$Secret = ' Hello world! '
$Passphrase = ' Some secret key '
$key = [byte[]] ($Passphrase. PadRight (24). Substring (0,24). ToCharArray ())
$Secret |
Convertto-securestring-asplaintext-force |
Convertfrom-securestring-key $key |
Out-file-filepath $Path
Notepad $Path
When you need to decrypt the contents, you need the original password:
$Passphrase = Read-host ' Enter The secret pass phrase ' $Path = "$env: Temp\secret.txt" $key = [byte[]] ($Passphrase. PadRight (24). Substring (0,24).
ToCharArray ()) try {$decryptedTextSecureString = Get-content-path $Path-raw | Convertto-securestring-key $key-erroraction Stop $cred = New-object-typename System.Management.Automation.PSCredenti Al (' Dummy ', $decryptedTextSecureString) $decryptedText = $cred. Getnetworkcredential (). Password} catch {$decryptedText = ' (Wrong key) '} "the decrypted secret text: $decryptedText"