RSA-1024

def exp_mode(base, exponent, n):
  bin_array = bin(exponent)[2:][::-1]
  r = len(bin_array)
  base_array = []
  pre_base = base
  base_array.append(pre_base)
  for _ in range(r - 1):
    next_base = (pre_base * pre_base) % n
    base_array.append(next_base)
    pre_base = next_base
  a_w_b = __multi(base_array, bin_array)
  return a_w_b % n
def __multi(array, bin_array):
  result = 1
  for index in range(len(array)):
    a = array[index]
    if not int(bin_array[index]):
      continue
    result *= a
  return result


 
# 加密 m是被加密的信息 加密成为c
def encrypt(m, pubkey):
  n = pubkey[0]
  e = pubkey[1]
  c = exp_mode(m, e, n)
  return c
# 解密 c是密文,解密为明文m
def decrypt(c, selfkey):
  n = selfkey[0]
  d = selfkey[1]
  m = exp_mode(c, d, n)
  return m
if __name__ == "__main__":
  '''公钥私钥中用到的两个大质数p,q'''
  #p = 106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169
  #q = 144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209
  '''生成公钥私钥'''
  #pubkey, selfkey = gen_key(p, q)
  '''需要被加密的信息转化成数字,长度小于秘钥n的长度,如果信息长度大于n的长度,那么分段进行加密,分段解密即可。'''
 # m = 1356205320457610288745198967657644166379972189839804389074591563666634066646564410685955217825048626066190866536592405966964024022236587593447122392540038493893121248948780525117822889230574978651418075403357439692743398250207060920929117606033490559159560987768768324823011579283223392964454439904542675637683985296529882973798752471233683249209762843835985174607047556306705224118165162905676610067022517682197138138621344578050034245933990790845007906416093198845798901781830868021761765904777531676765131379495584915533823288125255520904108500256867069512326595285549579378834222350197662163243932424184772115345
  '''信息加密'''
  pubkey = (140218042035580172420364932248555007367482790664685055083394324080147243394100961496086452391381422732357561718982205902666977156351465372055783527322300206627171949426523932629113893356603544562923187548724517705432927770612313751657683602943013823818882300883355701485072067598085910265646761088731994650173,25705)
  selfkey = (140218042035580172420364932248555007367482790664685055083394324080147243394100961496086452391381422732357561718982205902666977156351465372055783527322300206627171949426523932629113893356603544562923187548724517705432927770612313751657683602943013823818882300883355701485072067598085910265646761088731994650173,23663328782351557594224589616581662009731194160801547129031883985982444732293716046295391187465964279827547276286512709814018552976178050339544397138541480740987411542113772600583939826494343243467033728202922302297834924035292893935329570297407718000372210731789693299686678289602502199271734009047980338137)
  m = 0x516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73516f726f73454541083D7821567398303A39C6C17432EDE0AA9CB6ED
  c = encrypt(m, selfkey)
  print(hex(c))
  
  d = decrypt(c, pubkey)
  print(hex(d))


  with open('hexEncryptedMessage.bin', 'w+')as fp:
    
        fp.write(hex(c))



  fp.close()
 
  print('done')  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汽车电子开发攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值