Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Base36-Encode
- {
- param ([parameter(ValueFromPipeline = $true)][int]$base10 = "")
- $alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- do {
- $remainder = ($base10 % 36)
- $char = $alphabet.Substring($remainder, 1)
- $base36 = "$char$base36"
- $base10 = ($base10 - $remainder) / 36
- } while ($base10 -gt 0)
- $base36
- }
- function Reverse-String
- {
- param ([parameter(ValueFromPipeline = $true)][string]$orig = "")
- $len = $orig.Length
- for ($e = $len-1; $e -ge 0; $e--) {
- $curChar =$orig[$e]
- $reverted = "$reverted$curChar"
- }
- $reverted
- }
- "----Hach-Lange SC1000 Controller keygen----"
- "`r`n"
- " PART 1 of key"
- $arbtrPart1_input = Read-Host -Prompt " Arbitrary value of part 1 (10...99)"
- if ($arbtrPart1_input.Length -eq 0) {
- $arbtrPart1 = Get-Random -Minimum 10 -Maximum 99
- " - no specified value, chosen random: $arbtrPart1"
- } else {
- $arbtrPart1 = [int]$arbtrPart1_input
- if (($arbtrPart1 -gt 99) -or ($arbtrPart1 -lt 10)) {
- throw "Arbitrary value of part 1 must be between 10 and 99 including extremes"
- }
- }
- " 2nd value of part 1 is 0 -> Algorithm version 0"
- $algoVersion = 0
- "`r`n"
- " PART 2 of key"
- #NOTE - Known Feature IDs: 1 - Modbus/TCP Server
- # 2 - OPC UA (on newer firmware versions only)
- $featureId_input = Read-Host -Prompt " Feature ID (0...128)"
- if ($featureId_input.Length -eq 0) {
- $featureId = 1
- " - no feature ID chosen, assuming Modbus/TCP server: feature ID 1"
- } else {
- $featureId = [int]$featureId_input
- if (($featureId -gt 128) -or ($featureId -lt 0)) {
- throw "Feature ID must be between 0 and 128 including extremes"
- }
- }
- $arbtrPart2_1_input = Read-Host -Prompt " 1st arbitrary value of part 2 (0...99)"
- if ($arbtrPart2_1_input.Length -eq 0) {
- $arbtrPart2_1 = Get-Random -Minimum 0 -Maximum 99
- " - no specified value, chosen random: $arbtrPart2_1"
- } else {
- $arbtrPart2_1 = [int]$arbtrPart2_1_input
- if (($arbtrPart2_1 -gt 99) -or ($arbtrPart2_1 -lt 0)) {
- throw "1st arbitrary value of part 2 must be between 0 and 99 including extremes"
- }
- }
- $arbtrPart2_2_input = Read-Host -Prompt " 2nd arbitrary value of part 2 (0...999)"
- if ($arbtrPart2_2_input.Length -eq 0) {
- $arbtrPart2_2 = Get-Random -Minimum 0 -Maximum 999
- " - no specified value, chosen random: $arbtrPart2_2"
- } else {
- $arbtrPart2_2 = [int]$arbtrPart2_2_input
- if (($arbtrPart2_2 -gt 999) -or ($arbtrPart2_2 -lt 0)) {
- throw "2nd arbitrary value of part 2 must be between 0 and 999 including extremes"
- }
- }
- " 4th value of part 2 is 99 -> version 9.9 -> Unlimited"
- $version = 99
- "`r`n"
- " PART 3 of key"
- " Expiry date (for calculating part 3) is 9999-10-10 -> Unlimited"
- $expiryDate = 99991010
- "`r`n"
- " PART 4 of key"
- do {
- $serialNum_input = Read-Host -Prompt " Serial number of SC1000 controller"
- } until ($serialNum_input.Length -gt 0)
- $serialNum = [long]$serialNum_input
- $obfuscatingNumber = $arbtrPart1 * $arbtrPart2_1 * $arbtrPart2_2
- #some obfuscation factor made with the arbitrary values
- $part1_base10 = ($arbtrPart1 * 10) + $algoVersion
- $part2_base10 = (($featureId * 10000000) + ($arbtrPart2_1 * 100000) +
- ($arbtrPart2_2 * 100) + $version)
- $part3_base10 = $expiryDate + $obfuscatingNumber
- $part4_base10 = $serialNum + $obfuscatingNumber
- $part1_partial = Reverse-String (Base36-Encode $part1_base10)
- #NOTE: This makes 4th and 5th character of the 1st part of the key;
- # the first 4 chars are the CRC16/MODBUS of the whole string
- $part2 = Reverse-String (Base36-Encode $part2_base10)
- $part3 = Reverse-String (Base36-Encode $part3_base10)
- $part4 = Reverse-String ([string]$part4_base10)
- #CRC16/MODBUS calculation - to be byte-swapped
- #and put at the beginning of the string
- $buf = [byte[]][char[]]"$part1_partial$part2$part3$part4"
- $crc = 0xFFFF
- for ($pos = 0; $pos -lt $buf.Count; $pos++) {
- $crc = $crc -bxor [UInt16]$buf[$pos]
- for ($i = 8; $i -ne 0; $i--) {
- if (($crc -band 0x0001) -ne 0) {
- $crc = ($crc -shr 1) -bxor 0xA001
- } else {
- $crc = $crc -shr 1
- }
- }
- }
- #swap bytes, and format as hexadecimal
- $crc_asStr = "{0:X}" -f (([uint16]$crc -shl 8) -bor ([uint16]$crc -shr 8))
- "`r`n"
- "--> $crc_asStr$part1_partial-$part2-$part3-$part4"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement