base64变化码表
# coding:utf-8
s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
# s = "WHydo3sThiS7ABLElO0k5trange+CZfVIGRvup81NKQbjmPzU4MDc9Y6q2XwFxJ/"
def My_base64_encode(inputs):
# 将字符串转化为2进制
bin_str = []
for i in inputs:
x = str(bin(ord(i))).replace('0b', '')
bin_str.append('{:0>8}'.format(x))
# 输出的字符串
outputs = ""
# 不够三倍数,需补齐的次数
nums = 0
while bin_str:
# 每次取三个字符的二进制
temp_list = bin_str[:3]
if len(temp_list) != 3:
nums = 3 - len(temp_list)
while len(temp_list) < 3:
temp_list += ['0' * 8]
temp_str = "".join(temp_list)
# 将三个8字节的二进制转换为4个十进制
temp_str_list = []
for i in range(0, 4):
temp_str_list.append(int(temp_str[i*6:(i+1)*6], 2))
if nums:
temp_str_list = temp_str_list[0:4 - nums]
for i in temp_str_list:
outputs += s[i]
bin_str = bin_str[3:]
outputs += nums * '='
print("Encrypted String:\n%s" % outputs)
return outputs
def My_base64_decode(inputs):
# 将字符串转化为2进制
bin_str = []
for i in inputs:
if i != '=':
x = str(bin(s.index(i))).replace('0b', '')
bin_str.append('{:0>6}'.format(x))
# 输出的字符串
outputs = ""
hex_outputs = "" # 用于存储十六进制形式的输出
byte_array = [] # 用于存储字节数组
nums = inputs.count('=')
while bin_str:
temp_list = bin_str[:4]
temp_str = "".join(temp_list)
# 补足8位字节
if len(temp_str) % 8 != 0:
temp_str = temp_str[0:-1 * nums * 2]
# 将四个6字节的二进制转换为三个字符
for i in range(0, int(len(temp_str) / 8)):
char = chr(int(temp_str[i*8:(i+1)*8], 2))
outputs += char
# 添加到十六进制输出
hex_outputs += '{:02X} '.format(ord(char))
# 添加到字节数组
byte_array.append(ord(char))
bin_str = bin_str[4:]
print("Decrypted String:\n%s" % outputs)
print("Hexadecimal Representation of Decrypted String:\n%s" % hex_outputs.strip())
print("Byte Array of Decrypted String:\n%s" % byte_array)
return byte_array
print()
print(" *************************************")
print(" * (1)encode (2)decode *")
print(" *************************************")
print()
num = input("Please select the operation you want to perform:\n")
if num == "1":
input_str = input("Please enter a string that needs to be encrypted: \n")
encoded_str = My_base64_encode(input_str)
else:
input_str = input("Please enter a string that needs to be decrypted: \n")
decoded_bytes = My_base64_decode(input_str)