Advertisement
Guest User

Untitled

a guest
May 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.88 KB | None | 0 0
  1. Imports System.Numerics
  2.  
  3. Public Class clsNLS
  4.   'Generator
  5.   Private NLS_G() As Byte = {&H2F}
  6.   'Modulus
  7.   Private NLS_N() As Byte = {&H87, &HC7, &H23, &H85, &H65, &HF6, &H16, &H12, &HD9, &H12, &H32, &HC7, &H78, &H6C, &H97, &H7E, &H55, &HB5, &H92, &HA0, &H8C, &HB6, &H86, &H21, &H3, &H18, &H99, &H61, &H8B, &H1A, &HFF, &HF8}
  8.   Private m_Username As String
  9.   Private m_Password As String
  10.   Private m_Salt() As Byte
  11.   Private m_a() As Byte
  12.   Private m_B() As Byte
  13.  
  14.   'random value between 0 and N
  15.   Private Sub Generate_a()
  16.     ReDim m_a(31)
  17.     Do
  18.       For I As Integer = 0 To 31 : m_a(I) = Int(Rnd() * 256) : Next I
  19.     Loop While ByteArrayToBigInt(m_a) > ByteArrayToBigInt(NLS_N)
  20.   End Sub
  21.  
  22.   Public Sub New(ByVal Username As String, ByVal Password As String)
  23.     m_Username = Username.ToUpper
  24.     m_Password = Password.ToUpper
  25.     Generate_a()
  26.   End Sub
  27.  
  28.   '256 bit random value sent from server, or generated on account creation
  29.   Public Property Salt As Byte()
  30.     Get
  31.       If IsNothing(m_Salt) Then
  32.         ReDim m_Salt(31)
  33.         For I As Integer = 0 To 31 : m_Salt(I) = Int(Rnd() * 256) : Next I
  34.       End If
  35.       Return m_Salt
  36.     End Get
  37.     Set(ByVal value As Byte())
  38.       m_Salt = value
  39.     End Set
  40.   End Property
  41.  
  42.   'Username & password hash, SHA1(s, SHA1(Username, ":", Password))
  43.   Public ReadOnly Property x As Byte()
  44.     Get
  45.       Dim SHAup() As Byte = StandardSHA(m_Username & ":" & m_Password)
  46.       Dim GenX(m_Salt.Length + SHAup.Length - 1) As Byte
  47.       Array.Copy(m_Salt, 0, GenX, 0, m_Salt.Length)
  48.       Array.Copy(SHAup, 0, GenX, m_Salt.Length, SHAup.Length)
  49.       Return StandardSHA(GenX)
  50.     End Get
  51.   End Property
  52.  
  53.   'Verifier, g ^ x % N
  54.   Public ReadOnly Property Verifier As Byte()
  55.     Get
  56.       Return BigIntToByteArray(BigInteger.ModPow(ByteArrayToBigInt(NLS_G), ByteArrayToBigInt(x), ByteArrayToBigInt(NLS_N)))
  57.     End Get
  58.   End Property
  59.  
  60.   'Client Key, g ^ a % N
  61.   Public ReadOnly Property A As Byte()
  62.     Get
  63.       Return BigIntToByteArray(BigInteger.ModPow(ByteArrayToBigInt(NLS_G), ByteArrayToBigInt(m_a), ByteArrayToBigInt(NLS_N)))
  64.     End Get
  65.   End Property
  66.  
  67.   'Server Key
  68.   Public Property B As Byte()
  69.     Get
  70.       Return m_B
  71.     End Get
  72.     Set(ByVal value As Byte())
  73.       m_B = value
  74.     End Set
  75.   End Property
  76.  
  77.   'First four bytes of SHA1(B)
  78.   Private ReadOnly Property u As Byte()
  79.     Get
  80.       Dim SHA_B() As Byte = StandardSHA(m_B)
  81.       ReDim Preserve SHA_B(3)
  82.       Array.Reverse(SHA_B)
  83.       Return SHA_B
  84.     End Get
  85.   End Property
  86.  
  87.   'Secret, ((N + B - v) % N) ^ (a + u * x) % N
  88.   Public ReadOnly Property S As Byte()
  89.     Get
  90.       Return BigIntToByteArray(BigInteger.ModPow(BigInteger.Remainder(ByteArrayToBigInt(NLS_N) + ByteArrayToBigInt(m_B) - ByteArrayToBigInt(Verifier), ByteArrayToBigInt(NLS_N)), ByteArrayToBigInt(m_a) + ByteArrayToBigInt(u) * ByteArrayToBigInt(x), ByteArrayToBigInt(NLS_N)))
  91.     End Get
  92.   End Property
  93.  
  94.   'Password Proof, SHA1(even bytes of S) & SHA1(odd bytes of S)
  95.   Public ReadOnly Property K As Byte()
  96.     Get
  97.       Dim bEven(S.Length / 2 - 1) As Byte
  98.       Dim bOdd(S.Length / 2 - 1) As Byte
  99.       For I As Byte = 0 To S.Length - 1
  100.         If I Mod 2 = 0 Then
  101.           bEven(Math.Floor(I / 2)) = S(I)
  102.         Else
  103.           bOdd(Math.Floor(I / 2)) = S(I)
  104.         End If
  105.       Next I
  106.       Dim SHAEven() As Byte = StandardSHA(bEven)
  107.       Dim SHAOdd() As Byte = StandardSHA(bOdd)
  108.       Dim bK(SHAEven.Length * 2 - 1) As Byte
  109.       For I As Byte = 0 To SHAEven.Length - 1
  110.         bK(I * 2) = SHAEven(I)
  111.         bK(I * 2 + 1) = SHAOdd(I)
  112.       Next I
  113.       Return bK
  114.     End Get
  115.   End Property
  116.  
  117.   'Client Password Proof, SHA1(SHA1(g) xor SHA1(N), SHA1(Username), s, A, B, K)
  118.   Public ReadOnly Property M1 As Byte()
  119.     Get
  120.       Dim G_XOR_N() As Byte = BigIntToByteArray(ByteArrayToBigInt(StandardSHA(NLS_G)) Xor ByteArrayToBigInt(StandardSHA(NLS_N)))
  121.       Dim SHA_User() As Byte = StandardSHA(m_Username)
  122.       Dim SHA_HashThis(G_XOR_N.Length + SHA_User.Length + m_Salt.Length + A.Length + m_B.Length + K.Length - 1) As Byte
  123.       Dim lPos As Integer = 0
  124.       Array.Copy(G_XOR_N, 0, SHA_HashThis, lPos, G_XOR_N.Length) : lPos += G_XOR_N.Length
  125.       Array.Copy(SHA_User, 0, SHA_HashThis, lPos, SHA_User.Length) : lPos += SHA_User.Length
  126.       Array.Copy(m_Salt, 0, SHA_HashThis, lPos, m_Salt.Length) : lPos += m_Salt.Length
  127.       Array.Copy(A, 0, SHA_HashThis, lPos, A.Length) : lPos += A.Length
  128.       Array.Copy(m_B, 0, SHA_HashThis, lPos, m_B.Length) : lPos += m_B.Length
  129.       Array.Copy(K, 0, SHA_HashThis, lPos, K.Length) : lPos += K.Length
  130.       Return StandardSHA(SHA_HashThis)
  131.     End Get
  132.   End Property
  133.  
  134.   'M2 = SHA1(A, M[1], K)
  135.   Public Function VerifyServerProof(ByVal M2() As Byte) As Boolean
  136.     Dim TmpM2() As Byte = StandardSHA(System.Text.Encoding.GetEncoding(LATIN_1).GetString(A) & System.Text.Encoding.GetEncoding(LATIN_1).GetString(M1) & System.Text.Encoding.GetEncoding(LATIN_1).GetString(K))
  137.     Return BitConverter.ToString(M2) = BitConverter.ToString(TmpM2)
  138.   End Function
  139.  
  140.   Public Function ValidateServerSignature(ByVal Signature() As Byte, ByVal IPAddress As Net.EndPoint)
  141.     ReDim Preserve Signature(Signature.Length)
  142.     Dim RSA_D As New BigInteger({1, 0, 1, 0})
  143.     Dim RSA_C As New BigInteger(Signature)
  144.     Dim RSA_N As New BigInteger({&HD5, &HA3, &HD6, &HAB, &HF, &HD, &HC5, &HF, &HC3, &HFA, &H6E, &H78, &H9D, &HB, &HE3, &H32,
  145.                                  &HB0, &HFA, &H20, &HE8, &H42, &H19, &HB4, &HA1, &H3A, &H3B, &HCD, &HE, &H8F, &HB5, &H56, &HB5,
  146.                                  &HDC, &HE5, &HC1, &HFC, &H2D, &HBA, &H56, &H35, &H29, &HF, &H48, &HB, &H15, &H5A, &H39, &HFC,
  147.                                  &H88, &H7, &H43, &H9E, &HCB, &HF3, &HB8, &H73, &HC9, &HE1, &H77, &HD5, &HA1, &H6, &HA6, &H20,
  148.                                  &HD0, &H82, &HC5, &H2D, &H4D, &HD3, &H25, &HF4, &HFD, &H26, &HFC, &HE4, &HC2, &H0, &HDD, &H98,
  149.                                  &H2A, &HF4, &H3D, &H5E, &H8, &H8A, &HD3, &H20, &H41, &H84, &H32, &H69, &H8E, &H8A, &H34, &H76,
  150.                                  &HEA, &H16, &H8E, &H66, &H40, &HD9, &H32, &HB0, &H2D, &HF5, &HBD, &HE7, &H57, &H51, &H78, &H96,
  151.                                  &HC2, &HED, &H40, &H41, &HCC, &H54, &H9D, &HFD, &HB6, &H8D, &HC2, &HBA, &H7F, &H69, &H8D, &HCF, &H0})
  152.     Dim RSA_M As BigInteger = BigInteger.ModPow(RSA_C, RSA_D, RSA_N)
  153.     Return BitConverter.ToString(BigIntToByteArray(RSA_M)).Substring(0, 11) = BitConverter.ToString(CType(IPAddress, System.Net.IPEndPoint).Address.GetAddressBytes())
  154.   End Function
  155.  
  156.  
  157.   Private Function ByteArrayToBigInt(ByVal b() As Byte) As BigInteger
  158.     ReDim Preserve b(b.Length)
  159.     Return New BigInteger(b)
  160.   End Function
  161.  
  162.   Private Function BigIntToByteArray(ByVal bI As BigInteger) As Byte()
  163.     Dim bTmp() As Byte = bI.ToByteArray
  164.     If bTmp(bTmp.Length - 1) = 0 Then ReDim Preserve bTmp(bTmp.Length - 2)
  165.     Return bTmp
  166.   End Function
  167. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement