首页 > 解决方案 > 在 python3 中加密 Radius 密码

问题描述

我在 python2 中使用了以下代码来加密 Radius Password 属性:

    def Encrypt_Pass(password, authenticator, secret):
        m = md5()
        m.update(secret + authenticator)
        return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(password.ljust(16, '\0')[:16], m.digest()[:16]))

这在 python3 中不起作用,m.update 行出错:

TypeError: Unicode-objects must be encoded before hashing

在我添加编码之后:

   def Encrypt_Pass(password, authenticator, secret):
        m = md5()
        m.update(secret.encode('ascii') + authenticator.encode('ascii'))
        return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(password.ljust(16, '\0')[:16], m.digest()[:16]))

我仍然失败:

TypeError: ord() expected string of length 1, but int found

现在我迷路了,忘记了算法的细节,你能帮忙吗?如何更改最后一行以使用 python3 ?(对于python2来说工作得很好)。

更新:我已经进行了故障排除,代码如下:

m = md5()
        m.update(secret.encode('ascii') + authenticator.encode('ascii'))
        print("Secret {}, encode {}".format(secret,secret.encode('ascii')))
        print("auth {}, encode {}".format(authenticator, authenticator.encode('ascii')))
        print("m digest: {}".format(m.digest()[:16]))
        print("passwd: {}".format(password.ljust(16, '\0')[:16]))
        #return "".join(chr(ord(x) ^ y) for x, y in zip(password.ljust(16, '\0')[:16], m.digest()[:16]))
        for x, y in zip(password.ljust(16, '\0')[:16], m.digest()[:16]):
            print("JOIN ord x: {} y: {}".format(ord(x),y))
            a = "".join(chr(ord(x) ^ y))
            print("a: {}".format(chr(ord(x) ^ y)))

我有:

Secret cisco, encode b'cisco'
auth 5002CVWVKCX13QTN, encode b'5002CVWVKCX13QTN'
m digest: b'\xf8\xb9\xc2\x1foZ~h\xff@,\x87\x07\xcc:m'
passwd: cisco
JOIN ord x: 99 y: 248
a: 
JOIN ord x: 105 y: 185
a: Ð
JOIN ord x: 115 y: 194
a: ±
JOIN ord x: 99 y: 31
a: |
JOIN ord x: 111 y: 111
a: 
JOIN ord x: 0 y: 90
a: Z
JOIN ord x: 0 y: 126
a: ~
JOIN ord x: 0 y: 104
a: h
JOIN ord x: 0 y: 255
a: ÿ
JOIN ord x: 0 y: 64
a: @
JOIN ord x: 0 y: 44
a: ,
JOIN ord x: 0 y: 135
a: 
JOIN ord x: 0 y: 7
a: 
JOIN ord x: 0 y: 204
a: Ì
JOIN ord x: 0 y: 58
a: :
JOIN ord x: 0 y: 109
a: m

所以看起来我只需要使用 y 而不是 ord(y) 吗?但即使使用该密码,python3 的编码仍然不正确 :(

标签: python-3.xradius

解决方案


推荐阅读