2017-06-12 65 views
0

使用SNMP版本3,我正在创建一个用户。 现在,我已经设置了我克隆用户的地方,并且工作得很好。但是,我需要更改新用户的authKey。我怎样才能做到这一点?我知道authKeyChange的oid,但是,我不知道如何生成新的密钥。我如何生成该密钥?可以使用SNMPSharpNet完成吗? 如果在创建用户时有更简单的方法来做到这一点,我也可以做到这一点。任何改变authKey(和privKey,但一次一步)的方式非常感谢。我使用VB.net,如果它意味着什么。更改用户的authKey

+0

大家都说“根据clone-from用户的秘密 privKey和用于新用户的密钥 生成keyChange值”,但没有人说HOW。 –

回答

0

所以我想出了如何做到这一点。这是一个复杂的过程。我跟着this文件,这是rfc2574。为“keyChange :: =”做一个ctrl + F,你会发现这个段落引导你通过算法来生成keyChange值。以下代码可靠地生成keyChange值。所有你需要做的就是将keyChange值推送到usmAuthKeyChange OID。如果要更改隐私密码,请将keyChange值推送到usmPrivKeyChange OID。我很惭愧地说,由于时间紧迫,我没有时间完成这项工作,所以当使用SHA时,我必须编写一个全新的方法,几乎​​完成同样的事情。再次,我很惭愧地把它贴出来,但我知道我有多么b against我的头靠在墙上,如果稍后有人来这里看到这些,我希望他们知道该怎么做,而不用经历斗争。

这里是所有你需要使用VB.Net和SNMPSharpNet库中的代码:

Private Function GenerateKeyChange(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte() 

    Dim authProto As AuthenticationDigests = param.Authentication 
    Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto) 
    Dim L As Integer = hash.DigestLength 
    Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId) 
    Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId) 
    Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L)) 
    Dim temp() As Byte = oldKey 
    Dim delta(L - 1) As Byte 
    Dim iterations As Integer = ((newKey.Length - 1)/L) - 1 
    Dim k As Integer = 0 
    If newKey.Length > L Then 
     For k = 0 To iterations 

      'Append random to temp 
      Dim merged1(temp.Length + random.Length - 1) As Byte 
      temp.CopyTo(merged1, 0) 
      random.CopyTo(merged1, random.Length) 

      'Store hash of temp in itself 
      temp = hash.ComputeHash(merged1, 0, merged1.Length) 

      'Generate the first 16 values of delta 
      For i = 0 To L - 1 
       delta(k * L + i) = temp(i) Xor newKey(k * L + i) 
      Next 
     Next 
    End If 

    'Append random to temp 
    Dim merged(temp.Length + random.Length - 1) As Byte 
    temp.CopyTo(merged, 0) 
    random.CopyTo(merged, temp.Length) 

    'Store hash of temp in itself 
    temp = hash.ComputeHash(merged, 0, merged.Length) 

    'Generate the first 16 values of delta 
    For i = 0 To (newKey.Length - iterations * L) - 1 
     delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i) 
    Next 

    Dim keyChange(delta.Length + random.Length - 1) As Byte 
    random.CopyTo(keyChange, 0) 
    delta.CopyTo(keyChange, random.Length) 
    Return keyChange 
End Function 

Private Function GenerateKeyChangeShaSpecial(ByVal newPass As String, ByVal oldPass As String, ByRef target As UdpTarget, ByRef param As SecureAgentParameters) As Byte() 

    Dim authProto As AuthenticationDigests = param.Authentication 
    Dim hash As IAuthenticationDigest = Authentication.GetInstance(authProto) 
    Dim L As Integer = 16 

    Dim oldKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(oldPass), param.EngineId) 
    Dim newKey() As Byte = hash.PasswordToKey(Encoding.UTF8.GetBytes(newPass), param.EngineId) 

    Array.Resize(oldKey, L) 
    Array.Resize(newKey, L) 

    Dim random() As Byte = Encoding.UTF8.GetBytes(GenerateRandomString(L)) 
    Dim temp() As Byte = oldKey 
    Dim delta(L - 1) As Byte 
    Dim iterations As Integer = ((newKey.Length - 1)/L) - 1 
    Dim k As Integer = 0 
    If newKey.Length > L Then 
     For k = 0 To iterations 
      'Append random to temp 
      Dim merged1(temp.Length + random.Length - 1) As Byte 
      temp.CopyTo(merged1, 0) 
      random.CopyTo(merged1, random.Length) 

      'Store hash of temp in itself 
      temp = hash.ComputeHash(merged1, 0, merged1.Length) 
      Array.Resize(temp, L) 

      'Generate the first 16 values of delta 
      For i = 0 To L - 1 
       delta(k * L + i) = temp(i) Xor newKey(k * L + i) 
      Next 
     Next 
    End If 

    'Append random to temp 
    Dim merged(temp.Length + random.Length - 1) As Byte 
    temp.CopyTo(merged, 0) 
    random.CopyTo(merged, temp.Length) 

    'Store hash of temp in itself 
    temp = hash.ComputeHash(merged, 0, merged.Length) 
    Array.Resize(temp, L) 

    'Generate the first 16 values of delta 
    For i = 0 To (newKey.Length - iterations * L) - 1 
     delta(iterations * L + i) = temp(i) Xor newKey(iterations * L + i) 
    Next 

    Dim keyChange(delta.Length + random.Length - 1) As Byte 
    random.CopyTo(keyChange, 0) 
    delta.CopyTo(keyChange, random.Length) 
    Return keyChange 
End Function 

Private Function GenerateRandomString(ByVal length As Integer) As String 
    Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 
    Dim r As New Random 
    Dim sb As New StringBuilder 
    For i As Integer = 1 To length 
     Dim idx As Integer = r.Next(0, 51) 
     sb.Append(s.Substring(idx, 1)) 
    Next 
    Return sb.ToString() 
End Function 

同样,我是如此清楚地知道这个代码是可怕的,但它的作品,这是我在此期间需要。我知道这是技术性债务,而不是我应该编码的方式,但它在这里,我希望你能从中获得一些用处。

如果这不起作用,不要忘记去frc2574看看算法。