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")))


目录
相关文章
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 向指定用户或终端发送消息 write命令 使用指南
【Shell 命令集合 网络通讯 】Linux 向指定用户或终端发送消息 write命令 使用指南
305 0
|
监控 关系型数据库 数据库
|
关系型数据库 MySQL 数据库
tushare宏观数据使用pandas入库,增加唯一索引
1,对pandas入数据库 pandas代码中自带了to_sql的方法可以直接使用。 但是数据字段是text的,需要修改成 varchar的,否则不能增加索引。 在增加了主键之后可以控制数据不能再增量修改了。 增加:dtype类型可以解决 dtype={col_name: NVARCHAR(length=255) for col_name in dat
3704 0
Idea单步调试快速跳过后面的断点-Mute Breakpoints 快速清空所有的断点
https://zhengyz.blog.csdn.net/article/details/128072266?spm=1001.2014.3001.5502
Idea单步调试快速跳过后面的断点-Mute Breakpoints 快速清空所有的断点
|
7月前
|
Web App开发 安全 Linux
【独家揭秘2025】VMware Workstation Pro虚拟机:免费安装教程大放送,一键解锁操作系统模拟神器!
VMware Workstation Pro 是由威睿(VMware)公司开发的一款功能强大的桌面虚拟化软件,允许用户在同一台物理计算机上同时运行多个操作系统,如Windows、..
487 2
【独家揭秘2025】VMware Workstation Pro虚拟机:免费安装教程大放送,一键解锁操作系统模拟神器!
|
8月前
|
人工智能 运维 应用服务中间件
OS Copilot测评报告
作为一名全栈开发,我在日常维护阿里云服务器时遇到了不少Linux操作难题。最近尝试了阿里云推出的OS Copilot,基于大模型的AI助手,大大简化了运维工作。通过简单的对话式命令,如“co nginx是否安装”和“co 将nginx设置为开启自启动 -t”,轻松完成任务。甚至可以通过文件定义复杂任务,如解析日志并提取攻击IP。OS Copilot显著提升了效率,降低了学习成本,真是运维利器!
145 24
|
11月前
|
人工智能 运维 安全
智慧工地:建筑热潮退去后的挑战与应对策略
智慧工地作为建筑业数字化转型的重要趋势之一,将共同推动建筑行业向更加高效、安全、可持续的方向发展
183 5
|
存储 缓存 分布式计算
|
安全 Java 数据库连接
Spring Boot 优雅关机时异步线程安全优化
Spring Boot 优雅关机时异步线程安全优化
374 1
|
存储 Kubernetes 关系型数据库
Kubernetes(K8S) 安装Nacos,报 No DataSource set
Kubernetes(K8S) 安装Nacos,报 No DataSource set
183 0