本文整理汇总了Python中BitVector.read_bits_from_file方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.read_bits_from_file方法的具体用法?Python BitVector.read_bits_from_file怎么用?Python BitVector.read_bits_from_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.read_bits_from_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: des2
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def des2(encrypt_or_decrypt, input_file, output_file, key):
## Get the key
cipher = ""
kBV = BitVector(filename=key)
kBV = kBV.read_bits_from_file(64)
## Get the round keys
rKeys = create_keys(kBV)
## Create the bitvector for readint in the file
bv = BitVector(filename=input_file)
## Prepare output file
FILEOUT = open(output_file, "wb")
## Use the more_to_read function to travers whole file
arr2 = []
s_box2 = []
for i in range(32):
arr2.append(numpy.random.permutation(16).tolist())
for i in range(0, 32, 4):
s_box2.append([arr2[k] for k in range(i, i + 4)])
while bv.more_to_read:
## Read 64 bit block
bitvec = bv.read_bits_from_file(64)
## Ensure that length is 64, if not pad
if (bitvec.length() % 64) != 0:
bitvec.pad_from_right(64 - (bitvec.length() % 64))
## Use bitvector fucntion to split it into two
[LE, RE] = bitvec.divide_into_two()
## Perform the 16 rounds of feistel
for i in range(16):
## Place right in temp as it'll be the next left
TEMP = RE
## Perform the expansion permutation
RE = RE.permute(expansion_permutation)
## check if we're encrypting or decrypting
if encrypt_or_decrypt == 0:
RE = RE ^ rKeys[i] ## XOR with the roundkey
elif encrypt_or_decrypt == 1:
RE = RE ^ rKeys[15 - i] ## reverse direction for decrypting
## Initialize a bit vector to use
sBV = BitVector(size=0)
## Perform the s-box functions for all 8
for j in range(8):
r = 2 * RE[6 * j] + 1 * RE[5 + 6 * j]
c = 8 * RE[1 + 6 * j] + 4 * RE[2 + 6 * j] + 2 * RE[3 + 6 * j] + 1 * RE[4 + 6 * j]
sBV += BitVector(intVal=int(s_box2[j][r][c]), size=4)
## Perform the p-permutation
pBV = sBV.permute(p_box_permutation)
## Calculate the new right based of the left, and our f(RE,k)
RE = LE ^ pBV
## Make the right the new left
LE = TEMP
## Reverse the two and write it to the file
bitvec = RE + LE
cipher += str(bitvec)
FILEOUT.write(bitvec.get_text_from_bitvector())
return cipher
FILEOUT.close()
示例2: checkdiff
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def checkdiff (file1, file2):
bv1 = BitVector(filename = file1)
bv2 = BitVector(filename = file2)
cuenta = 0
while (bv1.more_to_read):
bitv1 = bv1.read_bits_from_file( 64 )
bitv2 = bv2.read_bits_from_file( 64 )
xorthi = bitv1 ^ bitv2
for i in range(64):
if xorthi[i] == 1:
cuenta = cuenta + 1
return cuenta
示例3: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def des(encrypt_or_decrypt, input_file, output_file, key ):
bv = BitVector( filename = input_file )
FILEOUT = open( output_file, 'wb' )
bitvec = bv.read_bits_from_file( 64 ) ## assumes that your file has an integral
## multiple of 8 bytes. If not, you must pad it.
print bitvec
[LE, RE] = bitvec.divide_into_two()
示例4: getInput_decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def getInput_decrypt(filein):
inputlist = []
bv = BitVector( filename=filein )
while (bv.more_to_read):
bitvec = bv.read_bits_from_file( 256 ) #get the encrypted data by 256 bit
inputlist.append(int(bitvec))
return inputlist
示例5: change_one_bit
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def change_one_bit():
subprocess.call(['cp message.txt temp.tmp'],shell=True)
file_size = os.path.getsize(input_file)
num_blocks = file_size / 8
if file_size % 8 != 0:
with open(input_file,"a") as filein:
for i in range(0,file_size % 8):
filein.write(" ")
for i in range(0,num_blocks):
#subprocess.call(['cp temp.tmp temp2.tmp'],shell=True)
bv = BitVector( filename = "temp.tmp" )
os.remove("message.txt")
FILEOUT = open("message.txt",'ab')
for j in range(0,num_blocks):
bitvcec = bv.read_bits_from_file( 64 )
if j == i:
rand = randint(0,63)
bitvec[rand] ^= 1
bitvec.write_to_file(FILEOUT)
FILEOUT.close()
subprocess.call(['./DES_baio.py'],shell=True)
os.close(filehandle)
os.remove(input_file)
move(final, input_file)
示例6: AES_dec
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def AES_dec(key, message, fileout):
## Get the key
kBV = BitVector( textstring = inKey )
bv = BitVector( filename = message )
FILEOUT = open(fileout, 'wb')
stateArray = [[0 for x in range(4)] for x in range(4)]
nextArray = [[0 for x in range(4)] for x in range(4)]
while(bv.more_to_read):
bitvec = bv.read_bits_from_file(128)
## Ensure that length is 128, if not pad
if((bitvec.length() % 128) != 0):
bitvec.pad_from_right(128-(bitvec.length() % 128))
getKeySchedule(bitvec)
bitvec = addRoundKey(bitvec, roundKeys[10])
for i in range(4):
for j in range(4):
nextArray[j][i] = hex(bitvec[32*i + 8*j:32*i+8*(j+1)].int_val())
## Different process for AES_dec
for x in range(9):
stateArray = nextArray
stateArray = InvShiftRows(stateArray)
stateArray = InvSubBytes(stateArray)
stateArray = addRoundKey(sAr2BV(stateArray), roundKeys[9-x])
for i in range(4):
for j in range(4):
nextArray[j][i] = hex(stateArray[32*i + 8*j:32*i+8*(j+1)].int_val())
nextArray = InvMixColumns(nextArray)
stateArray = InvShiftRows(nextArray)
stateArray = InvSubBytes(stateArray)
stateArray = addRoundKey(sAr2BV(stateArray), kBV)
FILEOUT.write(stateArray.get_text_from_bitvector())
FILEOUT.close()
示例7: messwithfile
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def messwithfile(rightf, messedf):
FILEOUT = open (messedf, 'wb')
lakey = DES_flegmann.get_encryption_key()
round_keys = DES_flegmann.extract_round_key(lakey)
tamano = os.path.getsize(rightf)
tamano = tamano / 8
bv = BitVector(filename = rightf)
count = 0
tot = 0
for index in range(8):
while (bv.more_to_read):
bitvec = bv.read_bits_from_file( 64 )
leng = bitvec.length()
pad = 64 - leng
if count == index:
bitvec.pad_from_right(pad)
bitvec[random.randint(0,15)] ^= 1
bitvec.write_to_file(FILEOUT)
count = count + 1
FILEOUT.close()
bv.close_file_object()
DES_flegmann.des(1, rightf, 'righte.txt',round_keys)
DES_flegmann.des(1, messedf, 'messede.txt', round_keys)
tot += checkdiff('righte.txt', 'messede.txt')
os.remove('righte.txt')
os.remove('messede.txt')
return tot / 8
示例8: openFile
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def openFile(file, type):
## Get the file length
flen = os.stat(file).st_size
bv = BitVector(filename = file)
## Read in the blocks, size dependent on encryption or decryption
if type:
data_blocks = [bv.read_bits_from_file(128) for i in range((flen/16)+1)]
else:
data_blocks = [int(bv.read_bits_from_file(256)) for i in range(flen/32)]
return data_blocks
## New line version of bitvector for appending
nl = BitVector(textstring = '\n')
## Pad the message with new lines
while(len(data_blocks[-1]) < 128): data_blocks[-1] = data_blocks[-1] + nl
## Prepend the data block with 128 zeroes to make it a 256 bit block
for block in data_blocks: block.pad_from_left(128)
return data_blocks
示例9: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def des(encrypt_or_decrypt, input_file, output_file, key ):
bv = BitVector( filename = input_file )
FILEOUT = open( output_file, 'w' )
while(bv.more_to_read):
bitvec = bv.read_bits_from_file( 64 ) ## assumes that your file has an integral
mod = bitvec.length() % 64
if not mod == 0:
bitvec.pad_from_right(64 - mod) ## multiple of 8 bytes. If not, you must pad it.
[LE, RE] = bitvec.divide_into_two()
rkey = extract_round_key(key)
for i in range(16):
## write code to carry out 16 rounds of processing
TMP = RE
## Expansion_permutation
RE = RE.permute(expansion_permutation)
# Get the order of key right
if encrypt_or_decrypt == "encrypt":
RE = RE ^ rkey[i]
elif encrypt_or_decrypt == "decrypt":
RE = RE ^ rkey[15 - i]
## Do the S_boxes subsititution
firstbit = 0
midbit = 1
lastbit = 5
newBV = BitVector(size = 0)
for ct in range(8):
row = 2 * RE[firstbit] + RE[lastbit]
col = 8 * RE[midbit] + 4 * RE[midbit + 1] + 2 * RE[midbit + 2] + RE[midbit + 3]
sbox_num = s_box[ct][row][col]
sbox_bitvector = BitVector(intVal = int(sbox_num), size = 4)
newBV += sbox_bitvector
firstbit += 6
midbit += 6
lastbit += 6
## Permutation with P-Box
newBV = newBV.permute(p_box_permutation)
## XOR with original LE
RE = LE ^ newBV
LE = TMP
## Add RE and LE up
bitvec = RE + LE
## Output the encryption or decryption
mytext = bitvec.get_text_from_bitvector()
FILEOUT.write(mytext)
print mytext
FILEOUT.close()
示例10: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def get_encryption_key():
#get key from user
key = raw_input("Enter Key Filename: ")
#convert key to bitvector
keybv = BitVector(filename = key)
keybv1 = keybv.read_bits_from_file( 64 )
return keybv1
示例11: getInput_encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def getInput_encrypt(filein):
inputlist = []
bv = BitVector( filename=filein )
while (bv.more_to_read):
bitvec = bv.read_bits_from_file( 128 ) #Get 128 bit data
while len(bitvec) < 128:
bitvec += BitVector( textstring=' ' )
bitvec.pad_from_left(128) #pad zero from left to let the data size be 256 bit
inputlist.append(int(bitvec))
return inputlist
示例12: EncryptFile
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def EncryptFile(inputFileName, outputFileName, roundKeyList, KeyPermutation1_List, KeyPermutation2_List, SBoxes_Dict_List, PBox_List, ShiftInfoDict, modeChar):
#open output file for writing and create BV instance of input file
outputTextFile = open(outputFileName, "wb")
inputFile_bv = BitVector(filename = inputFileName)
nullByte_bv = BitVector(size = 8)
nullChar = chr(int('00000000',2))
#If decrypting --> retrieve size of output file (to aid with stripping of null bytes at end)
if (modeChar == 'D'):
fileSize = os.path.getsize(inputFileName)
byteCount = 0
#Scan in 64-Bit blocks of file at a time
while (inputFile_bv.more_to_read):
bitBlock_bv = inputFile_bv.read_bits_from_file(64)
if bitBlock_bv.length() > 0:
#Pad bit block with trailing zeros if necessary
if (bitBlock_bv.length() != 64):
bitBlock_bv.pad_from_right(64-bitBlock_bv.length())
# bytesShort = (64 - bitBlock_bv.length())/8
# bytesShort_bv = BitVector(int = bytesShort)
# for ind in range(0, bytesShort):
# bitBlock_bv = bitBlock_bv + bytesShort_bv
#Split Bit Vector into 2-32Bit halves
[lBV, rBV] = bitBlock_bv.divide_into_two()
#Perform 1st Round of Encryption
[lBV, rBV] = Encrypt_64Bit_Block(lBV, rBV, roundKeyList[0], SBoxes_Dict_List, PBox_List)
#Perform Remaining 15 Rounds of Encryption
for roundNum in range(1,16):
#Retrieve Round Key
round_key = roundKeyList[roundNum]
#Call 64-bit Block Encryption Function
[lBV, rBV] = Encrypt_64Bit_Block(lBV, rBV, round_key, SBoxes_Dict_List, PBox_List)
#Concatenate Left and Right Halves of 64-Bit Block
encryptedBitBlock_bv = rBV + lBV
#Check for Padded NULL bytes if in "Decrypt" Mode
ind = 0
outputText = encryptedBitBlock_bv.getTextFromBitVector()
if (modeChar == 'D'):
byteCount = byteCount + 8
if (byteCount > (fileSize - 8)):
outputText = outputText.strip(nullChar)
#Write Output To Output File
#print outputText
outputTextFile.write(outputText)
#Close Files
outputTextFile.close()
inputFile_bv.close_file_object()
return
示例13: calculateHash
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [as 别名]
def calculateHash(filename):
size = os.path.getsize(filename)
size=size-1
input_len=size*8
binary = BitVector(filename=filename)
bv1 = binary.read_bits_from_file(8 * size)
#padding and appending size
rem=len(bv1)%1024
if (rem==0):
newblock=BitVector(intVal=0, size=896)
newblock[0]=1
in_len=BitVector(intVal=input_len, size=128)
newblock=newblock+in_len
bv1=bv1+newblock
elif (rem<=896):
one=BitVector(intVal=1)
zero=BitVector(intVal=0, size=1)
bv1=bv1+one
while len(bv1)%1024!=896:
bv1=bv1+zero
in_len=BitVector(intVal=input_len, size=128)
bv1=bv1+in_len
elif (rem>896) and (rem<1024):
one=BitVector(intVal=1)
zero=BitVector(intVal=0, size=1)
bv1=bv1+one
while (len(bv1)%1024)!=0:
bv1=bv1+zero
newblock=BitVector(intVal=0, size=896)
newblock[0]=1
bvlen=BitVector(intVal=input_len, size=128)
newblock=newblock+bvlen
bv1=bv1+newblock
binary.close_file_object()
K_init() #initialize K.
iv=initVec_init()
block=[]
counter=0
for i in range(0, len(bv1)):
block.append(bv1[i])
counter=counter+1
if counter==1024:
counter=0
iv=f(block, iv)
block=[]
hash=[hex(iv[0]),hex(iv[1]),hex(iv[2]),hex(iv[3]),hex(iv[4]),hex(iv[5]),hex(iv[6]),hex(iv[7])]
hash=striphex(hash)
final_hash="" #final hash in CSV form
for i in hash:
final_hash=final_hash+i+","
final_hash=final_hash[0:len(final_hash)-1]
return final_hash
示例14: Crack_RSA
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [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
示例15: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import read_bits_from_file [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)