在python中使用PKCS#1v1.5进行签名

时间:2016-11-18 23:39:02

标签: python string rsa signature

我正在为python中的RSA签名编写基本代码。我正在遵循算法"signing using PKCS#1v1.5" given in this link"来编写我的代码。

我遇到的问题是:

  1. str(0xff)将打印为255如果要将其打印为ff该怎么做。
  2. m1=int(EB)这里给出的错误是字符串无法转换为整数
  3. 另请参阅代码中的注释。

    import base64
    import hashlib
    import sys
    
    n = 103923750583675917777452723084902275956322321347251369547198561741725160583083214244873468324950694356892627873520290850868687184677774313738345853731914172873201679734133922293223038318576487055717405740516986030705800665383388489578499651714083347835387576319528274700123545962228546255987411521556243948149
    
    e = 65537
    d = 17565091249452647225549595093023216057619090797007849313736034124434893324058360379487364521346397323516481360986683274410980788633530769996790469838235089550522885790106853124306578513977869050393094033637670437634572856174875038057195517551596745993052717984365235149738925687601660611790816608095661093629
    
    m= '250887726'
    h= hashlib.sha256 (m)
    print h
    
    t= '3031300d060960864801650304020105000420' + str(h)
    print t
    str1=str(00)
    print str1
    str2=str(01)
    print str2
    str3=str(0xff)  # it prints as 255 what to do if want to print it as ff
    print str3
    EB=str1+str2+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str3+str1+str(t)
    print 'EB:', EB
    
    m1=int(EB) # it gives error here that string can't be converted to integer                                      
    
    s= pow (m1, d, n)
    
    print 'signature: ',s
    

1 个答案:

答案 0 :(得分:0)

  

str(0xff)将打印为255,如果要将其打印为ff。

您可以使用格式:

'{:x}'.format(0xff)
  

m1 = int(EB)这里给出的错误是字符串不能转换为整数

使用int()的第二个参数,它显示整数的基数:

int('ff', 16)