本文整理汇总了Python中BitVector.gf_multiply_modular方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.gf_multiply_modular方法的具体用法?Python BitVector.gf_multiply_modular怎么用?Python BitVector.gf_multiply_modular使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.gf_multiply_modular方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mixColumns
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def mixColumns(shift_block):
sblock=shift_block
mixed_block=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
two=BitVector(intVal=2, size=8)
three=BitVector(intVal=3, size=8)
modulus=BitVector(bitstring='100011011')#AES modulus
n=8
for i in range(0,4):#conversion
for j in range(0,4):
#print ord(sblock[i][j])
sblock[i][j]=BitVector(intVal=ord(sblock[i][j]), size=8)
for j in range(0, 4):
mixed_block[0][j]=(two.gf_multiply_modular(sblock[0][j], modulus, n))^(three.gf_multiply_modular(sblock[1][j], modulus, n))^(sblock[2][j])^(sblock[3][j])
print int(mixed_block[0][j])
for j in range(0, 4):
mixed_block[1][j]=(sblock[0][j])^(two.gf_multiply_modular(sblock[1][j], modulus, n))^(three.gf_multiply_modular(sblock[2][j], modulus, n))^(sblock[3][j])
for j in range(0, 4):
mixed_block[2][j]=sblock[0][j]^sblock[1][j]^(two.gf_multiply_modular(sblock[2][j], modulus, n))^(three.gf_multiply_modular(sblock[3][j], modulus, n))
for j in range(0, 4):
mixed_block[3][j]=(three.gf_multiply_modular(sblock[0][j], modulus, n))^sblock[1][j]^sblock[2][j]^(two.gf_multiply_modular(sblock[3][j], modulus, n))
for i in range(0, 4): #conversion back to char
for j in range(0, 4):
#print mixed_block[i][j]
mixed_block[i][j]=chr(mixed_block[i][j])
return mixed_block
示例2: inverseMixColumns
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def inverseMixColumns(shift_block):
sblock=shift_block
invmixed_block=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
EE=BitVector(intVal=14, size=8)
BB=BitVector(intVal=11, size=8)
DD=BitVector(intVal=13, size=8)
nine=BitVector(intVal=9, size=8)
modulus=BitVector(bitstring='100011011')#AES modulus
n=8
for i in range(0,4):#conversion
for j in range(0,4):
#print ord(sblock[i][j])
sblock[i][j]=BitVector(intVal=ord(sblock[i][j]), size=8)
for j in range(0, 4):
invmixed_block[0][j]=(EE.gf_multiply_modular(sblock[0][j], modulus, n))^(BB.gf_multiply_modular(sblock[1][j], modulus, n))^(DD.gf_multiply_modular(sblock[2][j], modulus, n))^(nine.gf_multiply_modular(sblock[3][j], modulus, n))
print int(invmixed_block[0][j])
for j in range(0, 4):
invmixed_block[1][j]=(nine.gf_multiply_modular(sblock[0][j], modulus, n))^(EE.gf_multiply_modular(sblock[1][j], modulus, n))^(BB.gf_multiply_modular(sblock[2][j], modulus, n))^(DD.gf_multiply_modular(sblock[3][j], modulus, n))
for j in range(0, 4):
invmixed_block[2][j]=(DD.gf_multiply_modular(sblock[0][j], modulus, n))^(nine.gf_multiply_modular(sblock[1][j], modulus, n))^(EE.gf_multiply_modular(sblock[2][j], modulus, n))^(BB.gf_multiply_modular(sblock[3][j], modulus, n))
for j in range(0, 4):
invmixed_block[3][j]=(BB.gf_multiply_modular(sblock[0][j], modulus, n))^(DD.gf_multiply_modular(sblock[1][j], modulus, n))^(nine.gf_multiply_modular(sblock[2][j], modulus, n))^(EE.gf_multiply_modular(sblock[3][j], modulus, n))
for i in range(0, 4): #conversion back to char
for j in range(0, 4):
#print mixed_block[i][j]
invmixed_block[i][j]=chr(invmixed_block[i][j])
return invmixed_block
示例3: mix_columns
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def mix_columns(state_array):
oper_m = [[2,3,1,1],
[1,2,3,1],
[1,1,2,3],
[3,1,1,2]]
mix_matrix = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
m = state_array[0][0:8]
print (m.get_hex_string_from_bitvector())
y = m.int_val()
print (type(y))
print (y)
print ("mix_columns begin$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
state_matrix = []
for i in range(4):
rowlis = []
for j in range(4):
#print (state_array[i][j*8:j*8+8].get_hex_string_from_bitvector()),
#print (state_array[i][j*8:j*8+8].int_val())
rowlis.append(state_array[i][j*8:j*8+8].int_val())
state_matrix.append(rowlis)
print (state_matrix)
for i in range(len(state_matrix)):
for j in range(len(state_matrix[i])):
print (state_matrix[i][j]),
modulus = BitVector(bitstring="100011011")
for i in range(len(oper_m)):
for j in range(len(state_matrix[0])):
bb = BitVector(intVal = 0, size=8)
for k in range(len(state_matrix)):
'''
ss = BitVector(intVal=oper_m[i][k],size=8)^BitVector(intVal=state_matrix[k][j],size=8)
bb = bb ^ ss
'''
a = BitVector(intVal=oper_m[i][k],size=8)
b = BitVector(intVal=state_matrix[k][j],size=8)
ss = a.gf_multiply_modular(b,modulus,8)
bb = bb ^ ss
#mix_matrix[i][j] += oper_m[i][k]*state_matrix[k][j]
mix_matrix[i][j] = bb
print ("mix_matrix******************")
mix_colums_bv = BitVector(size=0)
for j in range(4):
for i in range(4):
#print (mix_matrix[i][j])
mix_colums_bv += mix_matrix[i][j]
print (mix_colums_bv.get_hex_string_from_bitvector())
return mix_colums_bv
示例4: inv_mix_columns
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def inv_mix_columns(ad_rk):
oper_m = [[0x0e,0x0b,0x0d,0x09],
[0x09,0x0e,0x0b,0x0d],
[0x0d,0x09,0x0e,0x0b],
[0x0b,0x0d,0x09,0x0e]]
mix_matrix = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
zz = BitVector(intVal=0xe,size=8)
print ("zz")
print (zz)
matrix = []
for i in range(4):
rowlis = []
for j in range(4):
rowlis.append(ad_rk[32*j+8*i:32*j+8*i+8])
matrix.append(rowlis)
for i in range(4):
for j in range(4):
print (matrix[i][j].get_hex_string_from_bitvector()),
print ("")
modulus = BitVector(bitstring="100011011")
for i in range(len(oper_m)):
for j in range(len(matrix[0])):
bb = BitVector(intVal = 0, size=8)
for k in range(len(matrix)):
a = BitVector(intVal=oper_m[i][k],size=8)
b = matrix[k][j]
ss = a.gf_multiply_modular(b,modulus,8)
bb = bb ^ ss
#mix_matrix[i][j] += oper_m[i][k]*state_matrix[k][j]
mix_matrix[i][j] = bb
mix_colums_bv = BitVector(size=0)
for j in range(4):
for i in range(4):
#print (mix_matrix[i][j])
mix_colums_bv += mix_matrix[i][j]
print (mix_colums_bv.get_hex_string_from_bitvector())
return mix_colums_bv
示例5: decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def decrypt(key_list):
fout = open(fout2, 'wb+')
bv = BitVector( filename = fout1 )
subbyte = decryptable
while (bv.more_to_read):
bitvec = bv.read_bits_from_file( 128 )
if len(bitvec) != 128:
bitvec.pad_from_right(128 - len(bitvec))
if len(bitvec) == 128:
bitvec = bitvec ^ key_list[14]
for i in range(14):
#shift row
for m in range(3):
for fan in range(m+1):
temp = BitVector( bitstring=str(bitvec[(32*3 + 8*(m+1)):(32*3+(8*(m+2)))]) )
for j in range(3):
j = 3 - j
bitvec[(32*j+8*(m+1)):(32*j+8*(m+2))] = bitvec[(32*(j-1)+8*(m+1)):(32*(j-1)+8*(m+2))]
bitvec[(8*(m+1)):(8*(m+2))] = temp
#look up the table
for j in range(16):
#print subbyte[int(bitvec[8*j:(8*j + 4)])][int(bitvec[(8*j + 4):(8*j + 8)])]
bitvec[8*j:(8*j+8)] = subbyte[int(bitvec[8*j:(8*j + 4)])][int(bitvec[(8*j + 4):(8*j + 8)])]
#add with round key
bitvec = bitvec ^ key_list[13 - i]
#mix columns
if i != 13:
tempout = BitVector(size=128)
for k in range(4):
for j in range(4):
tempbyte = BitVector( size=8 )
for l in range(4):
tempmultiply2 = bitvec[(32*j+l*8):(32*j+(l+1)*8)]
tempmultiply1 = BitVector( intVal= colmixD[k][l], size=8)
tempbyte = tempbyte ^ (tempmultiply1.gf_multiply_modular(tempmultiply2, modulus, 8))
tempout[(32*j+8*k):(32*j+8*(k+1))] = BitVector(bitstring=str(tempbyte))
bitvec = BitVector(bitstring=str(tempout))
fout.write(bitvec.getTextFromBitVector())
fout.close()
示例6: g_function
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def g_function(word, round_num, s_box):
# one-byte left circular rotation
for i in range(8):
word.circular_rotate_left_by_one()
# Perform byte substitution for each byte of the word --> SubBytes
new_word = BitVector(size = 0)
for i in range(4):
byte_idx = (i * 8)
row = int(word[byte_idx:(byte_idx + 4)])
col = int(word[(byte_idx + 4):(byte_idx + 8)])
new_word = new_word + BitVector(intVal = s_box[row][col], size = 8)
word = new_word
# Xor with round constant
AES_modulus = BitVector(bitstring = "100011011")
n = 8
empty_bv = BitVector(size = 24)
if ((round_num) > 0):
rcj1 = BitVector(size = 0)
rc = BitVector(hexstring = "02")
for i in range(round_num, 0, -1): #for (i = round_num; i > 0; i--):
if (i != 1):
rcj1 = BitVector(hexstring = "02")
else:
rcj1 = BitVector(hexstring = "01")
rc = rc.gf_multiply_modular(rcj1, AES_modulus, n)
else:
rc = BitVector(hexstring = "01")
round_constant = rc + empty_bv
word = word ^ round_constant
return word
示例7: print
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
print("\nTest division in GF(2^n):")
mod = BitVector(bitstring='100011011') # AES modulus
n = 8
a = BitVector(bitstring='11100010110001')
quotient, remainder = a.gf_divide_by_modulus(mod, n)
print("Dividing a=" + str(a) + " by mod=" + str(mod) + " in GF(2^8) returns the quotient "
+ str(quotient) + " and the remainder " + str(remainder))
# 10001111
print("\nTest modular multiplication in GF(2^n):")
modulus = BitVector(bitstring='100011011') # AES modulus
n = 8
a = BitVector(bitstring='0110001')
b = BitVector(bitstring='0110')
c = a.gf_multiply_modular(b, modulus, n)
print("Modular product of a=" + str(a) + " b=" +
str(b) + " in GF(2^8) is " + str(c))
# 10100110
print("\nTest multiplicative inverses in GF(2^3) with " +
"modulus polynomial = x^3 + x + 1:")
print("Find multiplicative inverse of a single bit array")
modulus = BitVector(bitstring='100011011') # AES modulus
n = 8
a = BitVector(bitstring='00110011')
mi = a.gf_MI(modulus, n)
print("Multiplicative inverse of " + str(a) + " in GF(2^8) is " + str(mi))
print("\nIn the following three rows shown, the first row shows the " +
"\nbinary code words, the second the multiplicative inverses," +
示例8: modularMul
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import gf_multiply_modular [as 别名]
def modularMul(byte, hexNum):
if(type(hexNum)!=str):
print "CRITICAL ERROR: hexnum is not a string"
a = BitVector( hexstring=hexNum)
return a.gf_multiply_modular(byte, modulus, 8)