你好,伙计们,
我的CV1 RSA证书稍微有点modified.So,我不想使用asn1wrap来解析'der‘文件,因为它有时太复杂了,相反,由于标签已经为CV1证书修复了,我可以通过将二进制数据转换为十六进制并提取所需的数据范围,来解析这个'der’文件的HEX数据。
但是,对于表示,我希望 OID 采用点格式,例如:绝对OID
我可以将整个十六进制数据中的十六进制字符串提取为:'060D2B0621040BA946964812D10905'
任何可以直接执行此转换的python3函数。或者有人能用逻辑来帮助转换相同的逻辑。
发布于 2018-04-04 18:20:23
为任何感兴趣的人找到了答案。在不使用pyasn1或asn1crypto的情况下,我没有找到将十六进制值转换为OID符号的任何包。所以我浏览了一下其他语言的代码,并在python中创建了一个代码。
def notation_OID(oidhex_string):
''' Input is a hex string and as one byte is 2 charecters i take an
empty list and insert 2 characters per element of the list.
So for a string 'DEADBEEF' it would be ['DE','AD','BE,'EF']. '''
hex_list = []
for char in range(0,len(oidhex_string),2):
hex_list.append(oidhex_string[char]+oidhex_string[char+1])
''' I have deleted the first two element of the list as my hex string
includes the standard OID tag '06' and the OID length '0D'.
These values are not required for the calculation as i've used
absolute OID and not using any ASN.1 modules. Can be removed if you
have only the data part of the OID in hex string. '''
del hex_list[0]
del hex_list[0]
# An empty string to append the value of the OID in standard notation after
# processing each element of the list.
OID_str = ''
# Convert the list with hex data in str format to int format for
# calculations.
for element in range(len(hex_list)):
hex_list[element] = int(hex_list[element],16)
# Convert the OID to its standard notation. Sourced from code in other
# languages and adapted for python.
# The first two digits of the OID are calculated differently from the rest.
x = int(hex_list[0] / 40)
y = int(hex_list[0] % 40)
if x > 2:
y += (x-2)*40
x = 2;
OID_str += str(x)+'.'+str(y)
val = 0
for byte in range(1,len(hex_list)):
val = ((val<<7) | ((hex_list[byte] & 0x7F)))
if (hex_list[byte] & 0x80) != 0x80:
OID_str += "."+str(val)
val = 0
# print the OID in dot notation.
print (OID_str)
notation_OID('060D2B0621040BA946964812D10905')
希望这能帮上忙。cHEErs!
https://stackoverflow.com/questions/49653398
复制相似问题