PowerShell implement encrypted decryption text file method instance _powershell

Source: Internet
Author: User

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" 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.