本文整理汇总了Python中BitVector.permute方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.permute方法的具体用法?Python BitVector.permute怎么用?Python BitVector.permute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.permute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: diffusion_or_confusion
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def diffusion_or_confusion(RE,LE,encrypt_or_decrypt,rkey,s_box):
bitvec = BitVector(size=64)
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
k=0
output = BitVector(size = 0)
for ct in range(8):
row = RE[k]*pow(2,1) + RE[k+5]*pow(2,0)
col = RE[k+1]*pow(2,3) + RE[k+2]*pow(2,2)+ RE[k+3]*pow(2,1) + RE[k+4]*pow(2,0)
sbox_val = s_box[ct][row][col]
sbox_bv = BitVector(intVal = int(sbox_val), size = 4)
output += sbox_bv
k += 6
## Permutation with P-Box
output = output.permute(p_box_permutation)
## XOR with original LE
RE = LE ^ output
LE = TMP
## Add RE and LE up
bitvec = RE + LE
return bitvec
示例2: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key():
user_given_key = raw_input("Enter encryption key: ")
while (len(user_given_key) != 8):
print("Error: Encryption key must be 8 characters long. Try again.")
user_given_key = raw_input("Enter encryption key: ")
user_key_bv = BitVector(textstring = user_given_key)
key_bv = user_key_bv.permute(key_permutation_1)
return key_bv
示例3: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
## make sure it satisfies any constraints on the key
## next, construct a BitVector from the key
user_key_bv = BitVector(text = user-supplied_key)
key_bv = user_key_bv.permute( initial_permutation ) ## permute() is a BitVector function
return key_bv
示例4: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
## make sure it satisfies any constraints on the key
with open('key.txt','r') as keyfile:
user_supplied_key = keyfile.read().replace('\n','')
## next, construct a BitVector from the key
user_key_bv = BitVector(textstring = user_supplied_key)
key_bv = user_key_bv.permute( key_permutation_1 ) ## permute() is a BitVector function
return key_bv
示例5: des2
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [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()
示例6: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [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()
示例7: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
key = None
f = open('key.txt', 'r')
rkey=f.read()
key = rkey[:8]#make key 8 characters
## make sure it satisfies any constraints on the key
## next, construct a BitVector from the key
user_key_bv = BitVector(textstring = key)
key_bv = user_key_bv.permute(key_permutation_1 ) ## permute() is a BitVector function
return key_bv
示例8: encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def encrypt(messages):
rno=0 #round no. counter
message = BitVector(hexstring = messages)#original message
message=message.permute(ip) #perform intial permutation
[l0,r0]=message.divide_into_two() #divide into left and right
while(rno<16):
#start calculation of f(r,k)
j=0
l1=r0
rf=r0.permute(e) #perform expansion on right
rf=rf ^ k[rno] #xor with key for that round
##process through s-box
output=''
bitno=0
while(j<8):
i=0
s=[]
while ( i < 6):
s.append( str(rf[bitno]) )
bitno+=1
i += 1
index1=int((s[0]+s[-1]),2)
index2=int(''.join(s[1:5]),2)
s=hex(s_box[j][index1][index2])
output+=s[2:]
j+=1
##end process through s-box
rf=BitVector(hexstring=output)
output=rf.permute(p)#permutation with p
#end calculation of f(r,k)
output=output^l0 #xor with l[0]
r1=output
l0=l1
r0=r1
rno+=1
output=r1+l1 #swapping left and right
output=output.permute(inv_ip) #inverse permutation
myhexstring = output.get_hex_string_from_bitvector()
print 'Encrypted Message='+myhexstring
示例9: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
while True:
try:
key = raw_input("Please enter an eight character key: ")
except EOFError: sys.exit()
## make sure it satisfies any constraints on the key
if len(key) == 8:
break
## next, construct a BitVector from the key
user_key_bv = BitVector(textstring = key)
#print user_key_bv
key_bv = user_key_bv.permute( key_permutation_1 ) ## permute() is a BitVector function
return key_bv
示例10: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
#f = open("key.txt","r")
key = raw_input("\nEnter 8 byte key: ")
## make sure it satisfies any constraints on the key
while not len(key) == 8:
key = raw_input("\nInvalid key length! Re-enter 8 byte key: ")
print 'You have provided an appropriate key.'
## next, construct a BitVector from the key
user_key_bv = BitVector(textstring=key)
key_bv = user_key_bv.permute(key_permutation_1) ## permute() is a BitVector function
##using given key_permutation_1 vector to permute the input key. This is the first step to obtain a 48 bit round key from the 56 bit key input by the user.
return key_bv
示例11: init_des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [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()
示例12: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
## make sure it satisfies any constraints on the key
valid = False;
while not valid:
user-supplied_key = raw_input("Enter 8 character key: ")
if len(user-supplied_key) != 8:
print "Error: Key is not 8 characters"
else:
valid = True
## next, construct a BitVector from the key
user_key_bv = BitVector(text = user-supplied_key)
key_bv = user_key_bv.permute( initial_permutation ) ## permute() is a BitVector function
return key_bv
示例13: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key ## ask user for input key
while True:
key = raw_input("Enter the 8 character long key: ")
if len(key) != 8:
print "Key should be 8 characters in length."
else:
break
## make sure it satisfies any constraints on the key
## next, construct a BitVector from the key
#print "Size of key: ", len(key)
user_key_bv = BitVector(textstring = key)
key_bv = user_key_bv.permute( key_permutation_1 ) ## permute() is a BitVector function
return key_bv #this is now the initial 56-bit key for round 1, which will have to be shifted and then contracted permuted to XOR with the right half
示例14: get_encryption_key
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [as 别名]
def get_encryption_key(): # key
## ask user for input
mykey = raw_input("Enter your 8 bytes key: ")
## make sure it satisfies any constraints on the key
while not len(mykey) == 8 or not mykey.isalpha():
print("Your key should be 8 English charaters.")
mykey = raw_input("Enter your 8 bytes key: ")
## next, construct a BitVector from the key
user_key_bv = BitVector(textstring = mykey)
key_bv = user_key_bv.permute(key_permutation_1) ## permute() is a BitVector function
return key_bv
示例15: des
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import permute [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)