本文整理汇总了Python中BitVector.write_to_file方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.write_to_file方法的具体用法?Python BitVector.write_to_file怎么用?Python BitVector.write_to_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.write_to_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AES_encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def AES_encrypt(filename, key) :
encrypted_data = BitVector(size=0)
words_4 = get_first_four_words(BitVector( textstring = key ))
key_sch = Key_Schedule(words_4)
round_keys = key_sch.round_keys
input_blocks = get_input_blocks(filename)
for block in input_blocks :
state_array = convert_block_to_state_array(block)
state_array = add_round_key(state_array, round_keys[0])
for i in range(10) :
state_array = substitute_bytes(state_array)
state_array = shift_rows(state_array)
if not (i == 9):
state_array = mix_columns(state_array)
state_array = add_round_key(state_array, round_keys[i+1])
encrypted_data += convert_state_array_to_128bits(state_array)
FILEOUT = open( "encrypted.txt", 'wb' )
encrypted_data.write_to_file(FILEOUT)
FILEOUT.close()
示例2: RSA_decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def RSA_decrypt(cipherfile, private_key, p, q):
print ("Decrypt bagin#######################################")
f = open(cipherfile,'r')
hexstr = f.read()
print (hexstr)
m = 0
n = 64
strlis = []
while(n <= len(hexstr)):
strlis.append(hexstr[m:n])
m += 64
n += 64
plains_bv = BitVector(size=0)
for substr in strlis:
package = BitVector(hexstring = substr)
print ("package")
print (package.get_hex_string_from_bitvector())
package_int = package.int_val()
Vp = pow(package_int,private_key[0],p)
Vq = pow(package_int,private_key[0],q)
pbv = BitVector(intVal=p)
qbv = BitVector(intVal=q)
Xp = q * qbv.multiplicative_inverse(pbv).int_val()
Xq = p * pbv.multiplicative_inverse(qbv).int_val()
plain_int = (Vp*Xp + Vq*Xq) % private_key[1]
plain_bv = BitVector(intVal=plain_int,size=128)
print ("plain_bv")
print (plain_bv.get_hex_string_from_bitvector())
plains_bv += plain_bv
fo = open("decrypted.txt",'w')
plains_bv.write_to_file(fo)
示例3: AES_decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def AES_decrypt(key) :
decrypted_data = BitVector(size=0)
#Split the input key into 4, 32 bit words
words_4 = get_first_four_words(BitVector( textstring = key ))
#Get the key schedule based on input key
key_sch = Key_Schedule(words_4)
round_keys = key_sch.round_keys
#Get input data in 128 bit blocks at a time
encrypted_blocks = get_input_blocks("encrypted.txt")
for block in encrypted_blocks :
#Do the pre-round processing
state_array = convert_block_to_state_array(block)
state_array = add_round_key(state_array, round_keys[10])
state_array = inv_shift_rows(state_array)
state_array = inv_substitute_bytes(state_array)
#Do the 10 rounds of processing
for i in reversed(range(1,10)) :
state_array = add_round_key(state_array, round_keys[i])
state_array = inv_mix_columns(state_array)
state_array = inv_shift_rows(state_array)
state_array = inv_substitute_bytes(state_array)
#Append decrypted data
state_array = add_round_key(state_array, round_keys[0])
decrypted_data += convert_state_array_to_128bits(state_array)
#Write decrypted data to file
FILEOUT = open( "decrypted.txt", 'wb' )
decrypted_data.write_to_file(FILEOUT)
FILEOUT.close()
示例4: aesDecryption
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def aesDecryption(cipherfile,roundkeys,np):
print ("Decryption begin %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
f = open(cipherfile,'r')
hexstr = f.read()
print (hexstr)
m = 0
n = 32
strlis = []
while(n <= len(hexstr)):
strlis.append(hexstr[m:n])
m += 32
n += 32
packages = BitVector(size=0)
print ("substr AAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAa")
print (strlis)
for substr in strlis:
package = BitVector(hexstring=substr)
print (package.get_hex_string_from_bitvector())
last4w = BitVector(size = 0)
for j in range(len(roundkeys[10])):
last4w += roundkeys[10][j]
print ("last4w")
print (last4w.get_hex_string_from_bitvector())
packager = package ^ last4w
print ("packager")
print (packager.get_hex_string_from_bitvector())
for ri in range(9,-1,-1):
print (str(ri)+" Decrypt round$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
shiftrows = inv_shift_rows(packager)
subinv = inv_sub_bytes(shiftrows)
print ("subinv")
print (subinv.get_hex_string_from_bitvector())
ad_rk = add_round_key(subinv,roundkeys,ri)
print ("ad_rk")
print (ad_rk.get_hex_string_from_bitvector())
if ri > 0:
colum_inv = inv_mix_columns(ad_rk)
else:
colum_inv = ad_rk
packager = colum_inv
print ("packager")
print (packager.get_hex_string_from_bitvector())
packages += packager
for i in range(9,0,-1):
print (i)
print ("np")
print (np)
if np > 0:
packages = packages.__getslice__(0,(len(strlis)-1)*128+np)
print (packages.get_hex_string_from_bitvector())
outfile = "decryptedtext.txt"
fo = open(outfile,'w')
packages.write_to_file(fo)
hh = "abcdefghijklmn"
print (hh)
rr = hh[0:5]
print (rr)
示例5: init_des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def init_des():
# Check use of program
if (len(sys.argv) != 5):
input_file = get_input_file()
output_file = get_output_file()
encrypt_or_decrypt = get_encrypt_or_decrypt()
nkey = get_encryption_key()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
with open(sys.argv[3]) as fp:
key = [x.strip('\n') for x in fp.readlines()]
key = ''.join(key)
if (len(key) != 8):
sys.exit("Error: Encryption key must be 8 characters long. Please modify your key file and try again.")
user_key_bv = BitVector(textstring = key)
nkey = user_key_bv.permute(key_permutation_1)
encrypt_or_decrypt = sys.argv[4][1]
if (encrypt_or_decrypt != 'e') and (encrypt_or_decrypt != 'd'):
sys.exit("Usage: python DES_Price.py <input_file> <output_file> <key_file> -e/d")
# Initialize input file as bit vector
bv_input = BitVector(filename = input_file)
file_out = open(output_file, 'wb')
result_bv = BitVector(size = 0)
sboxs = assemble_sboxes("s-box-tables.txt")
if (encrypt_or_decrypt == "e"):
print("Implementing DES encryption on '%s'..." % input_file)
else:
print("Implementing DES decryption on '%s'..." % input_file)
# Begin either encryption/decryption
result_bv = des(encrypt_or_decrypt, bv_input, result_bv, nkey, sboxs)
if (encrypt_or_decrypt == "e"):
print("Writing cipher-text to '%s'..." % output_file)
else:
print("Writing plain-text to '%s'..." % output_file)
result_bv.write_to_file(file_out)
print("DES complete!")
bv_input.close_file_object()
file_out.close()
示例6: init_des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def init_des():
if (len(sys.argv) != 5):
input_file = get_input_file()
output_file = get_output_file()
encrypt_or_decrypt = get_encrypt_or_decrypt()
nkey = get_encryption_key()
else:
input_file = sys.argv[1]
output_file = sys.argv[2]
with open(sys.argv[3]) as fp:
nkey = [x.strip('\n') for x in fp.readlines()]
nkey = ''.join(nkey)
if (len(nkey) != 8):
sys.exit("Error: Encryption key must be 8 characters long. Please modify your key file and try again.")
if (sys.argv[4] == "-e"):
encrypt_or_decrypt = "e"
elif (sys.argv[4] == "-d"):
encrypt_or_decrypt = "d"
else:
sys.exit("Usage: python DES_Price.py <input_file> <output_file> <key_file> -e/d")
sboxs = assemble_sboxes()
rkeys = generate_round_keys(nkey)
# Initialize input file as bit vector
bv_input = BitVector(filename = input_file)
file_out = open(output_file, 'wb')
result_bv = BitVector(size = 0)
if (encrypt_or_decrypt == "e"):
print("Implementing DES encryption on '%s'..." % input_file)
else:
print("Implementing DES decryption on '%s'..." % input_file)
result_bv = des(encrypt_or_decrypt, bv_input, result_bv, sboxs, rkeys)
if (encrypt_or_decrypt == "e"):
print("Writing cipher-text to '%s'..." % output_file)
else:
print("Writing plain-text to '%s'..." % output_file)
result_bv.write_to_file(file_out)
print("DES complete!")
bv_input.close_file_object()
file_out.close()
示例7: decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def decrypt(input_file, output_file, key):
d = key[0]
FILEOUT = open( output_file, 'ab' )
n = key[1]
p = key[2]
q = key[3]
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(256)
dec = Chinese_magic(bitvec, d, n, p, q)
pad = 128 - BitVector(intVal =dec).length()
result_bv += BitVector(intVal =dec, size = 128 )
result_bv.write_to_file(FILEOUT)
示例8: crack_with_CRT
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def crack_with_CRT() :
e, n1 = get_public_key("pub_key1.txt")
_, n2 = get_public_key("pub_key2.txt")
_, n3 = get_public_key("pub_key3.txt")
C1_blocks = get_encrypted_blocks("encrypted1.txt")
C2_blocks = get_encrypted_blocks("encrypted2.txt")
C3_blocks = get_encrypted_blocks("encrypted3.txt")
N = n1 * n2 * n3
N1 = N / n1
N2 = N / n2
N3 = N / n3
n1_bv = BitVector(intVal=n1)
n2_bv = BitVector(intVal=n2)
n3_bv = BitVector(intVal=n3)
N1_bv = BitVector(intVal=N1)
N2_bv = BitVector(intVal=N2)
N3_bv = BitVector(intVal=N3)
d1 = int(N1_bv.multiplicative_inverse(n1_bv))
d2 = int(N2_bv.multiplicative_inverse(n2_bv))
d3 = int(N3_bv.multiplicative_inverse(n3_bv))
# Bitvector used to store decrypted
total_bv = BitVector(size=0)
# Decrypt each block
for i in range(len(C1_blocks)) :
# Use chinese remainder thm to simplify
x = (C1_blocks[i] * N1 * d1 +
C2_blocks[i] * N2 * d2 +
C3_blocks[i] * N3 * d3) % N
de_int = solve_pRoot(3, x)
# Condence all the decrypted into 1 bitvector
total_bv += BitVector(intVal=de_int, size=128)
# Write the decrypted output to file
with open("cracked.txt", 'w') as f :
total_bv.write_to_file(f)
示例9: encrypt_RSA
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [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
示例10: encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def encrypt(self):
#Encrypt input and add encrypted blocks to Object
self.encrypted_blocks = self.encrypt_decrypt( range(16), self.plain_text_blocks )
encrypted_bits = BitVector( size = 0 )
#Put all the blocks into one long bitvector
for block in self.encrypted_blocks :
encrypted_bits = encrypted_bits + block
self.encrypted_bits = encrypted_bits
#Write out to the appropriate file
FILEOUT = open( self.encrypted_file, 'wb' )
encrypted_bits.write_to_file(FILEOUT)
FILEOUT.close()
示例11: decrypt_RSA
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def decrypt_RSA(self):
fileSize = os.path.getsize(self.inputFileName) #determine size of input file to be decrypted
numBitBlocks = fileSize / (self.modSize / 8) #determine size of decryption bit blocks
bitBlockInd = 1 #bit block index counter to know when to check for new line characters to be stripped from decrypted text
self.OpenFiles() #Open I/O files
#scan in bits from input file and perform decryption algorithm
while(self.inputFile_bv.more_to_read):
nextBitBlock_bv = self.inputFile_bv.read_bits_from_file(self.modSize) #Scan in bit block
nextBitBlock_bv = self.CRT(nextBitBlock_bv) #Perform modular exponentiation using CRT to speed up process
[zeros_pad, nextBitBlock_bv] = nextBitBlock_bv.divide_into_two() #remove padded zeros
if (bitBlockInd == numBitBlocks):
#Strip appended new line characters
outputTextHex = nextBitBlock_bv.get_hex_string_from_bitvector()
outputTextHex = outputTextHex.strip("0a")
nextBitBlock_bv = BitVector(hexstring = outputTextHex)
nextBitBlock_bv.write_to_file(self.outputFile) #write bit block to output file
bitBlockInd = bitBlockInd + 1 #increment bit block index counter
self.CloseFiles() #Close I/O Files
return
示例12: encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [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)
示例13: Crack_RSA
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_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
示例14: decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def decrypt(inputfile, outputfile, private_key_file):
# Get the private key values from file
d, n, p, q = get_private_key(private_key_file)
# get encrypted data as integers
en_data = get_encrypted_blocks(inputfile)
# Bitvector used to store decrypted
total_bv = BitVector(size=0)
# Decrypt each block
for en_int in en_data :
# Use chinese remainder thm to simplify
de_int = CRT_simplify(en_int, d, n, p, q)
# Condence all the decrypted into 1 bitvector
total_bv += BitVector(intVal=de_int, size=128)
# Write the decrypted output to file
with open(outputfile, 'w') as f :
total_bv.write_to_file(f)
示例15: CRT_crack
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import write_to_file [as 别名]
def CRT_crack(crackfile, cipherbv_lis1, cipherbv_lis2, cipherbv_lis3, public_key1, public_key2, public_key3):
print ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
n1 = public_key1[1]
n2 = public_key2[1]
n3 = public_key3[1]
print (n1)
print (n2)
print (n3)
N = n1 * n2 * n3
M1 = N / n1
M2 = N / n2
M3 = N / n3
'''
M1bv = BitVector(intVal=M1,size=128)
M2bv = BitVector(intVal=M2,size=128)
M3bv = BitVector(intVal=M3,size=128)
n1bv = BitVector(intVal=n1,size=128)
'''
MI1 = modinv(M1, n1)
MI2 = modinv(M2, n2)
MI3 = modinv(M3, n3)
cipherlen = len(cipherbv_lis1)
b = solve_pRoot(3,125)
print (b)
plains_bv = BitVector(size=0)
for i in range(cipherlen):
C1 = cipherbv_lis1[i].int_val()
C2 = cipherbv_lis2[i].int_val()
C3 = cipherbv_lis3[i].int_val()
M_to3 = (C1*M1*MI1 + C2*M2*MI2 + C3*M3*MI3) % N
M = solve_pRoot(3, M_to3)
plain_bv = BitVector(intVal=M, size=128)
plains_bv += plain_bv
fo = open(crackfile,'w')
plains_bv.write_to_file(fo)