ctfshow刷题记录-cry方向-1

打印 上一主题 下一主题

主题 891|帖子 891|积分 2675

0x00

标题泉源:ctfshow 菜狗杯 crypto方向 base47
标题描述:
神必字符: E9CVT+HT5#X36RF4@LAU703+F$E-0N$@68LMXCVDRJJD5@MP#7MUZDTE?WWLG1S#L@+66H@59KTWYK8TW0RV
神必字典:
0123456789ABCDEFGHJKLMNPQRSTUVWXYZ?!@#$%^&*-+
0x01

第一次做这种base换表的标题,在网上查了查相干wp,感觉自己对base家族还不太熟悉,于是自己先用py写了个base64的加解密的脚本,代码如下:
  1. string1 = "E9CV^T+HT5#X36RF4@LAU703+F$E-0N$@68LMXCVDRJJD5@MP#7MUZDTE?WWLG1S#L@+^66H@59KTWYK8TW0RV"
  2. test_string = "abc123"
  3. dict1 = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ?!@#$%^&*-+"
  4. def base64_encode(test_string):
  5.     dict2 = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")# base64表
  6.     temp_string = str()
  7.     for i in test_string:  # 字符->二进制ascii码, 放到temp_string里
  8.         temp_string += bin(ord(i))[2:].zfill(8)
  9.     temp_list = list()
  10.     if len(test_string) % 3 == 1:
  11.         temp_string += "0000"
  12.     if len(test_string) % 3 == 2:
  13.         temp_string += "00"
  14.     while temp_string!='':
  15.         temp_list.append(temp_string[0:6])
  16.         temp_string=temp_string[6:]
  17.     fina_list = list()  # 以这些6位的二进制数值查找base表,存到fina_list中
  18.     for i in range(0, len(temp_list)):
  19.         fina_list.append(dict2[int(temp_list[i], 2)])
  20.     fina_string = str()
  21.     for i in fina_list:  # 转化成字符串fina_string
  22.         fina_string +=  i
  23.     if len(test_string) % 3 == 1:
  24.         fina_string += "=="
  25.     if len(test_string) % 3 == 2:
  26.         fina_string += "="
  27.     return fina_string
  28. '''print("asfaegqfa123:", base64_encode("asfaegqfa123"), "YXNmYWVncWZhMTIz"==base64_encode("asfaegqfa123"))
  29. print("asfaegqfa12:", base64_encode("asfaegqfa12"), "YXNmYWVncWZhMTI="==base64_encode("asfaegqfa12"))
  30. print("asfaegqfa1231:", base64_encode("asfaegqfa1231"), "YXNmYWVncWZhMTIzMQ=="==base64_encode("asfaegqfa1231"))'''
  31. def base64_decode(string):
  32.     #dict2 = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ?!@#$%^&*-+"
  33.     dict2="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  34.     list2 = list(dict2)
  35.     temp_string=str()
  36.     if string[-1] == "=" and string[-2] == "=":
  37.         string = string[0 : len(string) - 2]
  38.     if string[-1] == "=" and string[-2] != "=":
  39.         string = string[0 : len(string) - 1]
  40.     for i in string:  # 字符转二进制码
  41.         temp_string+=str(bin(list2.index(i))[2:].zfill(6))
  42.     length=len(temp_string)
  43.     if length%8==2:
  44.         temp_string=temp_string[0:length-2]
  45.     if length%8==4:
  46.         temp_string=temp_string[0:length-4]
  47.     fina_list=list()
  48.     while temp_string!='':   #每八位分一组  放入fina_list中
  49.         fina_list.append(temp_string[0:8])
  50.         temp_string=temp_string[8:]
  51.     fina_string=str()
  52.     for i in fina_list:
  53.         fina_string+=chr(int(i,2))
  54.     return fina_string
  55. '''print("asfaegqfa123"==base64_decode("YXNmYWVncWZhMTIz"))
  56. print("asfaegqfa12" == base64_decode("YXNmYWVncWZhMTI="))
  57. print("asfaegqfa1231"==base64_decode(""))'''
  58. `
复制代码
厥后问了问王师傅,基本明白了base的本质其实就是进制转换,每个字符串都对应一串256进制的数字(一个字符都是8bit,用ascii解码方式)   回过头来看标题,这个其实就是base45加密的字符串,思绪应该是密文中每个字符对应字典的下标这一串数字是len(字典)进制的,先转成10进制,再转ascii码字符串即可,脚本代码如下:
  1. import libnum
  2. cipher = "E9CV^T+HT5#X36RF4@LAU703+F$E-0N$@68LMXCVDRJJD5@MP#7MUZDTE?WWLG1S#L@+^66H@59KTWYK8TW0RV"
  3. key = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ?!@#$%^&*-+"
  4. sum=0
  5. for i in range(len(cipher)):
  6.     sum+=key.index(cipher[i])*pow(len(key),len(cipher)-1-i)
  7. flag=libnum.n2s(sum)
  8. print(flag)
复制代码
0x02

由这道题引发的思索,可不可以做一个base任意数字的编码,由上述原理写出了一个basexx加解密的脚本,代码如下:
  1. import libnum
  2. def base_xx_decode(cipher):
  3.     # key为任意映射表
  4.     # key = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ?!@#$%^&*-+"  
  5.     key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"  #base64表
  6.     sum = 0
  7.     for i in range(len(cipher)):
  8.         sum += key.index(cipher[i]) * pow(len(key), len(cipher) - 1 - i)  #len(key)进制转10进制
  9.     flag = libnum.n2s(sum)    #ascii值转字符串
  10.     return flag
  11. print(base_xx_decode("YWJjMTIz"))       # just an example  =='abc123'
  12. def base_xx_encode(plaintext):
  13.     key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  14.     beichushu = libnum.s2n(plaintext)
  15.     cipher=str()
  16.     chushu = len(key)   #给chushu yushu shang beichushu赋初值  然后辗转相除法进行10进制转len(key)进制,并直接映射到cipher里
  17.     yushu = beichushu % chushu
  18.     shang = beichushu // chushu
  19.     beichushu = shang
  20.     cipher += key[yushu]
  21.     while shang != 0:
  22.         yushu = beichushu % chushu
  23.         shang = beichushu // chushu
  24.         beichushu = shang
  25.         cipher += key[yushu]
  26.     cipher=cipher[::-1]
  27.     return cipher
  28. print(base_xx_encode("abc123"))  # just an example   =='YWJjMTIz'
复制代码
这个脚本只能加解密无添补规则且ascii编码的那种,比如base64就是无“=”

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南七星之家

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表