How to encrypt XML content with a given AES key in Powershell

807 views Asked by At

I need to use a vendor API to create support tickets automatically. API requires XML content encrypted with a given AES key. How can I do this? I´m having problems with following code.

$xml =
'
<Service>
    <Action>SubmitTicket</Action>
    <UserName>[email protected]</UserName>
    <Case>
        <Title>Title</Title>
        <Description>Description</Description>
        <ProductName>ProductName</ProductName>
        <ProductVersion>1.0</ProductVersion>
        <ProductLanguage>English</ProductLanguage>
    <Purpose>Support</Purpose>
    </Case>
</Service>
'

$Key = '!QeRs6%x2RXzk6ab' (fake but similar one)

$secureString = ConvertTo-SecureString $xml -AsPlainText -Force
$encrypted_xml = ConvertFrom-SecureString $secureString -SecureString $key

I get following error.

ConvertFrom-SecureString : Cannot bind parameter 'Key'. Cannot convert value 
"!QeRs6%x2RXzk6ab" to type "System.Byte". Error: "Input string was not in a 
correct format." At C:\Users\user\Desktop\Powershell\API_Submitter.ps1:39 
char:62 + $EncryptedInfo = ConvertFrom-SecureString $secureSTring -Key $key
+ CategoryInfo: InvalidArgument: (:) [ConvertFrom-SecureString], 
ParameterBindingException + FullyQualifiedErrorId : 
CannotConvertArgumentNoMessage, 
Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand
1

There are 1 answers

1
Janne Tuukkanen On

The Key parameter of ConvertFrom-SecureString should be Byte array, not a string. You can use GetBytes to achieve this: (I'm assuming UTF8encoding)

$Key = [System.Text.Encoding]::UTF8.GetBytes('!QeRs6%x2RXzk6ab')

Specify the parameter names in the call:

$encrypted_xml = ConvertFrom-SecureString -SecureString $secureString -Key $key