更改用户的 authKey

Change authKey of a user

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

所以我想出了如何做到这一点。这是一个有点复杂的过程。我遵循了 this 文档,即 rfc2574。对 "keyChange ::=" 执行 ctrl+F,您将找到引导您完成算法以生成 keyChange 值的段落。以下代码可靠地生成了 keyChange 值。从这一点开始,您所要做的就是将 keyChange 值推送到 usmAuthKeyChange OID。如果要更改隐私密码,请将 keyChange 值推送到 usmPrivKeyChange OID。很惭愧地说,由于时间紧迫,我没有时间完全完成这项工作,所以在使用 SHA 时,我不得不编写一个全新的方法来完成几乎完全相同的事情。再一次,我对 post 感到羞愧,但我知道我的头撞到墙上的程度有多大,如果有人稍后来到这里看到这个,我希望他们知道该怎么做而无需经过斗争。

以下是使用 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看看算法。