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


Python BitVector.length方法代码示例

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


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

示例1: des

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import length [as 别名]
def des( s_box, input_file, output_file, keys ): 
    FILEOUT = open( output_file, 'ab' ) 
    bv = BitVector( filename = input_file )
    while (bv.more_to_read ):
        bitvec = bv.read_bits_from_file( 64 )   ## assumes that your file has an integral
        leng = bitvec.length()                                        ## multiple of 8 bytes. If not, you must pad it.
        pad = 64 - leng
        bitvec.pad_from_right(pad)
        [LE, RE] = bitvec.divide_into_two()   
        for i in range(16):        
            righty = RE.permute(expansion_permutation)
            resultxor = righty ^ keys[i]
            #s substitution
            temp_bv = BitVector(size = 6)
            row_bv = BitVector(size = 2)
            col_bv = BitVector(size = 4)
            ini=0
            fin=6
            inf=0
            fif=4
            final_bv = BitVector(size = 32)
            for k in range(8):
                temp_bv = resultxor[ini:fin]
                row_bv[0:1] = temp_bv[0:1]
                row_bv[1:2] = temp_bv[5:6]
                col_bv = temp_bv[1:5]
                row = row_bv.int_val()#get row
                col = col_bv.int_val()#get column      
                    #get stuff from sbox 
                newd = s_box[k][row][col]
                temp = BitVector(intVal = newd)
                temp_leng = temp.length()
                lpad = 4 - temp_leng
                temp.pad_from_left(lpad)
                final_bv[inf:fif] = temp
                inf = inf + 4
                fif = fif + 4
                ini = ini + 6#increase to the next thing
                fin = fin + 6#increase to th next chunck
                #permutation with p box
            last_p = final_bv.permute(p_box_permutation)
                #xor with le result = RE
            temporal = RE
            RE = last_p ^ LE
            LE = temporal #le equals old re     
            #for loop done
            #put left + right on output file
        file_writebv =  RE + LE
        file_writebv.write_to_file(FILEOUT)
开发者ID:igalfsg,项目名称:ECE404,代码行数:51,代码来源:Average_flegmann.py

示例2: encrypt

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import length [as 别名]
def encrypt (input_file, output_file, key):
    e = key[0]
    FILEOUT = open( output_file, 'a' ) 
    n = key[1]
    result_bv = BitVector(size =0) #used to append the results
    bv = BitVector(filename = input_file)
    while(bv.more_to_read):
        bitvec = bv.read_bits_from_file(128)
        leng = bitvec.length()                                      
        pad = 128 - leng
        bitvec.pad_from_right(pad)
        bitvec.pad_from_left(128)
        
        temp = find_exponent(int(bitvec), int(e),int(n))
        temp  = BitVector(intVal = temp)
        pad = 256 -  temp.length()
        temp.pad_from_left(pad)
        result_bv += temp
    result_bv.write_to_file(FILEOUT)
开发者ID:igalfsg,项目名称:ECE404,代码行数:21,代码来源:flegmann_RSA_hw06.py

示例3: Crack_RSA

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import length [as 别名]
def Crack_RSA(key1, key2, key3, etext1, etext2, etext3, output_file):
    fin = BitVector(size = 0)
    e = key1[0]
    FILEOUT = open( output_file, 'a' ) 
    n1 =  key1[1]
    
    
    n2 =  key2[1]
    
    n3 =  key3[1]
    bigN = int(n1) * int(n2) * int (n3)
    bigN1 = BitVector (intVal = (bigN / int(n1)))
    bigN2 = BitVector (intVal = (bigN / int(n2)))
    bigN3 = BitVector (intVal = (bigN / int(n3)))
    mi1 = int(bigN1.multiplicative_inverse(n1))
    mi2 = int(bigN2.multiplicative_inverse(n2))
    mi3 = int(bigN3.multiplicative_inverse(n3))
    
    bv1 = BitVector(filename = etext1)
    bv2 = BitVector(filename = etext2)
    bv3 = BitVector(filename = etext3)
   
    while(bv1.more_to_read):
        bitvec1 = bv1.read_bits_from_file(256)
        bitvec2 = bv2.read_bits_from_file(256)
        bitvec3 = bv3.read_bits_from_file(256)
        
        temp = ((int(bitvec1) * int(bigN1) * mi1) + 
                (int(bitvec2) * int(bigN2) * mi2) +
                (int(bitvec3) * int(bigN3) * mi3)) % bigN
        
        temp2 = solve_pRoot(3, temp)
        temp = BitVector(intVal = temp2)
        pad = 128 - temp.length()
        temp.pad_from_left(pad)
        fin +=temp
    fin.write_to_file(FILEOUT)
    return
开发者ID:igalfsg,项目名称:ECE404,代码行数:40,代码来源:flegmann_breakRSA_hw06.py

示例4: BitVector

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import length [as 别名]
    sys.stderr.write("Usage: %s  <string to be hashed>\n" % sys.argv[0])
    sys.exit(1)

message = sys.argv[1]

# Initialize hashcode for the first block. Subsequetnly, the
# output for each 512-bit block of the input message becomes
# the hashcode for the next block of the message.
h0 = BitVector(hexstring='67452301')
h1 = BitVector(hexstring='efcdab89')
h2 = BitVector(hexstring='98badcfe')
h3 = BitVector(hexstring='10325476')
h4 = BitVector(hexstring='c3d2e1f0')

bv = BitVector(textstring = message)
length = bv.length()
bv1 = bv + BitVector(bitstring="1")
length1 = bv1.length()
howmanyzeros = (448 - length1) % 512
zerolist = [0] * howmanyzeros
bv2 = bv1 + BitVector(bitlist = zerolist)
bv3 = BitVector(intVal = length, size = 64)
bv4 = bv2 + bv3

words = [None] * 80

for n in range(0,bv4.length(),512):
    block = bv4[n:n+512]
    words[0:16] = [block[i:i+32] for i in range(0,512,32)]
    for i in range(16, 80):
        words[i] = words[i-3] ^ words[i-8] ^ words[i-14] ^ words[i-16]
开发者ID:mbaio94,项目名称:ECE404_Assignments,代码行数:33,代码来源:sha1_from_command_line.py

示例5: encrypt

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import length [as 别名]
def encrypt():

    #get key from user and then extract the round key.
    key = get_encryption_key()
    round_key = extract_round_key( key )

    #get the filename of the plaintext
    filein1 = raw_input("Enter name of Plaintext File: ")
 
    #convert file to bitvector
    bv = BitVector(filename = filein1)

    encrypted_text = ""	

    #loop through file 64 bits at a time
    while (bv.more_to_read):
        
	bitvec = bv.read_bits_from_file( 64 )
        
	if bitvec.length() == 64:
       
		#loop through 16 rounds for every 64 bit set
		for round_no in range(0,16):
			
			#perform single shift left during rounds 2,9,16
			if round_no in [2,9,16]:

				[temp_l, temp_r] = round_key.divide_into_two()

				temp_l.circular_rot_left()
				temp_r.circular_rot_left()
				
				round_key = temp_l + temp_r

			#perform double shift left during appropriate rounds
			elif round_no in [3,4,5,6,7,8,10,11,12,13,14,15]:

				[temp_l, temp_r] = round_key.divide_into_two()

				temp_l.circular_rot_left()
				temp_r.circular_rot_left()
				temp_l.circular_rot_left()
				temp_r.circular_rot_left()

				round_key = temp_l + temp_r
			
			
			#split 64 bits into right and left halves
			[LE, RE] = bitvec.divide_into_two()

			#expand 32 bits into 48 bits
		        newRE = RE.permute( expansion_permutation )

			#xor right side with round key
	        	out_xor = ( newRE ^ round_key )

			counter = 0
			RE_temp = BitVector(size = 0) 

			#loop through each 48 bit set by groups of 6
			for box in range(0,8):

				#begin s substitution

				#set row equal to the first and last bit of every 6 bit set concatenated
				row = out_xor[counter] + out_xor[counter + 5]
	
				#set column equal to the middle four bits concatenated
				column = out_xor[counter + 1] + out_xor[counter + 2] + out_xor[counter + 3] + out_xor[counter + 4]
	
				#append to empty string till done with 48 bits
				RE_temp += ( BitVector(intVal = (s_box[box][int(row)][int(column)]) , size = 4 ))

				counter += 6

			
			if RE_temp.length() == 32:		
			#convert RE temp back to 32 bits with PBOX permutation
				RE_modified = RE_temp.permute( P_box )
		
			#concatenate the unmodified Right side with the new right side xored with the left side
			bitvec = RE + (RE_modified ^ ( LE ))

			
		encrypted_text = encrypted_text + (str(bitvec))
		
    print encrypted_text
开发者ID:nkumar212,项目名称:ECE404,代码行数:89,代码来源:hw2_des.py


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