SHARE
TWEET

REALbasic 13-digit Key Encoder/Decoder

a guest Jun 16th, 2011 384 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Encode13(Product As UInt32, PublicValue As UInt32, PrivateValue As UInt32) As String
  2.  
  3.   Dim Salt As Integer = &H13AC9741
  4.   Dim aOrd(11) As Integer = Array(6, 0, 2, 9, 3, 11, 1, 7, 5, 4, 10, 8)
  5.   Dim Encoded As New MemoryBlock(12)
  6.   Dim C As Byte, i As Integer, dw As UInt32
  7.  
  8.   Encoded.StringValue(0, 2) = Right("00" + Str(Product), 2)
  9.   Encoded.StringValue(2, 7) = Right("0000000" + Str(PublicValue), 7)
  10.   Encoded.StringValue(9, 3) = Right("000" + Str(PrivateValue), 3)
  11.   Dim Key As String = Encoded.StringValue(0, 12)
  12.   Encoded = New MemoryBlock(13)
  13.  
  14.   For i = 11 To 0 Step -1
  15.     C = Asc(Mid(Key, 1 + i, 1))
  16.     If C <= 55 Then
  17.       Encoded.Byte(aOrd(I)) = (C Xor (Salt And 7))
  18.       Salt = ShiftRight(Salt, 3)
  19.     Else
  20.       Encoded.Byte(aOrd(I)) = (C Xor I And 1)
  21.     End If
  22.   Next
  23.  
  24.   dw = 3
  25.   For i = 0 To 11
  26.     dw = dw + BitXor(Val(Chr(Encoded.Byte(i))), (dw * 2))
  27.   Next
  28.   dw = dw Mod 10
  29.   Encoded.Byte(12) = Asc(Str(dw))
  30.  
  31.   Return Encoded.StringValue(0, 13)
  32.  
  33. End Function
  34.  
  35. Function Decode13(Key As String, ByRef ProductValue As UInt32, ByRef PublicValue As UInt32, ByRef PrivateValue As UInt32) As Boolean
  36.  
  37.   Dim Salt As Integer = &H13AC9741
  38.   Dim aOrd(11) As Integer = Array(6, 0, 2, 9, 3, 11, 1, 7, 5, 4, 10, 8)
  39.   Dim Decoded As New MemoryBlock(12)
  40.   Dim C As Byte, i As Integer, dw As UInt32
  41.  
  42.   For i = 11 To 0 Step -1
  43.     C = Asc(Mid(Key, 1 + aOrd(i), 1))
  44.     If C <= 55 Then
  45.       Decoded.Byte(i) = (C Xor (Salt And 7))
  46.       Salt = ShiftRight(Salt, 3)
  47.     Else
  48.       Decoded.Byte(i) = (C Xor i And 1)
  49.     End If
  50.   Next
  51.  
  52.   dw = 3
  53.   For i = 0 To 11
  54.     dw = dw + BitXor(Val(Mid(Key, 1 + i, 1)), (dw * 2))
  55.   Next
  56.   dw = dw Mod 10
  57.  
  58.   If Val(Mid(Key, 13, 1)) = dw Then
  59.    
  60.     ProductValue = Val(Decoded.StringValue(0, 2))
  61.     PublicValue = Val(Decoded.StringValue(2, 7))
  62.     PrivateValue = Val(Decoded.StringValue(9, 3))
  63.     Return True
  64.    
  65.   Else
  66.     Return False
  67.    
  68.   End If
  69.  
  70. End Function
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top