教学服务系统

 找回密码
 立即注册
搜索
查看: 649|回复: 3

信息计算2019级2班32号王诗琪

[复制链接]

8

主题

16

帖子

114

积分

注册会员

Rank: 2

积分
114
发表于 2022-4-22 14:48:19 | 显示全部楼层 |阅读模式
4.19

4.22


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-4-29 18:59:06 | 显示全部楼层
04.29

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-2 19:16:55 | 显示全部楼层
大数版本的RSA
环境:PyCharm3.7.9
生成素数:
  1. import random
  2. from random import randint


  3. def proBin(w):  # w是产生位数,生成伪素数
  4.     list = []
  5.     list.append('1')  
  6.     for _ in range(w - 2):
  7.         c = random.choice(['0', '1'])
  8.         list.append(c)
  9.     list.append('1')
  10.     res = int(''.join(list), 2)
  11.     return res


  12. # 幂模运算
  13. def X_n_mod_P(base, exponent, n):
  14.     bin_array = bin(exponent)[2:][::-1]
  15.     r = len(bin_array)
  16.     base_array = []

  17.     pre_base = base
  18.     base_array.append(pre_base)

  19.     for _ in range(r - 1):
  20.         next_base = (pre_base * pre_base) % n
  21.         base_array.append(next_base)
  22.         pre_base = next_base

  23.     a_w_b = __multi(base_array, bin_array, n)
  24.     return a_w_b % n


  25. def __multi(array, bin_array, n):
  26.     result = 1
  27.     for index in range(len(array)):
  28.         a = array[index]
  29.         if not int(bin_array[index]):
  30.             continue
  31.         result *= a
  32.         result = result % n
  33.     return result


  34. def MillerRabin(a, p):
  35.     if X_n_mod_P(a, p - 1, p) == 1:
  36.         u = (p - 1) >> 1
  37.         while (u & 1) == 0:
  38.             t = X_n_mod_P(a, u, p)
  39.             if t == 1:
  40.                 u = u >> 1
  41.             else:
  42.                 if t == p - 1:
  43.                     return True
  44.                 else:
  45.                     return False
  46.         else:
  47.             t = X_n_mod_P(a, u, p)
  48.             if t == 1 or t == p - 1:
  49.                 return True
  50.             else:
  51.                 return False
  52.     else:
  53.         return False


  54. def testMillerRabin(p, k):
  55.     while k > 0:
  56.         a = randint(2, p - 1)
  57.         if not MillerRabin(a, p):
  58.             return False
  59.         k = k - 1
  60.     return True


  61. def makeprime(w):  # 产生w位素数
  62.     while 1:
  63.         d = proBin(w)
  64.         for i in range(50):  # 伪素数附近50个奇数都没有真素数的话,重新再产生一个伪素数
  65.             u = testMillerRabin(d + 2 * (i), 5)
  66.             if u:
  67.                 b = d + 2 * (i)
  68.                 break
  69.             else:
  70.                 continue
  71.         if u:
  72.             return b
  73.         else:
  74.             continue
复制代码
算法:
  1. from makeprime import makeprime

  2. # 构造字典
  3. dict = {'a': '31', 'b': '32', 'c': '33', 'd': '34', 'e': '35', 'f': '36', 'g': '37',
  4.         'h': '38', 'i': '39', 'j': '10', 'k': '11', 'l': '12', 'm': '13', 'n': '14',
  5.         'o': '15', 'p': '16', 'q': '17', 'r': '18', 's': '19', 't': '20', 'u': '21',
  6.         'v': '22', 'w': '23', 'x': '24', 'y': '25', 'z': '26', '1': '41', '2': '42',
  7.         '3': '43', '4': '44', '5': '45', '6': '46', '7': '47', '8': '48', '9': '49',
  8.         '0': '40', ' ': '50'}


  9. # 字符与数字之间的转换
  10. def transferToNum(str):
  11.     m = ""
  12.     for d in str:
  13.         m += dict[d]
  14.     return m


  15. def transferTostr(num):
  16.     n = ""
  17.     for i in range(0, len(num), 2):
  18.         n += {value: key for key, value in dict.items()}[num[i] + num[i + 1]]
  19.     return n


  20. def ext_gcd(a, b):
  21.     if b == 0:
  22.         x1 = 1
  23.         y1 = 0
  24.         x = x1
  25.         y = y1
  26.         r = a
  27.         return r, x, y
  28.     else:
  29.         r, x1, y1 = ext_gcd(b, a % b)
  30.         x = y1
  31.         y = x1 - a // b * y1
  32.         return r, x, y


  33. '''
  34. 大整数大次幂然后取模
  35. (base ^ exponent) mod n
  36. '''


  37. def exp_mode(base, exponent, n):
  38.     bin_array = bin(exponent)[2:][::-1]
  39.     r = len(bin_array)
  40.     base_array = []

  41.     pre_base = base
  42.     base_array.append(pre_base)

  43.     for _ in range(r - 1):
  44.         next_base = (pre_base * pre_base) % n
  45.         base_array.append(next_base)
  46.         pre_base = next_base

  47.     a_w_b = __multi(base_array, bin_array, n)
  48.     return a_w_b % n


  49. def __multi(array, bin_array, n):
  50.     result = 1
  51.     for index in range(len(array)):
  52.         a = array[index]
  53.         if not int(bin_array[index]):
  54.             continue
  55.         result *= a
  56.         result = result % n
  57.     return result


  58. # 生成公钥私钥,p、q为两个大质数
  59. def gen_key(p, q):
  60.     n = p * q
  61.     fy = (p - 1) * (q - 1)  # 计算与n互质的整数个数 欧拉函数
  62.     e = 65537  # 选取e 65537
  63.     a = e
  64.     b = fy
  65.     x = ext_gcd(a, b)[1]

  66.     if x < 0:
  67.         d = x + fy
  68.     else:
  69.         d = x
  70.     print("公钥:" + "(" + str(n) + "," + str(e) + ")\n私钥:" + "(" + str(n) + "," + str(d) + ")")
  71.     return (n, e), (n, d)


  72. # 加密 m是被加密的信息 加密成为c
  73. def encrypt(m, pubkey):
  74.     n = pubkey[0]
  75.     e = pubkey[1]

  76.     c = exp_mode(m, e, n)
  77.     return c


  78. # 解密 c是密文,解密为明文m
  79. def decrypt(c, selfkey):
  80.     n = selfkey[0]
  81.     d = selfkey[1]

  82.     m = exp_mode(c, d, n)
  83.     return m


  84. if __name__ == "__main__":
  85.     print("1.生成大于64位的质数p和q(以100位为例):")
  86.     p = makeprime(100)
  87.     print("p:", p)
  88.     q = makeprime(100)
  89.     print("q:", q)

  90.     print("2.生成公钥、私钥")
  91.     pubkey, selfkey = gen_key(p, q)

  92.     print("3.输入明文(小写英文与数字的组合)")
  93.     plaintext = str(input())
  94.     m = int(transferToNum(plaintext))

  95.     print("4.用公钥加密信息")
  96.     c = encrypt(m, pubkey)
  97.     print("密文:", c)

  98.     print("5.用私钥解密")
  99.     d = decrypt(c, selfkey)
  100.     print("明文:", transferTostr(str(d)))
复制代码
运行截图:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-5 00:34:29 | 显示全部楼层
5.3观看截图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 13:54 , Processed in 0.017521 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表