Konica Minolta and AD SSO and IC Card Reader

I needed a way to convert HID ISO prox card numbers (facility code and ID) to hex for use with our Konica Minolta printers. This was to save us from having to enter “hex trace” mode on the printer every time we wanted to add a new card to an employee’s AD account for card-based printer authentication. Additionally, I needed a way to decode from the Konica hex back to decimal so that I could verify card numbers. Here is what I wrote once upon a time.

If you want to learn more about the Weigend 26-bit format for the HID ISO prox II cards, I’d suggest that you refer to their handy PDF.

function ConvertTo-KonicaHex {
    <#
    .SYNOPSIS
    Convert HID card to Konica Hex

    .EXAMPLE
    ~> ConvertTo-KonicaHex 002155
    000010d7
    .EXAMPLE
    ~> ConvertTo-KonicaHex 2155
    000010d7
    .EXAMPLE
    ~> ConvertTo-KonicaHex 02155
    000010d7
    .EXAMPLE
    ~> ConvertTo-KonicaHex 102155
    020210d7
    #>
    [cmdletbinding()]
    param (
        [string]$CardNumber
    )

    if ($CardNumber.length -le 5) {
        Write-Verbose "Just a card, no facility code provided"
        $CardNumber = $CardNumber.ToString().PadLeft(5, "0")
        $FacilityCode = "00000"
        $FacilityCodeBinary = [System.Convert]::ToString($FacilityCode, 2).padleft(8, "0")
        $CardID = "$CardNumber"[-5..-1] -join '' #Last 5 digits
        $CardIDBinary = [System.Convert]::ToString($CardID, 2).padleft(16, "0")
    }
    elseif ($CardNumber.length -lt 16) {
        Write-Verbose "Running as a base 10 string"
        $CardNumber = $CardNumber.ToString().PadLeft(10, "0")
        $FacilityCode = "$CardNumber"[-8..-6] -join '' #The possible Facility codes
        $FacilityCodeBinary = [System.Convert]::ToString($FacilityCode, 2).padleft(8, "0")
        Write-Verbose $FacilityCodeBinary

        $CardID = "$CardNumber"[-5..-1] -join '' #Last 5 digits
        $CardIDBinary = [System.Convert]::ToString($CardID, 2).padleft(16, "0")
        Write-Verbose $CardIDBinary
    }
    elseif ($CardNumber.length -le 24) {
        Write-Verbose "Running as a binary string"
        if ($cardnumber -match "[^01]") {
            throw "Binary string is not in Binary!"
        }
        else {
            $FacilityCodeBinary = $CardNumber[-24..-17] -join ''
            $CardIDBinary = $CardNumber[-16..-1] -join ''
        }
    }
    else {
        throw "No valid format specified. Either type in a less than 16 character decimal string or a 16-24 character binary string."
    }

    # Even Parity check on the first 12 digits of the card.
    [string]$EvenParity = ($FacilityCodeBinary + $CardIDBinary[0..3] -replace "[^1]").length % 2

    # Odd Parity check on the last 12 digits.
    # When dividing by 2, if there is no remainder, we need to set the Odd Parity bit to on so that there are an odd number of 1's.
    if ((($CardIDBinary[4..15] -replace "[^1]") -join '').length % 2) { $OddParity = 0 }
    else { $OddParity = 1 }

    $OutputString = [System.Convert]::ToString(
        [System.Convert]::ToInt32($EvenParity + $FacilityCodeBinary + $CardIDBinary + $OddParity, 2),
        16
    ).PadLeft(8, "0")
    return $OutputString
}

And here is the function I wrote to decode back:

function ConvertFrom-KonicaHex {
    <#
    .SYNOPSIS
    Convert HID card to Konica Hex

    .EXAMPLE
    ~> ConvertFrom-KonicaHex 020210d7
    0000102155
    .EXAMPLE
    ~> ConvertFrom-KonicaHex 000010d7
    0000002155
    .EXAMPLE
    ~> ConvertFrom-KonicaHex 10d7
    0000002155
    #>
    [cmdletbinding()]
    param (
        [string]$HexValue
    )
    $BinaryString = ([System.Convert]::ToString([System.Convert]::ToInt32($HexValue, 16), 2)).PadLeft(26, "0").tostring()
    # Convert HexValue as Hex to base 10 and then convert that to binary string.
    # Finally, pad with 0's until we hit 26 characters.
    $FacilityCode = [System.Convert]::ToInt32(($BinaryString[1..8] -join ''), 2).ToString().PadLeft(5, "0")
    $CardID = [System.Convert]::ToInt32(($BinaryString[9..24] -join ''), 2).ToString().PadLeft(5, "0")

    return $FacilityCode + $CardID
}

To round out usage, here are a few examples:

~> ConvertTo-KonicaHex 002155
000010d7
~> ConvertTo-KonicaHex 2155
000010d7
~> ConvertTo-KonicaHex 02155
000010d7
~> ConvertTo-KonicaHex 102155
020210d7

~> ConvertFrom-KonicaHex 020210d7
0000102155
~> ConvertFrom-KonicaHex 000010d7
0000002155
~> ConvertFrom-KonicaHex 10d7
0000002155

You can view those yourself in Posh if you know how to use help:

 Get-Help ConvertTo-KonicaHex -Examples

Discussion

Discuss on reddit