1
2
def hgh_String_to_Byte(self):       # 字符串转byte
self.plaintext_byte = self.plaintext.encode('utf-8')

byte异或

1
2
3
4
5
6
7
8
9
10
def hgh_Byte_Xor(self,byte1,byte2):
hgh = b''
length = len(byte1)
if len(byte1)!=len(byte2):
print('Xor---byte1 与 byte2 长度不相等')
else:
for i in range(length):
hgh +=bytes([byte1[i] ^ byte2[i]])

return hgh

byte与

1
2
3
4
5
6
7
8
9
10
def hgh_Byte_And(self,byte1,byte2):
hgh = b''
length = len(byte1)
if len(byte1) != len(byte2):
print('And---byte1 与 byte2 长度不相等')
else:
for i in range(length):
hgh += bytes([byte1[i] & byte2[i]])

return hgh

byte或

1
2
3
4
5
6
7
8
9
10
11

def hgh_Byte_Or(self,byte1,byte2):
hgh = b''
length = len(byte1)
if len(byte1) != len(byte2):
print('Or----byte1 与 byte2 长度不相等')
else:
for i in range(length):
hgh += bytes([byte1[i] | byte2[i]])

return hgh

byte非

1
2
3
4
5
6
7
8
9

def hgh_Byte_No(self,X):
hgh = b''
length = len(X)
h = 2**8 - 1
for i in range(length):
hgh += bytes([X[i] ^ h])

return hgh

byte模加

1
2
3
4
5
6
7
8

def hgh_Byte_Mod_Add(self,*A):
hgh = int.from_bytes(A[0],byteorder='big')
for i in range(1,len(A)):
h = int.from_bytes(A[i],byteorder='big')
hgh = pow(hgh+h,1,2**32)

return hgh.to_bytes(length=4,byteorder='big')

byte循环左移

1
2
3
4
5
6
7
8
def hgh_Byte_LiftShift(self,bt,iv):
length = len(bt)
iv = iv % 32
hgh = int.from_bytes(bt,byteorder='big')
h = hgh >> (length*8-iv)
g = (hgh - (h << (length*8-iv))) << (iv)

return (h+g).to_bytes(length=length,byteorder='big')

byte循环右移

1
2
3
4
5
6
7
8
def hgh_Byte_RightShift(self,bt,iv):
length = len(bt)
iv = iv % 32
hgh = int.from_bytes(bt,byteorder='big')
h = hgh >> (iv)
g = (hgh - (h << (iv))) << (length*8-iv)

return (h+g).to_bytes(length=length,byteorder='big')

主要是两个函数:
(1)将byte转换为大整数的函数: int.from_bytes(somebyte,byteorder='big')
(2)将大整数转换为byte的函数:
someint.to_bytes(length=len,byteorder='big')