[关闭]
@Lin-- 2019-09-29T09:43:28.000000Z 字数 2138 阅读 440

ComSec作业四:求AES中的S-Box

ComSec


题目:根据课本描述,编程实现:1、输出AES算法中的S-Box。2、实现具体的字节代换功能,输入一个8比特数,输出其对应的S-Box的值。
第二小题中,本人利用课本例值0x95,结果返回2A。

  1. '''
  2. # File : AES_S-Box.py
  3. # Author : Hongpei Lin
  4. # Date : 20190925
  5. # Purpose : In algorithm AES
  6. # build S-Box in SubBytes
  7. '''
  8. #multilpy in GF(2^8)
  9. def mul(a,b):
  10. r=0
  11. while b:
  12. if b%2:
  13. r=r^a #add operation : XOR
  14. b=b>>1
  15. if a&int('10000000',2)==0: #first bit's value = 0
  16. a=a<<1
  17. else: #first bit's value = 1
  18. a=a<<1
  19. a=a^283
  20. return r
  21. #compute the max index number which < 2^count
  22. #return count, from 0
  23. def highest_bit(n):
  24. count = 0
  25. while n:
  26. count+=1
  27. n=n>>1
  28. return count-1
  29. #division about polymerization
  30. #return quotient and remainder
  31. def div(a,b):
  32. if a==b:
  33. return 1,0
  34. if a<b:
  35. return 0,a
  36. a_bit = highest_bit(a)
  37. b_bit = highest_bit(b)
  38. result = 0
  39. while not a_bit<b_bit:
  40. move=a_bit-b_bit
  41. temp=b<<move
  42. result=result+(1<<move)
  43. a=a^temp
  44. a_bit=highest_bit(a)
  45. return result,a
  46. #compute the inverse about a', where a*a'=1(mod m)
  47. #the algorithrm likes EGCD
  48. def inverse(a,m):
  49. r0,s0,r1,s1=1,0,0,1
  50. while m>0:
  51. t=m
  52. q,m=div(a,m)#q=a//m,m=a mod m
  53. a=t#a=m
  54. r0,r1=r1,r0^mul(q,r1)#sub operation:XOR
  55. s0,s1=s1,s0^mul(q,s1)
  56. return r0 #a'
  57. T=[]
  58. T_v=143
  59. #build the matrix to multiply b0-b7 in step 3
  60. for i in range(8):
  61. T.append(T_v)
  62. if T_v&int('00000001',2):
  63. T_v=(T_v>>1)^int('10000000',2)
  64. else:
  65. T_v=T_v>>1
  66. S0=[[0]*16 for i in range(16)]#inital S_Box in step 1
  67. S1=[[0]*16 for i in range(16)]#S_Box
  68. for i in range(16):
  69. for j in range(16):
  70. S0[i][j]=(i<<4)+j
  71. S1[i][j]=inverse(S0[i][j],283)
  72. #In order to multiply matrix T,let every bit in a byte reverse.
  73. Bit=list('{:08b}'.format(S1[i][j]))
  74. Bit.reverse()
  75. Bit_s=""
  76. for k in Bit:
  77. Bit_s=Bit_s+str(k)
  78. Bit=int(Bit_s,2)
  79. #T */& b0-b7
  80. T_result=[]
  81. for l in T:
  82. And=l&Bit
  83. And_list=list('{:08b}'.format(And))
  84. And_reslut=int(And_list[0],2)
  85. for m in And_list[1:]:
  86. And_reslut=And_reslut^int(m,2)
  87. T_result.append(And_reslut)
  88. T_result_s=""
  89. for n in T_result:
  90. T_result_s=T_result_s+str(n)
  91. #get the reslut +/XOR c/63's reverse bits
  92. S_temp=int(T_result_s,2)^int('11000110',2)
  93. S_temp=list('{:08b}'.format(S_temp))
  94. S_temp.reverse()#reverse again, get the final answer
  95. S_temp_s=""
  96. for o in S_temp:
  97. S_temp_s=S_temp_s+str(o)
  98. S1[i][j]=hex(int(S_temp_s,2))
  99. '''
  100. #print the S-Box
  101. for i in range(16):
  102. for j in range(16):
  103. print(S1[i][j][2:],end=',')
  104. print("")
  105. '''
  106. def SubByte(S_BOX,n):
  107. high_four=(n&int('11110000',2))>>4
  108. low_four=(n&int('00001111',2))
  109. return S_BOX[high_four][low_four]
  110. print(SubByte(S1,int('95',16)))
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注