RSA算法优化

2019-04-14 20:32发布

RSA算法优化
  1. 大数乘法
  2. 模乗优化
  3. 剩余定理(孙子定理)
  4. RSA加解密
  5. python的RSA计算优化
  #-*- coding: utf-8 -*- ''' /********************************************************************************* *Copyright(C),2000-2013,KK Studio *FileName: rsa *Author: KingKong *Version: 1.0 *Date: 20130709 *Description: //用于主要说明此程序文件完成的主要功能 //与其他模块或函数的接口、输出值、取值范围、 //含义及参数间的控制、顺序、独立及依赖关系 *Others: //其他内容说明 *Function List: //主要函数列表,每条记录应包含函数名及功能简要说明 1.RSA 2.RSA CRT 3.RSA MulMod *History: //修改历史记录列表,每条修改记录应包含修改日期、修改者及修改内容简介 1.20130702: **********************************************************************************/ ''' # sudo apt-get install python-setuptools # sudo easy_install rsa-3.1.1-py2.7.egg # import binascii #print repr(binascii.unhexlify('0123456789abcdef')) EASYKEY = True def CRT_SRC(c, n, p, q, d=None, exp1=None, exp2=None): ''' 剩余定理的基础实现 c是密文 exp1 = d % (p-1) exp2 = d % (q-1) (1)计算d1←d(mod(p-1))与d2←d(mod(q-1)); (2)计算C1←c(modp)与C2←c(modq); (3)计算M1←C1^d1 (modp)与M2←C2^d2(modq); (4)计算B1←q-1(modp)与B2←p-1(modq); (5)计算m←(M1*B1*q+M2*B2*p)(modN) ''' c1 = c % p c2 = c % q if d != None: d1 = d % (p-1) d2 = d % (q-1) elif exp1 != None: d1 = exp1 d2 = exp2 else: return 0 import rsa y1 = rsa.common.inverse(q, p) y2 = rsa.common.inverse(p, q) m1 = pow(c1, d1, p) m2 = pow(c2, d2, q) m = (m1*q*y1 + m2*p*y2)%n return m def CRT_MMRC(c, n, p, q, coef, d=None, exp1=None, exp2=None): ''' 剩余定理的快速实现 c是密文 exp1 = d % (p-1) exp2 = d % (q-1) self.coef = rsa.common.inverse(q, p) (1)计算d1←d(mod(p-1))与d2←d(mod(q-1)); (2)计算C1←c(mod p)与C2←c(mod q); (3)计算M1←C1^d1 (modp)与M2←C2^d2(modq); (4)计算B←p^-1(modp); (5)计算m←M1+[(M2-M1)*B(modq)]*p ''' c1 = c % p c2 = c % q if d != None: d1 = d % (p-1) d2 = d % (q-1) elif exp1 != None: d1 = exp1 d2 = exp2 else: return 0 y1 = coef m1 = pow(c1, d1, p) m2 = pow(c2, d2, q) m = m2 + (((m1-m2)*y1)%p)*q return m def dec2bin(number): ''' 转换数字为二进制字符串 :param number: ''' m = {'0':'0000', '1':'0001', '2':'0010', '3':'0011', '4':'0100', '5':'0101', '6':'0110', '7':'0111', '8':'1000', '9':'1001', 'a':'1010', 'b':'1011', 'c':'1100', 'd':'1101', 'e':'1110', 'f':'1111'} s = hex(number)[2:].rstrip('L') return ''.join(m[x] for x in s).lstrip('0') #print dec2bin(10), len(dec2bin(10)) def MulMod(m, r, e): ''' a^m%r 343^474%2003=1819 ''' c = 1L b = dec2bin(e) length = 0; while(length < (len(b))): c = (c*c)%r; # print c, b[length] if (b[length] == "1"): c = (c * m) % r; length = length + 1; return c def RSA_ENC(m, n, e): ''' RSA加密,处理小数据 :param m: :param n: :param e: ''' return m**e%n def RSA_DEC(c, n, d): ''' RSA解密,处理小数据 :param c: :param n: :param d: ''' return c**d%n def RSA_ENC_Fast(m, n, e): ''' RSA加密,处理大数,加速处理 :param m: :param n: :param e: ''' return pow(m, e, n) def RSA_DEC_Fast(c, n, d): ''' RSA解密,处理大数,加速处理 :param c: :param n: :param d: ''' return pow(c, d, n) def main(): if EASYKEY == True: n = 3727264081 d = 3349121513 e = 65537 p = 65063 q = 57287 exp1 = 55063 exp2 = 10095 coef = 50797 else: n = 133258714669197804455201327242498072620373933399830946281753432589524373262313529490829857553863402092345114025453326547226675345976454214588491707723768296657213731743431331618394950680996499630699923360897031860272219245284778878593279460078556127568327691304405295451439978360703575209901885763486177804307 d = 88839143112798536303467551494998715080249288933220630854502288393016248841542352993886571702575601394896742683635551031484450230650969476392327805149178849037945720743702166302175205762735121467799910708222531056914667451445033725048565810909623712841116051352011118012226070375134490825522121220289982706011 e = 3 p = 11933806723950669295207846073987787705734940703054957716278358174994444687961839258803748173125990183157845108140695431551588508864566689717312651807708143 q = 11166488426677208786957286068049106111694059354243605518996542043073672540329181171939965947432316470456431280477737669321209492974404928986620399396037149 exp1 = 7955871149300446196805230715991858470489960468703305144185572116662963125307892839202498782083993455438563405427130287701059005909711126478208434538472095 exp2 = 7444325617784805857971524045366070741129372902829070345997694695382448360219454114626643964954877646970954186985158446214139661982936619324413599597358099 coef = 9906165481638181059785426924280606820580988396251355030296387570862138753002899617836092623649635665775562393844489153345463178213574659230193241203692517 m = 9999 print '********RSA BEGIN********************************************' print 'message:', m c = RSA_ENC(m, n, e) print 'encrypt:', c r = RSA_DEC_Fast(c, n, d) print 'decrypt:', r print '********RSA END**********************************************' print '********RSA FAST BEGIN***************************************' print 'message:', m c = RSA_ENC_Fast(m, n, e) print 'encrypt:', c r = RSA_DEC_Fast(c, n, d) print 'decrypt:', r print '********RSA FAST END*****************************************' print '********RSA MulMod BEGIN*************************************' print 'message:', m c = MulMod(m, n, e) print 'encrypt:', c r = MulMod(c, n, d) print 'decrypt:', r print '********RSA MulMod END***************************************' print '********RSA CRT BEGIN****************************************' print 'message:', m c = RSA_ENC_Fast(m, n, e) print 'encrypt:', c r = CRT_SRC(c, n, p, q, d) print 'decrypt:', r print '********RSA CRT END******************************************' print '********RSA CRT FAST BEGIN***********************************' print 'message:', m c = RSA_ENC_Fast(m, n, e) print 'encrypt:', c r = CRT_MMRC(c, n, p, q, coef, d, exp1, exp2) print 'decrypt:', r print '********RSA CRT FAST END*************************************' if __name__ == '__main__': main()