如何修复python中int不可编辑的问题?

2024-10-05 14:21:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我用十六进制输入比桑。例如,e=5、n=221和pesan=1e。然后pesan将转换为十进制并解密。结果(十进制)将再次转换为十六进制。但我犯了个错误

TypeError: 'int' object is not iterable.

如何解决?这是密码

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    plainDecimal=[]
    for i in m:
        cipherElement=mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = hex_to_decimal(pesan) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

Tags: tomodnumberforinputreturndefint
2条回答

我已经解决了这个问题。这是密码

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    m_str = str(m)
    decimalList =[int(m_str)]
    plainDecimal=[]
    for i in decimalList:
        cipherElement = mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = str(hex_to_decimal(pesan)) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

在您给出的代码中,decimalp值是int,我现在使用str()函数将decimalp值更改为string。代码现在应该可以完美地工作了。下面给出了更正后的代码

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    plainDecimal=[]
    for i in m:
        cipherElement=mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = str(hex_to_decimal(pesan)) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

相关问题 更多 >