RSA的软件算法参考算法
下面介绍基于蒙哥马利的基2的Python算法实现,即大数模乘模乘的实现,易于硬件实现
#A
#p=106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169
#q = 144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209
def bin_trans(a):
b = bin (a)
b1 = list(b)
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b1.pop()
b1.pop()
b2= ''.join(b1) #
return b2
def bin_trans_1(a):
b = bin (a)
b1 = list(b)
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b1.pop()
b1.pop()
b1.reverse()
b2= ''.join(b1) #
return b2
def bin_list(b1):
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b2= ''.join(b1) #
return b2
C = 0
p = ['0','0','1','0','1','1'] #1011
A = bin_list(p) #str
#A=11 #0111
#A_bin = bin_trans(A)
A_bin_1 = bin_trans_1(7)
print('二进制A-11:',A_bin_1)
B = 7
#B = q
B_bin = bin_trans(B)
B_bin_1 = bin_trans_1(B)
print('\n二进制B-2^8:',B_bin_1)
M = 13
M_bin = bin_trans(M)
M_bin_1 = bin_trans_1(M)
print('\n二进制M-13:',M_bin_1)
##########################
for a in A:
if int(a) == 1:
C += B
else:
C = C
C_bin = bin_trans(C) #将C转为二进制字符串
if int(C_bin[0]) == 1:
C += M
C //= 2
else:
C //= 2
C_bin = bin_trans_1(C)
print('C_each',C_bin)
#############################
print('\nC value:',C)
if C >= M:
C1 = C - M
print('\nC1 value:',C1)
else:
C1 = C
print('\nC1 value:',C1)
C_bin = bin_trans_1(C) #str
print('\n二进制C:',C_bin)
C_bin = bin_trans_1(C1) #str
print('\n二进制C1:',C_bin)
print('\n输出 A*B*2^-n mod M ')
版本v2
#A
#p=106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169
#q = 144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209
def bin_trans(a):
b = bin (a)
b1 = list(b)
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b1.pop()
b1.pop()
b2= ''.join(b1) #
return b2
def bin_trans_1(a):
b = bin (a)
b1 = list(b)
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b1.pop()
b1.pop()
b1.reverse()
#b2= ''.join(b1) #
return b1
def bin_list(b1):
b1.reverse() #将二进制数倒置,b[0] 即表示最后一位
b2= ''.join(b1) #
return b2
##########################
def Mon(A,B,M): #A = ['','','',''] B = ['','','','']
A.reverse()
A_bin= ''.join(A) #置反
B.reverse()
B_bin= ''.join(B) #置反
a1= 0
i = 0
for a in A_bin:
a1 += int(a)*2**i
i += 1
print('A:',bin(a1))
b1 = 0
j = 0
for b in B_bin:
b1 += int(b)*2**j
j += 1
print('B:',bin(b1))
C = 0
for a in A_bin:
if int(a) == 1:
C += b1
else:
C = C
C_bin =bin(C)
#print(C_bin)
#print(C_bin[-1])
if int(C_bin[-1]) == 1:
C += M
C //= 2
else:
C //= 2
#C_bin1 = bin_trans_1(C)
print('C_each',bin(C))
print(C)
m = 0
while C > M:
C = C - M
print('C1 value:',C,'\n')
return C
#############################
A = ['1','0','0','0','1']
R = ['1','0','0','0','0','0','0','0','0','0','0']
#R = ['0','0','1','0']
B = ['0','1','1','1','1']
M = 29
C1 = Mon(A,R,M)
R = ['1','0','0','0','0','0','0','0','0','0','0']
C2 = Mon(B,R,M)
print(C1)
C11 = bin_trans_1(C1)
print(C11)
C22 = bin_trans_1(C2)
C3 = Mon(C11,C22,M)
C33= bin_trans_1(C3)
print(C33)
C33 =['0','1','0','1','1']
C4 = Mon(C33,['1'],M)
print('C ',C4,'\n')
#C5 = Mon(A,B,M)
#print(C5)
版本v3
#A
#p=106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169
#q = 144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209
##########################Montgomery algorithm######################################
def Mon(A, B, M, An_bit, Bn_bit):
n_a = '{:0%sb}' % An_bit
A1_bin = n_a.format(A) #二进制A
print('X:',A1_bin)
n_b = '{:0%sb}' % Bn_bit
B1_bin = n_b.format(B) #二进制B
print('Y:',B1_bin)
C = 0
for a in A1_bin[::-1]: #最低位开始
if int(a) == 1:
C += B
else:
C = C
C_bin =bin(C)
if int(C_bin[-1]) == 1:
C += M
C //= 2
else:
C //= 2
#print('C_each',bin(C))
while C > M: #0 < C < M
C = C - M
print('MMM(X,Y,M) value:',C,'\n')
return C
M = 29
#############################
A =19
An_bit =5 #5bit
#R = 2**10
R = 2**10 % M
Rn_bit = 10 #10bit
C1 = Mon(A, R, M, An_bit, Rn_bit) ###C1
#######################################
#R = 2**10
R = 2**10 % M
Rn_bit = 10
B=15
Bn_bit =5
C2 = Mon(B, R, M, Bn_bit, Rn_bit) #C2
############################################
C11n_bit = 5
C22n_bit = 5
C3 = Mon(C1, C2, M, C11n_bit, C22n_bit) #C3
############################################
C33n_bit = 5
C4 = Mon(C3, 1, M, C33n_bit, 5) #C4
############################################
print('A*B mod M = ',C4)