BASE系列

简介: BASE系列

BASE64

  • 编码对象: ascill字符
  • 原理:
  • 将每个字符的8位ascill码组合在一起,保证组合后的长度是24的倍数,不足的在后面添0
  • 将组合好的字符串以6位一组拆分

将六位二进制转为十进制,并查字符表

数值 字符 数值 字符 数值 字符 数值 字符
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

  • 注意,最后面的数为0的不需要查表,替换为"="
  • 举例
  • 编码: ab

转为ascill码: 01100001 01100010

长度为12,不是24的倍数,补0 : 01100001 01100010 00000000

分组: 011000 010110 001000 000000

转为10进制: 24 22 8 0

最后有一个0,替换为"+", 其余查表: Y W I =

所以ab编码后的密文为: YWI=

代码

# write in 2021/6/21
import re
DIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def is_ascii(char):
  if ord(char) >= 0 and ord(char) <= 127:
    return True
  return False
def encrypt_base64(string):
  bin_string = ""
  ciphertext = ""
  num = 0
  length = len(string)
  for char in string:
    # 异常检测
    if not is_ascii(char):
      return -1
    bin_string += ("00000000" + str(bin(ord(char)))[2::])[-8::]
  #print(length)
  if length % 3 == 1:
    bin_string += "0"*4
    num = 2
  elif length % 3 == 2:
    bin_string += "0"*8
    num = 1
  bin_list = re.findall(".{6}",bin_string)
  for i in bin_list:
    ciphertext += DIC[int(i,2)]
  return ciphertext + "="*num
def decrypt_base64(ciphertext):
  plaintext = ""
  bin_string = ""
  print(ciphertext)
  num = ciphertext.count("=")
  #print(num)
  if num:
    ciphertext = ciphertext[0:-num:]
  #print(ciphertext)
  for i in ciphertext:
    # 异常检测
    if i not in DIC:
      return -1
    bin_string += ("0"*6 + str(bin(DIC.index(i)))[2::])[-6::]
  bin_list = re.findall(".{8}", bin_string)
  print(bin_list)
  for i in bin_list:
    plaintext += chr(int(i, 2))
  return plaintext
# 01000000
if __name__ == '__main__':
  print("Aabc ",encrypt_base64("Aabc"))
  print("Aabc ",decrypt_base64(encrypt_base64("Aabc")))

BASE32

  • 编码对象: ascill字符
  • 原理:
  • 将每个字符转为8位ascill码,组合在一起,需要保证长度是40的倍数, 不足的在后面补0
  • 将组合后的串以每五位分割开来
  • 将五位的二进制转为十进制,在查表:
数值 字符 数值 字符 数值 字符 数值 字符
0 A 8 I 16 Q 24 Y
1 B 9 j 17 R 25 Z
2 C 10 K 18 S 26 2
3 D 11 L 19 T 27 3
4 E 12 M 20 U 28 4
5 F 13 N 21 V 29 5
6 G 14 O 22 W 30 6
7 H 15 P 23 X 31 7
  • 注意:最后的0不需要查表, 替换为"="
  • 代码
# write in 2021/6/22
import re
DIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
def is_ascii(char):
  if ord(char) >= 0 and ord(char) <= 127:
    return True
  return False
def encrypt_base32(string):
  bin_string = ""
  ciphertext = ""
  num = 0
  length = len(string)
  for char in string:
    # 异常检测
    if not is_ascii(char):
      return -1
    bin_string += ("00000000" + str(bin(ord(char)))[2::])[-8::]
  #print(length)
  if length % 5 == 1:
    bin_string += "0"*2
    num = 6
  elif length % 5 == 2:
    bin_string += "0"*4
    num = 4
  elif length % 5 == 3:
    bin_string += "0"
    num = 3
  elif length % 5 == 4:
    bin_string += "0"*3
    num = 1
  bin_list = re.findall(".{5}",bin_string)
  print(bin_list)
  for i in bin_list:
    ciphertext += DIC[int(i,2)]
  return ciphertext + "="*num
def decrypt_base32(ciphertext):
  plaintext = ""
  bin_string = ""
  #print(ciphertext)
  num = ciphertext.count("=")
  print(num)
  if num:
    ciphertext = ciphertext[0:-num:]
  #print(ciphertext)
  for i in ciphertext:
    # 异常检测
    if i not in DIC:
      return -1
    bin_string += ("0"*5 + str(bin(DIC.index(i)))[2::])[-5::]
  bin_list = re.findall(".{8}",bin_string)
  for i in bin_list:
    plaintext += chr(int(i,2))
  return plaintext
if __name__ == '__main__':
  print("Aabc ",encrypt_base32("Aabc"))
  print("Aabc ",decrypt_base32(encrypt_base32("Aabc")))

BASE16

  • 编码对象: ascill字符
  • 原理:
  • 将字符转为8位ascill码,并组合在一起
  • 将字符以每4位一组分割
  • 将4位二进制转为十进制
  • 查表:
数值 字符 数值 字符 数值 字符 数值 字符
0 0 4 4 8 8 12 c
1 1 5 5 9 9 13 d
2 2 6 6 10 a 14 e
3 3 7 7 11 b 15 f

  • 代码
# write in 2021/6/22
import re
DIC = "0123456789ABCDEF"
def is_ascii(char):
  if ord(char) >= 0 and ord(char) <= 127:
    return True
  return False
def encrypt_base16(string):
  bin_string = ""
  ciphertext = ""
  length = len(string)
  for char in string:
    # 异常检测
    if not is_ascii(char):
      return -1
    bin_string += ("00000000" + str(bin(ord(char)))[2::])[-8::]
  #print(length)
  bin_list = re.findall(".{4}",bin_string)
  print(bin_list)
  for i in bin_list:
    ciphertext += DIC[int(i,2)]
  return ciphertext
def decrypt_base16(ciphertext):
  plaintext = ""
  bin_string = ""
  #print(ciphertext)
  for i in ciphertext:
    # 异常检测
    if i not in DIC:
      return -1
    bin_string += ("0"*4 + str(bin(DIC.index(i)))[2::])[-4::]
  bin_list = re.findall(".{8}",bin_string)
  for i in bin_list:
    plaintext += chr(int(i,2))
  return plaintext
if __name__ == '__main__':
  print("Aabc ",decrypt_base16("Aabc"))
  print("Aabc ",decrypt_base16(encrypt_base16("Aabc")))


目录
相关文章
|
3月前
|
存储 XML 数据格式
深入理解Base64编码
【8月更文挑战第20天】
166 0
|
6月前
|
XML 存储 算法
BASE64的算法说明
【5月更文挑战第10天】BASE64的算法说明
57 3
|
机器学习/深度学习 算法 数据可视化
Induction base
Induction base 是一种基于归纳推理的算法或方法,通常用于解决机器学习或数据挖掘中的问题,特别是关联规则挖掘和分类问题。其基本思想是基于一些基本的规则或假设,通过反复应用于数据来推导出更复杂的规则或结论。
38 1
|
存储 索引
Base64编码
通过阅读本篇文章,你可以了解到: 1、Base64 编码的作用 2、Base64 编码的规则 3、Base64 索引表
233 0
Base64编码
|
运维 算法 数据挖掘
Proximity-Base Approaches|学习笔记
快速学习 Proximity-Base Approaches
Proximity-Base Approaches|学习笔记
|
JavaScript PHP
Base64初探
base64是一种编码格式。
934 0
Base64初探