当前位置:   article > 正文

国密算法 ZUC流密码 祖冲之密码 python代码完整实现_祖冲之密码算法python

祖冲之密码算法python

此前发布了包含SM2公钥密码、SM3杂凑算法和SM4分组密码的国密算法完整工具包,其实还是有些不完整,因为没有流密码。今天把我们国家自己的流密码——ZUC祖冲之密码算法补充上。介绍其他国密算法的链接如下:

SM2:国密算法 SM2 公钥加密 非对称加密 数字签名 密钥协商 python实现完整代码_qq_43339242的博客-CSDN博客_python国密sm2

SM3:国密算法 SM3 消息摘要 杂凑算法 哈希函数 散列函数 python实现完整代码_qq_43339242的博客-CSDN博客

SM4国密算法 SM4 对称加密 分组密码 python实现完整代码_qq_43339242的博客-CSDN博客_sm4代码python

对上述几个算法和实现不了解的,建议点进去看看。下面这篇文章是对上述的汇总:

国密算法 SM2公钥密码 SM3杂凑算法 SM4分组密码 python代码完整实现

所有代码托管在码云:hggm - 国密算法 SM2 SM3 SM4 python实现完整代码: 国密算法 SM2公钥密码 SM3杂凑算法 SM4分组密码 python代码完整实现 效率高于所有公开的python国密算法库 (gitee.com)

废话多了。上ZUC的代码:

  1. from array import array
  2. _ZUC_D = [0x44D700, 0x26BC00, 0x626B00, 0x135E00, 0x578900, 0x35E200, 0x713500, 0x09AF00,
  3. 0x4D7800, 0x2F1300, 0x6BC400, 0x1AF100, 0x5E2600, 0x3C4D00, 0x789A00, 0x47AC00]
  4. _ZUC_S0 = bytes([
  5. 0x3E, 0x72, 0x5B, 0x47, 0xCA, 0xE0, 0x00, 0x33, 0x04, 0xD1, 0x54, 0x98, 0x09, 0xB9, 0x6D, 0xCB,
  6. 0x7B, 0x1B, 0xF9, 0x32, 0xAF, 0x9D, 0x6A, 0xA5, 0xB8, 0x2D, 0xFC, 0x1D, 0x08, 0x53, 0x03, 0x90,
  7. 0x4D, 0x4E, 0x84, 0x99, 0xE4, 0xCE, 0xD9, 0x91, 0xDD, 0xB6, 0x85, 0x48, 0x8B, 0x29, 0x6E, 0xAC,
  8. 0xCD, 0xC1, 0xF8, 0x1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, 0xE9,
  31. 0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89,
  32. 0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4,
  33. 0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE,
  34. 0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21,
  35. 0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34,
  36. 0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28,
  37. 0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2])
  38. _ZUC_S = array('H')
  39. for byte1 in _ZUC_S0: # 构造两字节的S盒变换表
  40. byte1 <<= 8
  41. _ZUC_S.extend(byte1 | byte2 for byte2 in _ZUC_S1)
  42. class ZUC:
  43. def __init__(self, key, iv):
  44. lfsr = self.lfsr = [_ZUC_D[i] | key[i] << 23 | iv[i] for i in range(16)]
  45. self.r = 0, 0
  46. for i in range(32):
  47. c = (((lfsr[15] << 1 & 0xFFFF0000 | lfsr[14] & 0xFFFF) ^ self.r[0]) + self.r[1] >> 1 & 0x7FFFFFFF)\
  48. + self.lfsr_next()
  49. self.lfsr_shift((c & 0x7FFFFFFF) + (c >> 31))
  50. def lfsr_next(self):
  51. lfsr, r = self.lfsr, self.r
  52. W1, W2 = r[0] + (lfsr[11] << 16 | lfsr[9] >> 15) & 0xFFFFFFFF, r[1] ^ ((lfsr[7] & 0xFFFF) << 16 | lfsr[5] >> 15)
  53. x, y = (W1 & 0xFFFF) << 16 | W2 >> 16, (W2 & 0xFFFF) << 16 | W1 >> 16 # L线性变换
  54. u = (x ^ (x << 2 | x >> 30) ^ (x << 10 | x >> 22) ^ (x << 18 | x >> 14) ^ (x << 24 | x >> 8)) & 0xFFFFFFFF
  55. v = (y ^ (y << 8 | y >> 24) ^ (y << 14 | y >> 18) ^ (y << 22 | y >> 10) ^ (y << 30 | y >> 2)) & 0xFFFFFFFF
  56. self.r = _ZUC_S[u >> 16] << 16 | _ZUC_S[u & 0xFFFF], _ZUC_S[v >> 16] << 16 | _ZUC_S[v & 0xFFFF] # S盒变换
  57. v_0, v_4, v_10, v_13, v_15 = lfsr[0], lfsr[4], lfsr[10], lfsr[13], lfsr[15]
  58. c = v_0 + ((v_0 << 8 | v_0 >> 23) & 0x7FFFFFFF)
  59. c = (c & 0x7FFFFFFF) + (c >> 31) + ((v_4 << 20 | v_4 >> 11) & 0x7FFFFFFF)
  60. c = (c & 0x7FFFFFFF) + (c >> 31) + ((v_10 << 21 | v_10 >> 10) & 0x7FFFFFFF)
  61. c = (c & 0x7FFFFFFF) + (c >> 31) + ((v_13 << 17 | v_13 >> 14) & 0x7FFFFFFF)
  62. c = (c & 0x7FFFFFFF) + (c >> 31) + ((v_15 << 15 | v_15 >> 16) & 0x7FFFFFFF)
  63. return (c & 0x7FFFFFFF) + (c >> 31)
  64. def lfsr_shift(self, x):
  65. self.lfsr.pop(0)
  66. self.lfsr.append(x)
  67. def enc(self, data):
  68. res, lfsr = [], self.lfsr
  69. for i in data:
  70. self.lfsr_shift(self.lfsr_next())
  71. res.append(i ^ ((lfsr[15] << 1 & 0xFFFF0000 | lfsr[14] & 0xFFFF) ^ self.r[0]) + self.r[1] & 0xFFFFFFFF
  72. ^ ((lfsr[2] & 0xFFFF) << 16 | lfsr[0] >> 15))
  73. self.lfsr_shift(self.lfsr_next())
  74. return res

这是未加速的版本,numba加速版本和测试代码请到码云下载。本代码的使用方法请参考测试代码。以下是从国密算法工具包的完整测试代码中截取的一部分和ZUC相关的:

  1. import os
  2. import random
  3. import time
  4. import numpy as np
  5. from Crypto.Cipher import ARC4
  6. from pysmx.ZUC import ZUC as ZUC_pysmx
  7. from hggm.ZUC import ZUC as ZUC_my_fast
  8. from hggm.slow.ZUC import ZUC as ZUC_my
  9. def zuc_compare_test():
  10. # 随机生成128位的密钥和初始向量
  11. key, iv = os.urandom(16), os.urandom(16)
  12. test_data = os.urandom(128)
  13. test_num = 50 # 测试次数
  14. # 随机生成消息
  15. short_data = [os.urandom(random.randint(16, 48)) for _ in range(test_num)] # 短消息列表
  16. long_data = [os.urandom(random.randint(1000, 2000)) for _ in range(test_num)] # 长消息列表
  17. enc_data_arc4 = [b''] * test_num
  18. enc_data_pysmx = [b''] * test_num
  19. enc_data_my = [b''] * test_num
  20. enc_data_my_fast = [b''] * test_num
  21. enc_data_arc4_2 = [b''] * test_num
  22. enc_data_pysmx2 = [b''] * test_num
  23. enc_data_my2 = [b''] * test_num
  24. enc_data_my_fast2 = [b''] * test_num
  25. dec_data = [b''] * test_num
  26. dec_data2 = [b''] * test_num
  27. print('\n———————————————————————————————————————————流密码测试———————————————————————————————————————————')
  28. print('前两次加密消息长度:%dB\n后两项为连续加密——短消息平均长度:%.1fB 长消息平均长度:%.1fB 测试次数:%d' %
  29. (len(test_data), np.mean(list(map(len, short_data))), np.mean(list(map(len, long_data))), test_num))
  30. print('算法库名\t\t\t首次初始化(μs)\t再次初始化(μs)\t首次加密(μs)\t再次加密(μs)\t处理短消息(ms)\t处理长消息(ms)')
  31. # Crypto - ARC4
  32. time_1 = time.perf_counter()
  33. arc4 = ARC4.new(key)
  34. time_2 = time.perf_counter()
  35. arc4_2 = ARC4.new(key)
  36. time_3 = time.perf_counter()
  37. arc4.encrypt(test_data)
  38. time_4 = time.perf_counter()
  39. arc4_2.encrypt(test_data)
  40. time_5 = time.perf_counter()
  41. for i in range(test_num):
  42. enc_data_arc4[i] = arc4.encrypt(short_data[i]) # 加密短消息
  43. time_6 = time.perf_counter()
  44. for i in range(test_num):
  45. enc_data_arc4_2[i] = arc4.encrypt(long_data[i]) # 加密长消息
  46. time_7 = time.perf_counter()
  47. print('Crypto-ARC4\t\t%.2f\t\t\t%.2f\t\t\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f' %
  48. ((time_2 - time_1) * 1000000, (time_3 - time_2) * 1000000, (time_4 - time_3) * 1000000,
  49. (time_5 - time_4) * 1000000, (time_6 - time_5) * 1000, (time_7 - time_6) * 1000))
  50. # pysmx - ZUC
  51. time_1 = time.perf_counter()
  52. zuc_smx = ZUC_pysmx(key, iv)
  53. time_2 = time.perf_counter()
  54. zuc_smx2 = ZUC_pysmx(key, iv)
  55. time_3 = time.perf_counter()
  56. zuc_smx.zuc_encrypt(test_data)
  57. time_4 = time.perf_counter()
  58. zuc_smx2.zuc_encrypt(test_data)
  59. time_5 = time.perf_counter()
  60. for i in range(test_num):
  61. enc_data_pysmx[i] = zuc_smx.zuc_encrypt(short_data[i]) # 加密短消息
  62. time_6 = time.perf_counter()
  63. for i in range(test_num):
  64. enc_data_pysmx2[i] = zuc_smx.zuc_encrypt(long_data[i]) # 加密长消息
  65. time_7 = time.perf_counter()
  66. print('pysmx-ZUC\t\t%.2f\t\t\t%.2f\t\t\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f' %
  67. ((time_2 - time_1) * 1000000, (time_3 - time_2) * 1000000, (time_4 - time_3) * 1000000,
  68. (time_5 - time_4) * 1000000, (time_6 - time_5) * 1000, (time_7 - time_6) * 1000))
  69. time_aim = time_7 - time_5
  70. # my - ZUC
  71. time_1 = time.perf_counter()
  72. zuc_my = ZUC_my(key, iv)
  73. time_2 = time.perf_counter()
  74. zuc_my2 = ZUC_my(key, iv)
  75. time_3 = time.perf_counter()
  76. zuc_my.enc(test_data)
  77. time_4 = time.perf_counter()
  78. zuc_my2.enc(test_data)
  79. time_5 = time.perf_counter()
  80. for i in range(test_num):
  81. enc_data_my[i] = zuc_my.enc(short_data[i]) # 加密短消息
  82. time_6 = time.perf_counter()
  83. for i in range(test_num):
  84. enc_data_my2[i] = zuc_my.enc(long_data[i]) # 加密长消息
  85. time_7 = time.perf_counter()
  86. print('my-ZUC\t\t\t%.2f\t\t\t%.2f\t\t\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f' %
  87. ((time_2 - time_1) * 1000000, (time_3 - time_2) * 1000000, (time_4 - time_3) * 1000000,
  88. (time_5 - time_4) * 1000000, (time_6 - time_5) * 1000, (time_7 - time_6) * 1000))
  89. time_my = time_7 - time_5
  90. # my - ZUC(fast)
  91. time_1 = time.perf_counter()
  92. zuc_my_fast = ZUC_my_fast(key, iv)
  93. time_2 = time.perf_counter()
  94. zuc_my_fast2 = ZUC_my_fast(key, iv)
  95. time_3 = time.perf_counter()
  96. zuc_my_fast.enc(test_data)
  97. time_4 = time.perf_counter()
  98. zuc_my_fast2.enc(test_data)
  99. time_5 = time.perf_counter()
  100. for i in range(test_num):
  101. enc_data_my_fast[i] = zuc_my_fast.enc(short_data[i]) # 加密短消息
  102. time_6 = time.perf_counter()
  103. for i in range(test_num):
  104. enc_data_my_fast2[i] = zuc_my_fast.enc(long_data[i]) # 加密长消息
  105. time_7 = time.perf_counter()
  106. print('my-ZUC(fast)\t%.2f\t\t\t%.2f\t\t\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f' %
  107. ((time_2 - time_1) * 1000000, (time_3 - time_2) * 1000000, (time_4 - time_3) * 1000000,
  108. (time_5 - time_4) * 1000000, (time_6 - time_5) * 1000, (time_7 - time_6) * 1000))
  109. time_my_fast = time_7 - time_5
  110. print('连续加密时,未加速时总耗时为pysmx的%.2f%%' % (time_my / time_aim * 100))
  111. print('加速以后总耗时为pysmx的%.2f%%、未加速的%.2f%%' % (time_my_fast / time_aim * 100, time_my_fast / time_my * 100))
  112. assert enc_data_pysmx == enc_data_my == list(map(list, enc_data_my_fast)) # 短消息加密生成的列表
  113. assert enc_data_pysmx2 == enc_data_my2 == list(map(list, enc_data_my_fast2)) # 长消息加密生成的列表
  114. # ARC4解密
  115. for i in range(test_num): # 解密短消息
  116. dec_data[i] = arc4_2.decrypt(enc_data_arc4[i])
  117. for i in range(test_num): # 解密长消息
  118. dec_data2[i] = arc4_2.decrypt(enc_data_arc4_2[i])
  119. assert dec_data == short_data and dec_data2 == long_data
  120. # ZUC解密
  121. for i in range(test_num): # 解密短消息
  122. dec_data[i] = bytes(zuc_my2.enc(enc_data_my[i]))
  123. dec_data2[i] = zuc_my_fast2.dec(enc_data_my_fast[i]).astype(np.uint8).tobytes()
  124. assert dec_data == dec_data2 == short_data
  125. for i in range(test_num): # 解密长消息(因为是流密码,从始至终加密和解密的顺序必须完全一致)
  126. dec_data[i] = bytes(zuc_my2.enc(enc_data_my2[i]))
  127. dec_data2[i] = zuc_my_fast2.dec(enc_data_my_fast2[i]).astype(np.uint8).tobytes()
  128. assert dec_data == dec_data2 == long_data
  129. if __name__ == "__main__":
  130. zuc_compare_test()

测试结果如下图:

效果还行,未加速版本的耗时约为pysmx库的60%,加速以后耗时约为其1%。未加速时能比pysmx库快一点,是因为采用了双字节S盒表,减少了函数调用和冗余计算(代码比pysmx库的实现短了很多,可能对可读性有些影响)。

需要注意的是,解密流密码密文时,必须保证解密顺序与加密时完全对应。本代码的解密方法可参考上面测试代码的最后一段,未加速版本统一用"enc"方法实现加密和解密,加速版本用"dec"方法实现解密。加密和解密后,我没有去转输出类型,未加速版本输出的是list,加速版本输出的是np数组,个人认为这个留给使用者自己转会更好,免得画蛇添足(上面解密的代码把输出类型转化为bytes后再与原始数据比较)

为了更好地进行对比,引入了Crypto(PyCryptodome)库的流密码算法ARC4(好像是RC4算法的非官方实现),可见ARC4连续加密长消息时性能是很强劲的(国外成熟密码算法库会调用编译好的C语言动态链接库),但ZUC的复杂度和安全性高于RC4。如果运行测试代码时Crypto库的部分报类似“ARC4_stream_init' not found in library”的错误,可以尝试一下老版本Crypto库,我用的3.8版本是没问题的,在cmd命令行中输入下面两行即可换版本:

pip uninstall pycryptodome

pip install pycryptodome==3.8.0

网络安全,人人有责!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/483277
推荐阅读
相关标签
  

闽ICP备14008679号