当前位置: 首页>>代码示例>>Python>>正文


Python BitVector.divide_into_two方法代码示例

本文整理汇总了Python中BitVector.divide_into_two方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.divide_into_two方法的具体用法?Python BitVector.divide_into_two怎么用?Python BitVector.divide_into_two使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BitVector的用法示例。


在下文中一共展示了BitVector.divide_into_two方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: inverseSubstituteBytes

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
def inverseSubstituteBytes(cipher_block):
	pblock=cipher_block
	for i in range(0,4):
		for j in range(0,4):
			intj=ord(pblock[i][j])#convert to ascii num
			bv=BitVector(intVal=intj, size=8)#converting to bitvector
			[row,col]=bv.divide_into_two()#divide into two
			pblock[i][j]=chr(isBox[int(row)][int(col)])
	invsub_block=pblock
	return invsub_block #return a list of CHAR"
开发者ID:CrocKim,项目名称:Introduction-to-Computer-Security-ECE404,代码行数:12,代码来源:tt.py

示例2: encrypt

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
def encrypt(messages):
  rno=0 #round no. counter
  message = BitVector(hexstring = messages)#original message
  message=message.permute(ip) #perform intial permutation
  [l0,r0]=message.divide_into_two() #divide into left and right
  while(rno<16):
    #start calculation of f(r,k)
    j=0
    l1=r0
    rf=r0.permute(e) #perform expansion on right 
    rf=rf ^ k[rno] #xor with key for that round           
    
    ##process through s-box
    output=''
    bitno=0
    while(j<8):   
      i=0
      s=[] 	  
      while ( i < 6):                                
			  s.append( str(rf[bitno]) )                             
			  bitno+=1
			  i += 1
      index1=int((s[0]+s[-1]),2)
      index2=int(''.join(s[1:5]),2)
      s=hex(s_box[j][index1][index2])
      output+=s[2:]
      j+=1
    ##end process through s-box
    
    rf=BitVector(hexstring=output)
    output=rf.permute(p)#permutation with p
    #end calculation of f(r,k)
    output=output^l0 #xor with l[0]
    r1=output
    l0=l1
    r0=r1
    rno+=1
  output=r1+l1 #swapping left and right
  output=output.permute(inv_ip) #inverse permutation
  myhexstring = output.get_hex_string_from_bitvector()
  print 'Encrypted Message='+myhexstring 
开发者ID:sheetalgiri,项目名称:DES,代码行数:43,代码来源:des.py

示例3: GenerateRoundKeys

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
def GenerateRoundKeys(Encryption_Key, KeyPermutation1_List, KeyPermutation2_List, ShiftInfoDict):
	#Generate 56-Bit BV from Encryption Key
	encryptionKeyBV = BitVector(textstring = Encryption_Key) #Declare BitVector from bit string

	#Perform Permutation 1
	textBV = encryptionKeyBV.deep_copy()
	for ind in range(0,56):
		textBV[ind] = encryptionKeyBV[KeyPermutation1_List[ind]]

	encryptionKeyBV = encryptionKeyBV.permute(KeyPermutation1_List)

	#Split Key into two 28-Bit Halves
	[key1_L, key1_R] = encryptionKeyBV.divide_into_two() #Split key into two 28-bit halves

	#Perform Round 1 Shift for Encryption 
	round1_shift = ShiftInfoDict[1] #obtain number of bits to shift key in round 1
	key1_L = key1_L << round1_shift #circularly shift left, left half of key
	key1_R = key1_R << round1_shift #circularly shift left, right half of key

	#Perform Contraction Permutation for round 1 key
	round1_key = key1_L + key1_R #join together both left and right halves of round 1 key
	round1_key = round1_key.permute(KeyPermutation2_List) #perform 56 to 48 bit contracting permutation

	#Add 1st Round Key to List
	roundNum = 1
	keyList = [round1_key]
	key_L = key1_L
	key_R = key1_R

	#Add Remaining Round Keys to List
	for roundNum in range(1,16):
		round_shift = ShiftInfoDict[roundNum+1] #obtain number of bits to shift ket by in present round
		key_L = key_L.deep_copy() << round_shift #circularly shift left, left half of key
		key_R = key_R.deep_copy() << round_shift #circularly shift left, right half of key
		round_key = key_L.deep_copy() + key_R.deep_copy() #join together both left and right halves of round 1 key
		round_key = round_key.permute(KeyPermutation2_List)#perform 56 to 48 bit contracting permutation
		keyList.append(round_key.deep_copy())

	return keyList
开发者ID:timtrippel,项目名称:Encryption-Tools,代码行数:41,代码来源:DES_ttrippel.py

示例4: print

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
# print bv                                    # nothing to show
bv1 = bv.read_bits_from_file(64)
print("\nPrint out the first 64 bits read from the file:")
print(bv1)
# 0100000100100000011010000111010101101110011001110111001001111001
print("\nRead the next 64 bits from the same file:")
bv2 = bv.read_bits_from_file(64)
print(bv2)
# 0010000001100010011100100110111101110111011011100010000001100110
print("\nTake xor of the previous two bit vectors:")
bv3 = bv1 ^ bv2
print(bv3)
# 0110000101000010000110100001101000011001000010010101001000011111

print("\nExperiment with dividing an even-sized vector into two:")
[bv4, bv5] = bv3.divide_into_two()
print(bv4)                            # 01100001010000100001101000011010
print(bv5)                            # 00011001000010010101001000011111

# Permute a bit vector:
print("\nWe will use this bit vector for experiments with permute()")
bv1 = BitVector(bitlist=[1, 0, 0, 1, 1, 0, 1])
print(bv1)                                    # 1001101

bv2 = bv1.permute([6, 2, 0, 1])
print("\nPermuted and contracted form of the previous bit vector:")
print(bv2)                                    # 1010

print(
    "\nExperiment with writing an internally generated bit vector out to a disk file:")
bv1 = BitVector(bitstring='00001010')
开发者ID:gsnedders,项目名称:BitVector,代码行数:33,代码来源:BitVectorDemo.py

示例5: getRoundKey

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
	def getRoundKey(self, rk):
		rcon0=["00000001","00000000","00000000","00000000"] #32bit
		rcon1=["00000010","00000000","00000000","00000000"] #32bit
		rcon2=["00000100","00000000","00000000","00000000"] #32bit
		rcon3=["00001000","00000000","00000000","00000000"] #32bit
		rcon4=["00010000","00000000","00000000","00000000"] #32bit
		rcon5=["00100000","00000000","00000000","00000000"] #32bit
		rcon6=["01000000","00000000","00000000","00000000"] #32bit
		rcon7=["10000000","00000000","00000000","00000000"] #32bit
		rcon=[rcon0,rcon1,rcon2,rcon3,rcon4,rcon5,rcon6,rcon7]

		#round constants
		key=rk
		keyarray=[]
		nkey=[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]		
		listkey=[]
		#initializing nkey for w0 to w5
		temp=[]
		j=0
		looprange=[0,6,12,18,24,30,36,42]
		for i in range(0, 24):#making 4x6 temp key array
			temp.append(key[i])
			j=j+1
			if j==4:
				j=0
				keyarray.append(temp)
				temp=[]

		"""for i in range(0, 6):#operation for rotating the key to 6x4 columnwise
			for j in range(0, 4):
				print i,j
				nkey[j][i]=keyarray[i][j] """

		for i in range(0, 48):#initializing w5 to w53
			keyarray.append(['0','0','0','0'])
		nkey=keyarray
		for i in range(0, 54):#conversion to int
			for j in range(0, 4):
				nkey[i][j]=ord(nkey[i][j])
		
		j=0

		for i in looprange:

############################# G-Function ####################################
			
			####### left c-rotate ##########
			temp1=nkey[i+5][1]
			temp2=nkey[i+5][2]
			temp3=nkey[i+5][3]
			temp4=nkey[i+5][0]
			################################
			
			######## SubByte ###############
			bv0=BitVector(intVal=temp1, size=8)
			bv1=BitVector(intVal=temp2, size=8)
			bv2=BitVector(intVal=temp3, size=8)
			bv3=BitVector(intVal=temp4, size=8)
			[l,r]=bv0.divide_into_two()
			temp1=sBox[int(l)][int(r)]
			[l,r]=bv1.divide_into_two()
			temp2=sBox[int(l)][int(r)]
			[l,r]=bv2.divide_into_two()
			temp3=sBox[int(l)][int(r)]
			[l,r]=bv3.divide_into_two()
			temp4=sBox[int(l)][int(r)]
			################################
			
			#########  XOR round Constant ##
			bvk=BitVector(intVal=int(temp1), size=8)
			bvrc=BitVector(bitstring=rcon[j][0])
			temp1=int(bvk^bvrc)
			bvk=BitVector(intVal=int(temp2), size=8)
			bvrc=BitVector(bitstring=rcon[j][1])
			temp2=int(bvk^bvrc)
			bvk=BitVector(intVal=int(temp3), size=8)
			bvrc=BitVector(bitstring=rcon[j][2])
			temp3=int(bvk^bvrc)
			bvk=BitVector(intVal=int(temp4), size=8)
			bvrc=BitVector(bitstring=rcon[j][3])
			temp4=int(bvk^bvrc)
			#################################
###########################################################################
			########## key expansion  ############
			nkey[i+6][0]=temp1^nkey[i][0]
			nkey[i+6][1]=temp2^nkey[i][1]
			nkey[i+6][2]=temp3^nkey[i][2]
			nkey[i+6][3]=temp4^nkey[i][3]


			nkey[i+7][0]=nkey[i+6][0]^nkey[i+1][0]
			nkey[i+7][1]=nkey[i+6][1]^nkey[i+1][1]
			nkey[i+7][2]=nkey[i+6][2]^nkey[i+1][2]
			nkey[i+7][3]=nkey[i+6][3]^nkey[i+1][3]


			nkey[i+8][0]=nkey[i+7][0]^nkey[i+2][0]
			nkey[i+8][1]=nkey[i+7][1]^nkey[i+2][1]
			nkey[i+8][2]=nkey[i+7][2]^nkey[i+2][2]
			nkey[i+8][3]=nkey[i+7][3]^nkey[i+2][3]
#.........这里部分代码省略.........
开发者ID:CrocKim,项目名称:Introduction-to-Computer-Security-ECE404,代码行数:103,代码来源:hw14.py

示例6: range

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import divide_into_two [as 别名]
for i in range(0,16):
    gen_rnd_key(key_bv, i)
f_e = open("encrypted.txt", 'w')
f_d = open("decrypted.txt", 'w')
#f_i = open("message.txt", 'r')
## ask user for input encryption string of 8 characters in size
while True:
    txt = raw_input("Enter the 8 character long encryption text: ")
    if len(txt) != 8:
        print "Encryption text should be 8 characters in length."
    else:
        break
#print "Size of encryption text: ", len(txt)
i_bv = BitVector(textstring = txt)

[left,right] = i_bv.divide_into_two() #divide the input string bitvector into two 32bit bitvectors

round_val = 0
while round_val != 16: #basically, do the same thing until 16 rounds have passed
    #print "Round: ", round_val + 1
    right_permute = right.permute(expansion_permutation) #perform expansion permutation for the right half
    #now we will do the contraction permutation step for the key to generate 48 bits from 56 bits
    fin_key_bv = round_keys[round_val].permute(key_permutation_2) #perform the contraction permutation for the round key
    right_xor = right_permute ^ fin_key_bv #XOR right and key 48 bits
    #now we will perform the S-table part that generates 32 bits from 48 bits
    j = 0
    k = 5
    tmp_bv = BitVector(size = 32) #this will store the 32-bit temp
    tmp_bv = gen_from_s_box(right_xor, 0, j, k)
    for i in range(1,8):
        tmp_bv = tmp_bv + gen_from_s_box(right_xor, i, j, k)
开发者ID:jedkalita,项目名称:Purdue,代码行数:33,代码来源:DES_kalita.py


注:本文中的BitVector.divide_into_two方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。