Difference in Outputs Between Two Methods of Producing String

I need to create machinekey's for web servers and SSRS servers. A pretty common task when putting together farms.

The original code I used was from here.

https://support.microsoft.com/en-ca/kb/312906/en-us

I'm guessing just about everyone has stumbled on this bit of C# code.

I figured I'd turn it into a PowerShell script as it would fit within my environment and documentation better rather than pre-compiled code.  And remove the requirement of needing to compile the code to begin with.  This isn't exactly high performance computing after all.

Anyways, I stumbled on to this page:

http://jeffgraves.me/2012/06/05/read-write-net-machine-key-with-powershell/

Which pretty much has the meat of what I need to achieve my goal.

In reviewing the script I thought maybe I could change it up a bit. So I tried taking the for loop in the GenKey function and turn it into the following:

$result += $buff | ForEach-Object { [String]::Format('{0:x2}', $_ ) }

This looks great but the output of $result is slightly different.  I get two characters, a space, two characters, a space and so on.

The question I have is does anyone know why it is putting in these spaces and potentially how to make this otherwise work?

One thing I did was the following:

$result.replace(' ','')


But that is just cheating IMO.

Desired output is something like this:

4002e8ba0450b45d2a9bd05f13a0526c329068655838991668e90e543bc86326966be13783155c276a130e4a37649dece26ba1428e0f7575b0b53620945bef05

Output I am getting is:

26 0f c9 a3 67 cf c2 b1 91 a1 c7 97 51 71 da e4 e9 d3 a7 79 a0 05 5b 23 70 7f 49 bb e4 8e 86 64 cb 27 42 c8 ca ac f7 9e b0 26 37 42 9b 4f 71 cf ff 82 45 24 8f e4 06 0c 64 31 f4 42 46 e5 92 af

  • Edited by FluffySniper 12 hours 20 minutes ago Added information.
June 18th, 2015 2:51pm

I think you mean something like this, perhaps?

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 3:02pm

Brilliant! Thank you!

That definitely produces the output I desire, and even more inline with the intent of PowerShell, which I appreciate.

June 18th, 2015 3:11pm

Just to give back to the community. This is what I was trying to produce.  Thanks again for the help.

Param (
    [Parameter(Mandatory=$true)]
    [Int] $DecyptionKeyLength,
    [Parameter(Mandatory=$true)]
    [Int] $ValidationKeyLength
)
function Get-Key {
    [CmdletBinding()]
    Param (
        [Int] $KeyLength
    )

    Begin {
        [String] $Result = $null
        [System.Byte[]] $Buffer = New-Object 'System.Byte[]' $KeyLength
        [System.Security.Cryptography.RNGCryptoServiceProvider] $RandomNumber = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
    }

    Process {
        $RandomNumber.GetBytes($Buffer)
        $Buffer | ForEach-Object { $Result += '{0:x2}' -f $_ }
    }

    End {
        $Result.ToString().ToUpper()
    }
    
}

'<machineKey validationKey="{0}" decryptionKey="{1}" validation="SHA1" decryption="AES" />' -f (Get-Key -KeyLength $ValidationKeyLength), (Get-Key -KeyLength $DecyptionKeyLength)


Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 5:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics