为了账号安全,请及时绑定邮箱和手机立即绑定

在 python 中,对象不按照定义的顺序打印

在 python 中,对象不按照定义的顺序打印

眼眸繁星 2023-12-09 15:36:24
我试图按照定义的顺序打印该函数的返回:import numpydef Decrypt_CRT_RSA(c, d, p, q):    """    Decrypts RSA with CRT    :param c: The cipher text you want to decrypt    :param d: The secret key    :param p: A prime number forming half of the secret key    :param q:A prime number forming the other half of the secret key    """    dp = d % (p - 1)    dq = d % (q - 1)    cp = c % p    cq = c % q    m0 = pow(cp, dp, p)    m1 = pow(cq, dq, q)    Msg = CRT([m0, m1], [p, q])[0]    return {        'p part of the cipher text: ' + str(cp),        'q part of the cipher text: ' + str(cq),        'p part of the private key: ' + str(dp),        'q part of the private key: ' + str(dq),        'first half of the message m: ' + str(m0),        'second half of the message m: ' + str(m1),        'the full message (aka m0 + m1): ' + str(Msg)    }c = 64649d = 241187p = 659q = 673print(Decrypt_CRT_RSA(c, d, p, q))但它是这样打印的:{'密文p部分:67', '完整消息(又名m0 + m1):12345', '消息前半部分m: 483', '私钥q部分: 611', '第二部分消息的一半 m: 231', 'p 私钥部分: 359', 'q 密文部分: 41'}正如你所看到的,打印的第二件事是the full message 我最后返回的那件事。我如何让 python 尊重函数定义的返回值?或者甚至更好,以定义的方式返回(也称为新行上的每个条目)
查看完整描述

1 回答

?
潇湘沐

TA贡献1816条经验 获得超6个赞

实际上 return 语句返回一个集合,这里的冒号在字符串内,所以尝试像这样返回 dict:

  return {
          'p part of the cipher text': str(cp),

等等


查看完整回答
反对 回复 2023-12-09
  • 1 回答
  • 0 关注
  • 39 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信