本文整理汇总了Python中BitVector.get_text_from_bitvector方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.get_text_from_bitvector方法的具体用法?Python BitVector.get_text_from_bitvector怎么用?Python BitVector.get_text_from_bitvector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.get_text_from_bitvector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
def decrypt(encrypted_bv, key):
PassPhrase = "Hopes and dreams of a million years"
BLOCKSIZE = 16
numbytes = BLOCKSIZE // 8
# Reduce the passphrase to a bit array of size BLOCKSIZE:
bv_iv = BitVector(bitlist = [0]*BLOCKSIZE)
for i in range(0,len(PassPhrase) // numbytes):
textstr = PassPhrase[i*numbytes:(i+1)*numbytes]
bv_iv ^= BitVector( textstring = textstr )
# Reduce the key to a bit array of size BLOCKSIZE:
key_bv = BitVector(bitlist = [0]*BLOCKSIZE)
key_bv = BitVector(bitstring = key)
# Create a bitvector for storing the decrypted plaintext bit array:
msg_decrypted_bv = BitVector( size = 0 )
# Carry out differential XORing of bit blocks and decryption:
previous_decrypted_block = bv_iv
for i in range(0, len(encrypted_bv) // BLOCKSIZE):
bv = encrypted_bv[i*BLOCKSIZE:(i+1)*BLOCKSIZE]
temp = bv.deep_copy()
bv ^= previous_decrypted_block
previous_decrypted_block = temp
bv ^= key_bv
msg_decrypted_bv += bv
# Extract plaintext from the decrypted bitvector:
return msg_decrypted_bv.get_text_from_bitvector()
示例2: writeFile
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
def writeFile(data_block, filename, type, mode):
## Open the output file
out = open(filename, 'wa')
## Depending if we're encrypting or decrypting for the size of byte to write
## 256 - Encrypt, 128 - Decrypt
for data in data_block:
if type:
bv = BitVector(intVal = data, size = 256)
else:
bv = BitVector(intVal = data, size = 128)
if mode:
out.write(bv.get_text_from_bitvector())
else:
out.write(bv.get_hex_string_from_bitvector())
示例3: BitVector
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
#key_bv ^= BitVector( textstring = keyblock ) #(S)
#go through all the different iterations of 16 bit key
for candidate in range(0,65536):
key_bv = BitVector(intVal = candidate)
# Create a bitvector for storing the decrypted plaintext bit array:
msg_decrypted_bv = BitVector( size = 0 ) #(T)
# Carry out differential XORing of bit blocks and decryption:
previous_decrypted_block = bv_iv #(U)
for i in range(0, len(encrypted_bv) // BLOCKSIZE): #(V)
bv = encrypted_bv[i*BLOCKSIZE:(i+1)*BLOCKSIZE] #(W)
temp = bv.deep_copy() #(X)
bv ^= previous_decrypted_block #(Y)
previous_decrypted_block = temp #(Z)
bv ^= key_bv #(a)
msg_decrypted_bv += bv #(b)
# Extract plaintext from the decrypted bitvector:
outputtext = msg_decrypted_bv.get_text_from_bitvector()
# Write plaintext to the output file:
#test the the output to make sure that it has funerals
if (outputtext.find("funerals") != -1):
FILEOUT = open(sys.argv[2], 'w')
print "yay I'm in the key is:"
print key_bv #(d)
FILEOUT.write(outputtext)
FILEOUT.close()
break
示例4: checkIfMessageExists
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
def checkIfMessageExists(self): #the function to check if the medium ctually contains a message
#first check if the direction that the message is embedded in is horizontal
if (self.dir == 'horizontal'): #for horizontal rasterization
pix = list(self.im.getdata()) #get the list of pixels
#now, we will check if the first line (the first 39 characters are '<?xml version="1.0" encoding="UTF-8"?>\n')
first_line = '<?xml version="1.0" encoding="UTF-8"?>\n'#the first line to be compared with this
st1 = '' #the string of bits that will make the first 39 characters extracted out of the medium
for i in range(0, 312):
st1 = st1 + bin(pix[i])[2:].rjust(8, '0')[7] #just get the LSB of each pixel 8 bit representation
bv_comp = BitVector(bitstring = st1)#get the equivalent bitvector representation
txt = bv_comp.get_text_from_bitvector()#get the equivalent text from the first 39 characters of the medium
if (txt != first_line):#if the first line is not matched
tup = (False, None) #the tuple is now (false, None)
return tup #return the tuple indicating that the medium has no message
else: #if the first line is as it is supposed to be
#now, in this case, the next step will be to find out of the underlying message is a Text, GrayImage, or ColorImage)
#before we proceed with the code, first of all let us note one important observation that the first 39 characters have been consumed by the statement represented byb first_line variable. The next 15 characters is the string '<message type="', and then this implies that the first (39+15)=54 characters are constant at this point. The 55th characters, however, corresponds to a 'C'(ColorImage), 'G'(GrayImage) or 'T'(Text), an this is what we will exploit to find out the type of image
st2 = ''#the string of bits holding the byte representation of the 55th character
for i in range(432, 440): #for the 55th character
st2 = st2 + bin(pix[i])[2:].rjust(8, '0')[7] #just get the LSB of each pixel 8 bit representation
bv_comp2 = BitVector(bitstring = st2)#get the equivalent bitvector representation
txt2 = bv_comp2.get_text_from_bitvector()#get the equivalent text from the first 39 characters of the medium
if (txt2 == 'C'): #if C, then ColorImage
tup = (True, 'ColorImage') #make the corresponding tuple
return tup #return the tuple
elif (txt2 == 'G'): #if G, then GrayImage
tup = (True, 'GrayImage') #make the corresponding tuple
return tup #return the tuple
elif (txt2 == 'T'): #if T, then Text
tup = (True, 'Text') #make the corresponding tuple
return tup #return the tuple
else:
pass
else: #if vertical rasterization on the medium
pixels = self.im.load() #2-D array of pixels of the medium image
pix = []#to store list of pixels in a vertical rasterization format
for i in range(self.width): #performing a vertical scanning(L-R,T-B)
for j in range(self.height):
p = pixels[i, j] #get the corresponding pixel
pix.append(p) #append the number/pixel gotten into the new pixel list
#now, we will check if the first line (the first 39 characters are '<?xml version="1.0" encoding="UTF-8"?>\n')
first_line = '<?xml version="1.0" encoding="UTF-8"?>\n'#the first line to be compared with this
st1 = '' #the string of bits that will make the first 39 characters extracted out of the medium
for i in range(0, 312):
st1 = st1 + bin(pix[i])[2:].rjust(8, '0')[7] #just get the LSB of each pixel 8 bit representation
bv_comp = BitVector(bitstring = st1)#get the equivalent bitvector representation
txt = bv_comp.get_text_from_bitvector()#get the equivalent text from the first 39 characters of the medium
if (txt != first_line):#if the first line is not matched
tup = (False, None) #the tuple is now (false, None)
return tup #return the tuple indicating that the medium has no message
else: #if the first line is as it is supposed to be
#now, in this case, the next step will be to find out of the underlying message is a Text, GrayImage, or ColorImage)
#before we proceed with the code, first of all let us note one important observation that the first 39 characters have been consumed by the statement represented byb first_line variable. The next 15 characters is the string '<message type="', and then this implies that the first (39+15)=54 characters are constant at this point. The 55th characters, however, corresponds to a 'C'(ColorImage), 'G'(GrayImage) or 'T'(Text), an this is what we will exploit to find out the type of image
st2 = ''#the string of bits holding the byte representation of the 55th character
for i in range(432, 440): #for the 55th character
st2 = st2 + bin(pix[i])[2:].rjust(8, '0')[7] #just get the LSB of each pixel 8 bit representation
bv_comp2 = BitVector(bitstring = st2)#get the equivalent bitvector representation
txt2 = bv_comp2.get_text_from_bitvector()#get the equivalent text from the first 39 characters of the medium
if (txt2 == 'C'): #if C, then ColorImage
tup = (True, 'ColorImage') #make the corresponding tuple
return tup #return the tuple
elif (txt2 == 'G'): #if G, then GrayImage
tup = (True, 'GrayImage') #make the corresponding tuple
return tup #return the tuple
elif (txt2 == 'T'): #if T, then Text
tup = (True, 'Text') #make the corresponding tuple
return tup #return the tuple
else:
pass
示例5: main
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
def main():
## Generate 3 sets of public and private keys
pub1, priv1, p1, q1, e = createKeys(3)
pub2, priv2, p2, q2, e = createKeys(3)
pub3, priv3, p3, q3, e = createKeys(3)
with open("private1.txt", 'w') as f :
f.write("d="+str(priv1[0])+"\n")
f.write("n="+str(p1*q1)+"\n")
f.write("p="+str(p1)+"\n")
f.write("q="+str(q1)+"\n")
with open("private2.txt", 'w') as f :
f.write("d="+str(priv2[0])+"\n")
f.write("n="+str(p2*q2)+"\n")
f.write("p="+str(p2)+"\n")
f.write("q="+str(q2)+"\n")
with open("private3.txt", 'w') as f :
f.write("d="+str(priv3[0])+"\n")
f.write("n="+str(p3*q3)+"\n")
f.write("p="+str(p3)+"\n")
f.write("q="+str(q3)+"\n")
## Encrypt 1 file 3 times with each of the different public keys
encrypted1 = encrypt("message.txt", pub1)
encrypted2 = encrypt("message.txt", pub2)
encrypted3 = encrypt("message.txt", pub3)
## Write these files out
writeFile(encrypted1, "enc1.txt", True, False)
writeFile(encrypted2, "enc2.txt", True, False)
writeFile(encrypted3, "enc3.txt", True, False)
## Calculate N the product of all the values of n
N = pub1[1] * pub2[1] * pub3[1]
## Caluclate Ni = N / ni
N1 = N / pub1[1]
N2 = N / pub2[1]
N3 = N / pub3[1]
## Get BitVector representations of Ni and ni for use in BitVector multiplicative inverse
bvM1 = BitVector(intVal = pub1[1])
bvM2 = BitVector(intVal = pub2[1])
bvM3 = BitVector(intVal = pub3[1])
bv1 = BitVector(intVal = N1)
bv2 = BitVector(intVal = N2)
bv3 = BitVector(intVal = N3)
## Caluclate the multiplicative inverse of each Ni modulo ni
C1 = int(bv1.multiplicative_inverse(bvM1))
C2 = int(bv2.multiplicative_inverse(bvM2))
C3 = int(bv3.multiplicative_inverse(bvM3))
crackedBV = BitVector(size = 0)
## By CRT recover M^3 by the below equation
for z in range(len(encrypted1)):
x = (encrypted1[z] * N1 * C1 + encrypted2[z] * N2 * C2 + encrypted3[z] * N3 * C3) % N
## Recover M
cINT = solve_pRoot(3, x)
crackedBV += BitVector(intVal = cINT, size = 128)
## Write out the recovered file
out = open("cracked.txt", 'wa')
out.write(crackedBV.get_text_from_bitvector())
out = open("HEXcracked.txt", 'wa')
out.write(crackedBV.get_hex_string_from_bitvector())
示例6: BitVector
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import get_text_from_bitvector [as 别名]
bv = BitVector(bitstring = '')
print("\nBit Vector constructed directly from an empty bit string:")
print(bv) # nothing
print("\nInteger value of the previous bit vector:")
print(bv.intValue()) # 0
print("\nConstructing a bit vector from the textstring 'hello':")
bv3 = BitVector(textstring = "hello")
print(bv3)
mytext = bv3.getTextFromBitVector()
print("Text recovered from the previous bitvector: ")
print(mytext) # hello
print("\nConstructing a bit vector from the textstring 'hello\\njello':")
bv3 = BitVector(textstring = "hello\njello")
print(bv3)
mytext = bv3.get_text_from_bitvector()
print("Text recovered from the previous bitvector:")
print(mytext) # hello
# jello
print("\nConstructing a bit vector from the hexstring '68656c6c6f':")
bv4 = BitVector(hexstring = "68656c6c6f")
print(bv4)
myhexstring = bv4.getHexStringFromBitVector()
print("Hex string recovered from the previous bitvector: ")
print(myhexstring) # 68656c6c6f
print("\nDemonstrating the raw bytes mode of constructing a bit vector (useful for reading public and private keys):")
mypubkey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5amriY96HQS8Y/nKc8zu3zOylvpOn3vzMmWwrtyDy+aBvns4UC1RXoaD9rDKqNNMCBAQwWDsYwCAFsrBzbxRQONHePX8lRWgM87MseWGlu6WPzWGiJMclTAO9CTknplG9wlNzLQBj3dP1M895iLF6jvJ7GR+V3CRU6UUbMmRvgPcsfv6ec9RRPm/B8ftUuQICL0jt4tKdPG45PBJUylHs71FuE9FJNp01hrj1EMFObNTcsy9zuis0YPyzArTYSOUsGglleExAQYi7iLh17pAa+y6fZrGLsptgqryuftN9Q4NqPuTiFjlqRowCDU7sSxKDgU7bzhshyVx3+pzXO4D2Q== [email protected]'
import base64
if sys.version_info[0] == 3:
import binascii