Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UnSecureString {
- [string]$EncryptedString
- [byte[]]$Key
- hidden static [void]CheckKeyLength([byte[]]$Key) {
- if ($Key.Length -notin @(16, 24, 32)) {
- throw "Invalid Key length. Must be 16, 24, or 32 bytes"
- }
- }
- static [byte[]]RandomKey() {
- [byte[]]$vKey = New-Object Byte[](32)
- $Rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
- $Rng.GetBytes($vKey)
- return $vKey
- }
- hidden static [void]CheckProperties([PSCustomObject]$Data) {
- 'Password', 'Key' |
- ? { $_ -notin $Data.PSObject.Properties.Name } |
- Select-Object -First 1 |
- % { throw "[UnSecureString]: [PSCustomObject] missing property $_" }
- }
- static [UnSecureString]Load([string]$Path) {
- $Data = Import-Clixml -Path $Path
- [UnSecureString]::CheckProperties($Data)
- return ([UnSecureString]::new($Data.Password, $Data.Key))
- }
- UnSecureString() { }
- UnSecureString([System.Security.SecureString]$SecureString) {
- $vKey = [UnSecureString]::RandomKey()
- $this.Key = $vKey
- $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
- }
- UnSecureString([System.Security.SecureString]$SecureString, [byte[]]$Key) {
- [UnSecureString]::CheckKeyLength($Key)
- $this.Key = $Key
- $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $Key
- }
- UnSecureString([string]$String) {
- $SecureString = ConvertTo-SecureString -String $String -AsPlainText -Force
- $vKey = [UnSecureString]::RandomKey()
- $this.Key = $vKey
- $this.EncryptedString = ConvertFrom-SecureString -SecureString $SecureString -Key $vKey
- }
- UnSecureString([string]$EncryptedString, [byte[]]$Key) {
- [UnSecureString]::CheckKeyLength($Key)
- $this.Key = $Key
- $this.EncryptedString = $EncryptedString
- }
- UnSecureString([PSCustomObject]$Data) {
- [UnSecureString]::CheckProperties($Data)
- [UnSecureString]::CheckKeyLength($Data.Key)
- $this.Key = $Data.Key
- $this.EncryptedString = $Data.Password
- }
- [System.Security.SecureString]ToSecureString() {
- [System.Security.SecureString]$SecureString = ConvertTo-SecureString -String $this.EncryptedString -Key $this.Key
- if ($SecureString -isnot [System.Security.SecureString]) { throw "Could not create SecureString object from UnSecureString object" }
- return $SecureString
- }
- [boolean]IsValid() {
- try {
- ConvertTo-SecureString -String $this.EncryptedString -Key $this.Key | Out-Null
- return $true
- } catch {
- return $false
- }
- }
- [boolean]IsNotValid() { return (-not $this.IsValid()) }
- [string]ToString() { return $this.EncryptedString }
- [string]ToString([byte[]]$Key) {
- [UnSecureString]::CheckKeyLength($Key)
- return ($this.ToSecureString() | ConvertFrom-SecureString -Key $Key)
- }
- [string]ToPassword() {
- [System.Security.SecureString]$SecureString = $this.ToSecureString()
- $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecureString)
- $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
- [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
- return $result
- }
- [PSCustomObject]ToPSObject() {
- $f = @{
- Password = $this.EncryptedString
- Key = $this.Key
- }
- return (New-Object -TypeName PSCustomObject -Property $f)
- }
- [PSCustomObject]ToPSObject([byte[]]$Key) {
- $f = @{
- Password = $this.ToString($Key)
- Key = $Key
- }
- return (New-Object -TypeName PSCustomObject -Property $f)
- }
- [void]Save([string]$Path) {
- $this.ToPSObject() | Export-Clixml $Path
- }
- [void]Save([string]$Path, [byte[]]$Key) {
- $this.ToPSObject($Key) | Export-Clixml $Path
- }
- }
- class UnCredential {
- [string]$Username
- [UnSecureString]$Password
- hidden static [void]CheckProperties([PSCustomObject]$Data) {
- 'Username', 'Password', 'Key' |
- ? { $_ -notin $Data.PSObject.Properties.Name } |
- Select-Object -First 1 |
- % { throw "[UnCredential]: [PSCustomObject] missing property $_" }
- }
- 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)
- }
- UnCredential([string]$Path) {
- $Data = Import-Clixml -Path $Path
- [UnCredential]::CheckProperties($Data)
- $this.Username = $Data.Username
- $this.Password = [UnSecureString]::new($Data.Password, $Data.Key)
- }
- UnCredential([string]$Username, [string]$Password, [byte[]]$Key) {
- $this.Username = $Username
- $this.Password = [UnSecureString]::new($Password, $Key)
- }
- UnCredential([PSCustomObject]$Data) {
- [UnCredential]::CheckProperties($Data)
- $this.Username = $Data.Username
- $this.Password = [UnSecureString]::new($Data.Password, $Data.Key)
- }
- UnCredential([System.Management.Automation.PSCredential]$PSCredential) {
- $this.Username = $PSCredential.Username
- $this.Password = [UnSecureString]::new($PSCredential.Password)
- }
- [PSCustomObject]ToPSObject() {
- $f = @{
- Username = $this.Username
- Password = $this.Password.EncryptedString
- Key = $this.Password.Key
- }
- return (New-Object -TypeName PSCustomObject -Property $f)
- }
- [PSCustomObject]ToPSObject([byte[]]$Key) {
- $f = @{
- Username = $this.Username
- Password = $this.Password.ToString($Key)
- Key = $Key
- }
- return (New-Object -TypeName PSCustomObject -Property $f)
- }
- [void]Save([string]$Path) {
- $this.ToPSObject() | Export-Clixml $Path
- }
- [void]Save([string]$Path, [byte[]]$Key) {
- $this.ToPSObject($Key) | Export-Clixml $Path
- }
- [System.Management.Automation.PSCredential]ToPSCredential() {
- [System.Security.SecureString]$Pword = $this.Password.ToSecureString()
- [System.Management.Automation.PSCredential]$Cred = New-Object System.Management.Automation.PSCredential($this.Username, $Pword)
- if ($Cred -isnot [System.Management.Automation.PSCredential]) { throw "Could not create PSCredential object from UnCredential object" }
- return $Cred
- }
- [string]ToString() { return $this.Username }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement