首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >整数编码格式

整数编码格式
EN

Stack Overflow用户
提问于 2022-04-14 17:45:19
回答 1查看 50关注 0票数 0

我遇到了一些PIN编码,我正在试图弄清楚,这样我就可以改进工作场所使用的web应用程序。

当我重置用户的PIN(在本例中,只是我自己的测试目的)时,我看到了以下内容:

代码语言:javascript
复制
PIN      VALUE
000000 = 7F55858585858585
111111 = 7F55868686868686
222222 = 7F55878787878787
999999 = 7F558E8E8E8E8E8E

000001 = 7F01313131313132
000011 = 7F55858585858686
000111 = 7F01313131323232
001111 = 7F55858586868686
011111 = 7F01313232323232
000002 = 7F02323232323234
100000 = 7F01323131313131
111112 = 7F03343434343435

123456 = 7F0738393A3B3C3D
654321 = 7F073D3C3B3A3938

1357924680 = 7F01323436383A3335373931

1111111111 = 7F5586868686868686868686
1234567890 = 7F0132333435363738393A31

这显然只是六边形,总是从7F (1111111或127)开始,但我没有看到如何选择下两个字符的模式。这两个字符似乎是转换PIN的决定性值。

例如:

代码语言:javascript
复制
000000       = 7F 55 858585858585
7F (hex)     = 127 (dec) or 1111111 (bin) ## appears to not be used in the calculation?
55 (hex)     =  85 (dec) or 1010101 (bin) 
0 (PIN) + 85 =  85
000000       =  858585858585

111111       = 7F 55 868686868686
7F (hex)     = 127 (dec) or 1111111 (bin) ## appears to not be used in the calculation?
55 (hex)     =  85 (dec)
1 (PIN) + 85 =  86
111111       =  868686868686

但同时也:

代码语言:javascript
复制
1357924680   = 7F 01 323436383A3335373931
01 (hex)     = 31 (dec) ?
1 (PIN) + 31 = 32
1357924680   = 323436383A3335373931

任何帮助我向正确的方向将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-04-25 20:35:28

我在您的minimal reproducible example中没有看到足够的数据来揭示如何确定pinshift值的算法(提供给pin_to_hex函数)。在以下解决方案中使用了一个随机值:

代码语言:javascript
复制
def hex_to_pin( pinhex: str) -> list:
    '''
    decode a PIN from a particular hexadecimal-formatted string
    hex_to_pin('7F0738393A3B3C3D')
    
    inverse of the "pin_to_hex" function (any of the following):
    hex_to_pin(pin_to_hex('123456', 7))
    pin_to_hex(*hex_to_pin('7F0738393A3B3C3D'))
    '''
    xxaux = bytes.fromhex(pinhex)
    return [bytes([x - xxaux[1] for x in xxaux[2:]]).decode(),
            xxaux[1]]


def pin_to_hex( pindec: str, pinshift: int, upper=False) -> str:
    '''
    encode a PIN to a particular hexadecimal-formatted string
    pin_to_hex('123456', 7)
    
    inverse of the "hex_to_pin" function (any of the following):
    pin_to_hex(*hex_to_pin('7F0738393A3B3C3D'),True)
    hex_to_pin(pin_to_hex('123456', 7))
    '''
    shift_ = max( 1, pinshift % 199) ## 134 for alpha-numeric PIN code
    retaux = [b'\x7F', shift_.to_bytes(1, byteorder='big')]
    for digit_ in pindec.encode():
        retaux.append( (digit_ + shift_).to_bytes(1, byteorder='big'))
    if upper:
        return (b''.join(retaux)).hex().upper()
    else:
        return (b''.join(retaux)).hex()


def get_pin_shift( pindec: str) -> int:
    '''
    determine "pinshift" parameter for the "pin_to_hex" function
    currently returns a random number
    '''
    return random.randint(1,198)  ## (1,133) for alpha-numeric PIN code
    

hexes = [
        '7F01323436383A3335373931',
        '7F0738393A3B3C3D',
        '7F558E8E8E8E8E8E'
        ]
print("hex_to_pin:")
maxlen = len( max(hexes, key=len))
deces = []
for xshex in hexes:
    xsdec = hex_to_pin( xshex)
    print( f"{xshex:<{maxlen}} ({xsdec[1]:>3}) {xsdec[0]}")
    deces.append(xsdec[0])

import random
print("pin_to_hex:")
for xsdec in deces:
    xsshift = get_pin_shift( xsdec)
    xshex = pin_to_hex( xsdec, xsshift)
    print( f"{xshex:<{maxlen}} ({xsshift:>3}) {xsdec}")

输出 SO\71875753.py

代码语言:javascript
复制
hex_to_pin:
7F01323436383A3335373931 (  1) 1357924680
7F0738393A3B3C3D         (  7) 123456
7F558E8E8E8E8E8E         ( 85) 999999
pin_to_hex:
7f1041434547494244464840 ( 16) 1357924680
7f4e7f8081828384         ( 78) 123456
7f013a3a3a3a3a3a         (  1) 999999
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71875753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档