1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# CIFRAR $password = "Im a password" $MS = New-Object System.IO.MemoryStream $TD = New-Object System.Security.Cryptography.DESCryptoServiceProvider $CS = New-Object System.Security.Cryptography.CryptoStream($MS,$TD.CreateEncryptor($TD.IV, $TD.IV), [System.Security.Cryptography.CryptoStreamMode]::Write) $Writer = New-Object System.IO.StreamWriter($CS) $Writer.Write($password) $Writer.Flush() $CS.FlushFinalBlock() #$MS.Seek(0, [System.IO.SeekOrigin]::Begin) > NULL #not sure of its utility $EncodedText = [Convert]::ToBase64String($MS.ToArray()) $IVS = [Convert]::ToBase64String($TD.IV) $EncryptedData = "${IVS}:$EncodedText" $Writer.Dispose() $EncryptedData ##DESCIFRAR $Data = $EncryptedData.Split(':') $IV = [Convert]::FromBase64String($Data[0]) $DataByte = [Convert]::FromBase64String($Data[1]) $MS = New-Object System.IO.MemoryStream(,$DataByte) #array of one item must have a preceding coma $CS = New-Object System.Security.Cryptography.CryptoStream($MS,$TD.CreateDecryptor($IV,$IV), [System.Security.Cryptography.CryptoStreamMode]::Read) $Reader = New-Object System.IO.StreamReader($CS) $Result = $Reader.ReadToEnd() $Reader.Dispose() $Result |