BASE系列

BASE64

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

    • 将组合好的字符串以6位一组拆分

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

      数值字符数值字符数值字符数值字符
      0A16Q32g48w
      1B17R33h49x
      2C18S34i50y
      3D19T35j51z
      4E20U36k520
      5F21V37l531
      6G22W38m542
      7H23X39n553
      8I24Y40o564
      9J25Z41p575
      10K26a42q586
      11L27b43r597
      12M28c44s608
      13N29d45t619
      14O30e46u62+
      15P31f47v63/
    • 注意,最后面的数为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

    • 将组合后的串以每五位分割开来

    • 将五位的二进制转为十进制,在查表:

      数值字符数值字符数值字符数值字符
      0A8I16Q24Y
      1B9j17R25Z
      2C10K18S262
      3D11L19T273
      4E12M20U284
      5F13N21V295
      6G14O22W306
      7H15P23X317
    • 注意:最后的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位二进制转为十进制

    • 查表:

      数值字符数值字符数值字符数值字符
      00448812c
      11559913d
      226610a14e
      337711b15f
    • 代码

      # 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")))
      
      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值