Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UnSecureString {
- [string]$EncryptedString
- [byte[]]$Key
- UnSecureString() { }
- UnSecureString([System.Security.SecureString]$SecureString) {
- $vKey = New-Object Byte[](32)
- $Rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
- $Rng.GetBytes($vKey)
- $this.Key = $vKey
- $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
- }
- UnSecureString([string]$String) {
- $SecureString = ConvertTo-SecureString -String $String -AsPlainText -Force
- $vKey = New-Object Byte[](32)
- $Rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
- $Rng.GetBytes($vKey)
- $this.Key = $vKey
- $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
- }
- [System.Security.SecureString]ToSecureString() {
- return (ConvertTo-SecureString -String $this.EncryptedString -Key $this.Key)
- }
- [string]ToString() { return $this.EncryptedString }
- }
- class UnCredential {
- [string]$Username
- [UnSecureString]$Password
- UnCredential([string]$Username, [System.Security.SecureString]$Password) {
- $this.Username = $Username
- $this.Password = [UnsecureString]::new($Password)
- }
- UnCredential([string]$Username, [string]$Password) {
- $this.Username = $Username
- $this.Password = [UnsecureString]::new($Password)
- }
- [PSCustomObject]ToPSObject() {
- $f = @{
- Username = $this.Username
- Password = $this.Password.EncryptedString
- Key = $this.Password.Key
- }
- return (New-Object -TypeName PSCustomObject -Property $f)
- }
- UnCredential([string]$Path) {
- $Data = Import-Clixml -Path $Path
- 'Username', 'Password', 'Key' | ? { $_ -notin $Data.PSObject.Properties.Name } | % { throw "XML in $Path missing property $_" }
- $this.Username = $Data.Username
- $this.Password = [UnSecureString]::new()
- $this.Password.EncryptedString = $Data.Password
- $this.Password.Key = $Data.Key
- }
- [void]Save([string]$Path) {
- $this.ToPSObject() | Export-Clixml $Path
- }
- [System.Management.Automation.PSCredential]ToPSCredential() {
- return (New-Object System.Management.Automation.PSCredential($this.Username, $this.Password.ToSecureString()))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement