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


Python BitVector.pad_from_left方法代码示例

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


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

示例1: SubWord

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
def SubWord(word):
    for i in range(0,32,8):
        temp = BitVector( intVal = subBytesTable[word[i:i+8].int_val()])
        if len(temp) < 8:
            temp.pad_from_left(8-len(temp))
        word[i:i+8] = temp

    return word
开发者ID:ajdunker,项目名称:ECE404,代码行数:10,代码来源:ece404_hw04_dunker.py

示例2: sAr2BV

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
def sAr2BV(state):
    tBV = BitVector(intVal = int(state[0][0], 0))
    if len(tBV) < 8:
        tBV.pad_from_left(8-(len(tBV)%8))
    bv = tBV
    for c in range(0,4,1):
        for r in range(0,4,1):
            if((r+c) != 0):
                tBV = BitVector(intVal = int(state[r][c], 0))
                if len(tBV) < 8:
                    tBV.pad_from_left(8-(len(tBV)))
                bv = bv + tBV
    return bv
开发者ID:ajdunker,项目名称:ECE404,代码行数:15,代码来源:ece404_hw04_dunker.py

示例3: des

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [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

示例4: encrypt_RSA

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
	def encrypt_RSA(self):
		self.OpenFiles() #Open I/O files
		#scan in bits from input file and perform encryption algorithm
		while(self.inputFile_bv.more_to_read):
			nextBitBlock_bv = self.inputFile_bv.read_bits_from_file(self.blockSize) #Scan in bit block
			if (nextBitBlock_bv.length() != self.blockSize):
				#Append newline characters to end of bit block
				num_NL_chars = (self.blockSize - nextBitBlock_bv.length()) / 8
				inputBitBlockText = nextBitBlock_bv.get_hex_string_from_bitvector()
				inputBitBlockText = inputBitBlockText + "0a"*num_NL_chars
				nextBitBlock_bv = BitVector(hexstring = inputBitBlockText)
			nextBitBlock_bv.pad_from_left(self.blockSize) #Pad bit block with zeros
			cipher_text = self.ModExpo(int(nextBitBlock_bv), self.e, self.n) #generate cipher-text "integer" using modular exponentiation
			outputBitBlock_bv = BitVector(intVal = cipher_text, size = self.modSize) #generate 256-bit bit-vector of cipher-text "integer"
			outputBitBlock_bv.write_to_file(self.outputFile) #Write cipher text to output file
		self.CloseFiles() #Close I/O Files
		return
开发者ID:timtrippel,项目名称:Encryption-Tools,代码行数:19,代码来源:RSA256.py

示例5: extract_round_key

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
def extract_round_key(): # round key 
    key = "howtogettosesame"
    key_bv = BitVector(textstring = key)
    #key_bv = BitVector(hexstring = "2b7e151628aed2a6abf7158809cf4f3c")
    key_arr = []
    key_arr.append(key_bv[0:32])
    key_arr.append(key_bv[32:64])
    key_arr.append(key_bv[64:96])
    key_arr.append(key_bv[96:128])
    ri = 0
    for i in range (0,37,4):
        #making G
        #rotation
        temp = key_arr[i+3].deep_copy()
        temp << 8
        star = 0
        finis = 8
        #substitution
        for k in range(0,4):
            val = temp[star:finis]
            [row, column] = val.divide_into_two()
            irow = int(row)
            icolumn = int(column)
            index = (irow*16) + icolumn
            tempint = BitVector(intVal = subBytesTable[index])
            largo = len(tempint)
            largo = 8 - largo
            tempint.pad_from_left(largo)
            temp[star:finis]  =tempint
            star= finis
            finis += 8
        g = Rcon[ri] ^ temp
        ri +=1
        #done making G
        key4 = key_arr[i] ^ g
        key5 = key4 ^ key_arr[i+1]
        key6 = key5 ^ key_arr[i+2]
        key7 = key6 ^ key_arr[i+3]
        key_arr.append(key4)
        key_arr.append(key5)        
        key_arr.append(key6)
        key_arr.append(key7)

    return key_arr
开发者ID:igalfsg,项目名称:ECE404,代码行数:46,代码来源:ece404_hw04_flegmann.py

示例6: encrypt

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [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

示例7: s_box_substitution

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
    def s_box_substitution(self, word_48bit) :
        
        #split the 48 bit word into 8, 6 bit words
        words_6bit = [word_48bit[0:6], word_48bit[6:12], word_48bit[12:18], word_48bit[18:24],
                      word_48bit[24:30], word_48bit[30:36], word_48bit[36:42], word_48bit[42:48]]
        
        #32 bit result that will be returned
        result_32bit = BitVector(size = 0)

        for i, word in enumerate(words_6bit):

            #Get the appropriate s-box
            s_box = self.s_boxes[i]
           
            #get the bits that yield the the row index
            row_bits = BitVector(bitlist = [word[0], word[5]])

            #get the bits that yield the the column index
            col_bits = BitVector(bitlist = [word[1], word[2], word[3], word[4]])

            #get the actual integer row index. Offset by one because arrays
            row = int(row_bits)

            #get the actual integer column index. Offset by one because arrays
            col = int(col_bits)

            s_row = s_box[row]

            new_value = s_row[col]

            new_word_6bit = BitVector(intVal = new_value)

            #check if new word is 4 bits, padded the left side with zeroes if its not
            if ( len(new_word_6bit) < 4 ) :
                new_word_6bit.pad_from_left( 4 - len(new_word_6bit) )

            #Add the new word to the total 32bit result
            result_32bit = result_32bit + new_word_6bit


        return result_32bit
开发者ID:sheel7,项目名称:ECE404,代码行数:43,代码来源:DES_sheeley.py

示例8: Crack_RSA

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [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

示例9: print

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
print(bv3)

print("Test len() on the above bit vector:")
print(len(bv3))                      # 64

print("\nTest forming a [5:22] slice of the above bit vector:")
bv4 = bv3[5:22]
print(bv4)                             # 00100100000011010

print("\nTest the iterator:")
for bit in bv4:
    print(bit)                         # 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0

print("\nDemonstrate padding a bit vector from left:")
bv = BitVector(bitstring='101010')
bv.pad_from_left(4)
print(bv)                              # 0000101010

print("\nDemonstrate padding a bit vector from right:")
bv.pad_from_right(4)
print(bv)                              # 00001010100000

print("\nTest the syntax 'if bit_vector_1 in bit_vector_2' syntax:")
try:
    bv1 = BitVector(bitstring='0011001100')
    bv2 = BitVector(bitstring='110011')
    if bv2 in bv1:
        print("%s is in %s" % (bv2, bv1))
    else:
        print("%s is not in %s" % (bv2, bv1))
except ValueError as arg:
开发者ID:gsnedders,项目名称:BitVector,代码行数:33,代码来源:BitVectorDemo.py

示例10: dec_aes

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
def dec_aes(input_file, output_file, llaves):
    #open file
    input_arr  =[[0 for x in range(4)] for x in range(4)]
    bv = BitVector( filename = input_file ) 
    FILEOUT = open( output_file, 'ab' ) 
    bv = BitVector( filename = input_file )
    tempv = [0]* 4
    while (bv.more_to_read ):
        bitvec = bv.read_bits_from_file( 128 )   
        leng = bitvec.length()                                        
        pad = 128 - leng
        bitvec.pad_from_right(pad)
        kv= 0
        start=0
        for i in range(0,4):
            for k in range(0,4):
                input_arr[i][k] = BitVector(size = 8)
                input_arr[i][k] =  bitvec[start:(start+8)]
                start +=8
        #finished construction of input array
        #print_arr(input_arr)
        llaves.reverse()
        #xor keys with "plain text"
        for i in range(0,4):
            input_arr[i][0] = input_arr[i][0] ^ llaves[i][0:8]
            input_arr[i][1] = input_arr[i][1] ^ llaves[i][8:16]
            input_arr[i][2] = input_arr[i][2] ^ llaves[i][16:24]
            input_arr[i][3] = input_arr[i][3] ^ llaves[i][24:32] 
      
        #print_arr(input_arr)

        for ig in range(0,10):
            #print ig
            #shift rows
            for i in range(1,4):
                for k in range(0,4):
                    tempv[(k+i)%4] = input_arr[k][i]
                for k in range (0,4):
                    input_arr[k][i] = tempv[k]
            #print_arr(input_arr)
            #lets do the substitution
            for i in range(0,4):
                for k in range(0,4):
                    [row, column] = input_arr[i][k].divide_into_two()
                    irow = int(row)
                    icolumn = int(column)
                    #print irow, icolumn
                    index = (irow*16) + icolumn
                    tempint = BitVector(intVal = invSubBytesTable[index])
                    largo = len(tempint)
                    largo = 8 - largo
                    tempint.pad_from_left(largo)
                    input_arr[i][k] = tempint
            #print_arr(input_arr)


            #xor key
            kv += 4   
            for i in range(0,4):
                input_arr[i][0] = input_arr[i][0] ^ llaves[kv+i][0:8]
                input_arr[i][1] = input_arr[i][1] ^ llaves[kv+i][8:16]
                input_arr[i][2] = input_arr[i][2] ^ llaves[kv+i][16:24]
                input_arr[i][3] = input_arr[i][3] ^ llaves[kv+i][24:32]

            #print_arr(input_arr)
            #shift columns
            if(ig > 0):
                for i in range (0,4):
                    s0 = (catorce.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ (once.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ (trece.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ (nueve.gf_multiply_modular(input_arr[i][3],modulus, 8))
                    s1 = (nueve.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ (catorce.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ (once.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ (trece.gf_multiply_modular(input_arr[i][3],modulus, 8))
                    s2 = (trece.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ (nueve.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ (catorce.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ (once.gf_multiply_modular(input_arr[i][3],modulus, 8))
                    s3 = (once.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ (trece.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ (nueve.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ (catorce.gf_multiply_modular(input_arr[i][3],modulus, 8))

                    input_arr[i][0] = s0
                    input_arr[i][1] = s1
                    input_arr[i][2] = s2
                    input_arr[i][3] = s3
            #print_arr(input_arr)
        for i in range (0,4):
            #print_arr(input_arr)
            file_writebv = input_arr[i][0] + input_arr[i][1] + input_arr[i][2] + input_arr[i][3]
            file_writestuff = file_writebv
            file_writebv.write_to_file(FILEOUT) 
开发者ID:igalfsg,项目名称:ECE404,代码行数:85,代码来源:ece404_hw04_flegmann.py

示例11: encrypt_aes

# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import pad_from_left [as 别名]
def encrypt_aes (input_file, output_file, llaves):
    #open file
    input_arr  =[[0 for x in range(4)] for x in range(4)]
    bv = BitVector( filename = input_file ) 
    FILEOUT = open( output_file, 'ab' ) 
    bv = BitVector( filename = input_file )
    tempv = [0]* 4
    
    while (bv.more_to_read ):
        bitvec = bv.read_bits_from_file( 128 ) 
        #bitvec = BitVector(hexstring = "3243f6a8885a308d313198a2e0370734")  
        leng = bitvec.length()                                        
        pad = 128 - leng
        bitvec.pad_from_right(pad)
        
        kv= 0
        start=0
        for i in range(0,4):
            for k in range(0,4):
                input_arr[i][k] = BitVector(size = 8)
                input_arr[i][k] =  bitvec[start:(start+8)]
                start +=8
        #finished construction of input array

        for i in range(0,4):
            input_arr[i][0] = input_arr[i][0] ^ llaves[i][0:8]
            input_arr[i][1] = input_arr[i][1] ^ llaves[i][8:16]
            input_arr[i][2] = input_arr[i][2] ^ llaves[i][16:24]
            input_arr[i][3] = input_arr[i][3] ^ llaves[i][24:32] 

        for ig in range(0,10):
            #lets do the substitution
            for i in range(0,4):
                for k in range(0,4):
                    [row, column] = input_arr[i][k].divide_into_two()
                    irow = int(row)
                    icolumn = int(column)
                    #print irow, icolumn
                    index = (irow*16) + icolumn
                    tempint = BitVector(intVal = subBytesTable[index])
                    largo = len(tempint)
                    largo = 8 - largo
                    tempint.pad_from_left(largo)
                    input_arr[i][k] = tempint
            #print_arr(input_arr)
            #2 shift rows
            for i in range(1,4):
                for k in range(0,4):
                    tempv[(k-i)%4] = input_arr[k][i]
                for k in range (0,4):
                    input_arr[k][i] = tempv[k]
            #print_arr(input_arr)
            #shift columns
            if(ig < 9):
                for i in range (0,4):
                    s0 = (dos.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ (tres.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ input_arr[i][2] ^ input_arr[i][3]
                    s1 = input_arr[i][0] ^ (dos.gf_multiply_modular(input_arr[i][1],modulus, 8)) ^ (tres.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ input_arr[i][3]
                    s2 = input_arr[i][0] ^ input_arr[i][1] ^ (dos.gf_multiply_modular(input_arr[i][2],modulus, 8)) ^ (tres.gf_multiply_modular(input_arr[i][3],modulus, 8))
                    s3 = (tres.gf_multiply_modular(input_arr[i][0],modulus, 8)) ^ input_arr[i][1] ^ input_arr[i][2] ^ (dos.gf_multiply_modular(input_arr[i][3],modulus, 8))
                    input_arr[i][0] = s0
                    input_arr[i][1] = s1
                    input_arr[i][2] = s2
                    input_arr[i][3] = s3
                #print_arr(input_arr)
                
            
        #xor key
            kv += 4   
            for i in range(0,4):
                input_arr[i][0] = input_arr[i][0] ^ llaves[kv+i][0:8]
                input_arr[i][1] = input_arr[i][1] ^ llaves[kv+i][8:16]
                input_arr[i][2] = input_arr[i][2] ^ llaves[kv+i][16:24]
                input_arr[i][3] = input_arr[i][3] ^ llaves[kv+i][24:32] 
        for i in range (0,4):
            #print_arr(input_arr)
            file_writebv = input_arr[i][0] + input_arr[i][1] + input_arr[i][2] + input_arr[i][3]
            file_writestuff = file_writebv
            file_writebv.write_to_file(FILEOUT) 
开发者ID:igalfsg,项目名称:ECE404,代码行数:80,代码来源:ece404_hw04_flegmann.py


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