本文为个人学习笔记,仅供大家参考
【CTF】Python 实例总结
一. 进制转换编码
1. ASCII码
2 10 16 图形
0010 0000 32 20 (空格)(␠)
0010 0001 33 21 !
0010 0010 34 22 "
0010 0011 35 23 #
0010 0100 36 24 $
0010 0101 37 25 %
0010 0110 38 26 &
0010 0111 39 27 '
0010 1000 40 28 (
0010 1001 41 29 )
0010 1010 42 2A *
0010 1011 43 2B +
0010 1100 44 2C ,
0010 1101 45 2D -
0010 1110 46 2E .
0010 1111 47 2F /
0011 0000 48 30 0
0011 0001 49 31 1
0011 0010 50 32 2
0011 0011 51 33 3
0011 0100 52 34 4
0011 0101 53 35 5
0011 0110 54 36 6
0011 0111 55 37 7
0011 1000 56 38 8
0011 1001 57 39 9
0011 1010 58 3A :
0011 1011 59 3B ;
0011 1100 60 3C <
0011 1101 61 3D =
0011 1110 62 3E >
0011 1111 63 3F ?
0100 0000 64 40 @
0100 0001 65 41 A
0100 0010 66 42 B
0100 0011 67 43 C
0100 0100 68 44 D
0100 0101 69 45 E
0100 0110 70 46 F
0100 0111 71 47 G
0100 1000 72 48 H
0100 1001 73 49 I
0100 1010 74 4A J
0100 1011 75 4B K
0100 1100 76 4C L
0100 1101 77 4D M
0100 1110 78 4E N
0100 1111 79 4F O
0101 0000 80 50 P
0101 0001 81 51 Q
0101 0010 82 52 R
0101 0011 83 53 S
0101 0100 84 54 T
0101 0101 85 55 U
0101 0110 86 56 V
0101 0111 87 57 W
0101 1000 88 58 X
0101 1001 89 59 Y
0101 1010 90 5A Z
0101 1011 91 5B [
0101 1100 92 5C \
0101 1101 93 5D ]
0101 1110 94 5E ^
0101 1111 95 5F _
0110 0000 96 60 `
0110 0001 97 61 a
0110 0010 98 62 b
0110 0011 99 63 c
0100 0000 64 40 @
0100 0001 65 41 A
0100 0010 66 42 B
0100 0011 67 43 C
0100 0100 68 44 D
0100 0101 69 45 E
0100 0110 70 46 F
0100 0111 71 47 G
0100 1000 72 48 H
0100 1001 73 49 I
0100 1010 74 4A J
0100 1011 75 4B K
0100 1100 76 4C L
0100 1101 77 4D M
0100 1110 78 4E N
0100 1111 79 4F O
0101 0000 80 50 P
0101 0001 81 51 Q
0101 0010 82 52 R
0101 0011 83 53 S
0101 0100 84 54 T
0101 0101 85 55 U
0101 0110 86 56 V
0101 0111 87 57 W
0101 1000 88 58 X
0101 1001 89 59 Y
0101 1010 90 5A Z
0101 1011 91 5B [
0101 1100 92 5C \
0101 1101 93 5D ]
0101 1110 94 5E ^
0101 1111 95 5F _
0110 0000 96 60 `
0110 0001 97 61 a
0110 0010 98 62 b
0110 0011 99 63 c
0110 0000 96 60 `
0110 0001 97 61 a
0110 0010 98 62 b
0110 0011 99 63 c
0110 0100 100 64 d
0110 0101 101 65 e
0110 0110 102 66 f
0110 0111 103 67 g
0110 1000 104 68 h
0110 1001 105 69 i
0110 1010 106 6A j
0110 1011 107 6B k
0110 1100 108 6C l
0110 1101 109 6D m
0110 1110 110 6E n
0110 1111 111 6F o
0111 0000 112 70 p
0111 0001 113 71 q
0111 0010 114 72 r
0111 0011 115 73 s
0111 0100 116 74 t
0111 0101 117 75 u
0111 0110 118 76 v
0111 0111 119 77 w
0111 1000 120 78 x
0111 1001 121 79 y
0111 1010 122 7A z
0111 1011 123 7B {
0111 1100 124 7C |
0111 1101 125 7D }
0111 1110 126 7E ~
0111 1111 127 7F DEL 删除
https://www.litefeel.com/tools/ascii.php
常见编码
‘a-z’:97~122
‘A-Z’:65~90
’ ':32
‘0-9’:48~57
ord(): 对应字符的ascii码
chr(): 与ord对立
ord():字符转ASCII码,参数为字符。
chr():ASCII码转字符,参数为数字(0-127),总共(0-255)
>>>ord(u'好')
22909
UTF-8和Unicode与GBK的关系
utf-8--------decode(解码)----->>Unicode类型<<-------decode(解码)-----gbk
utf-8<<--------encode(编码)----->>Unicode类型<<-------encode(编码)----->>gbk
utf-8 Unicode ASCII 编码互转
(实在不行,用notepad++转换编码)
#utf-8编码
>>>a = '哈'
>>>a
'\xe5\x93\x88'
#Unicode编码,引号前加u
>>>a = u'哈'
>>>a
u'\u54c8'
#ASCII编码
#Unicode 转ASCII
ord(u'哈')
21704
#ASCII 转 Unicode
>>>unichr(21704)
u'\u54c8'
>>>print(unichr(21704))
哈
#unicode 与 utf-8互转
#encode('utf8')将unicode"编码"为utf8
#decode('utf8')将utf8"解码"为unicode
#注:linux默认编码为unicode,若为其他两种编码进行转换,需unicode作为“媒介”
>>>a = u'哈'
>>>b = a.encode('utf-8')
>>>c = b.decode('utf-8')
>>>c
u'\u54c8'
#gbk转utf8
a = '哈'
b = a.decode('gbk')
c = b.encode('utf-8')
print(c)
Traceback (most recent call last):
File "C:/Users/Nah/Desktop/test_3.py", line 4, in <module>
b = a.decode('gbk')
AttributeError: 'str' object has no attribute 'decode'
???
解码汉字
#按utf-8解码汉字
unicode('哈','utf-8')
u'\u54c8'
#按gbk解码汉字
unicode('哈','gbk')
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeDecodeError: 'gbk' codec can't decode byte 0x88 in position 2: incomplete multibyte sequence
???
>>> help(ord) # ord的帮助文档
Help on built-in function ord in module builtins:
ord(c, /)
Return the Unicode code point for a one-character string.
>>> help(chr)
>Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
>>> ord('a') # 例子
97
>>> chr(97)
'a'
(1) ASCII 码 与 字符串 互转
Python 转换查看
print(ord('a'))
print(chr(97))
97
a
#encoding=utf-8
#py3+
# 用户输入字符
c = input("请输入一个字符: ")
# 用户输入ASCII码,并将输入的数字转为整型
a = int(input("请输入一个ASCII码: "))
print(c + " 的ASCII 码为", ord(c))
print(a, " 对应的字符为", chr(a))
#encoding=utf-8
#无提示输入字符串,获取每个字符的ascii码
a = input()
for i in range(len(a)):
print("ascii of " + a[i] + " is: " + ascii(ord(a[i])))
Aa‘“
ascii of A is: 65
ascii of a is: 97
ascii of ‘ is: 8216
ascii of “ is: 8220
#encoding=utf-8
c = input("Please input a char: ")
a = int(input("Please input a ascii:"))
while True:
if a < 0:
print("ascii is wrong, Plese try again")
a = int(input("Please input a ascii:"))
elif a > 1000:
print("ascii is wrong, Plese try again")
a = int(input("Please input a ascii:"))
else:
break
print(" this is a ascii test")
print("assic is:",ord(c))
print(" char is:", chr(a))
高级版
# !/usr/bin/python coding=utf-8
flag = 1
while flag:
print(">>==============================================<<")
print("mode choose:")
print(" 0: 字符串转换为ASCII码\n 1: ASCII码转换为字符串 \n 退出: control+c")
print(">>==============================================<<")
mode = input("请输入模式: ")
# print(type(mode))
if mode == "0":
print("字符串转换为ASCII码 \n")
string = input("请输入字符串(python2 带引号输入): \n")
strtable = []
asciistring = "["
length = len(string)
print("字符串长度为(length is : )" + str(length))
print(">>>>>>>>>change to ASCII>>>>>>>>>")
i = 0
for char in string:
print("当前字符序号:" + str(i))
print('当前字符 : ', char)
tmp_ASCII = ord(char)
print("当前字符的ASCII码为: ", tmp_ASCII)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# tmp_ASCII = ord(char)
strtable.append(tmp_ASCII)
if i < (length - 1):
asciistring = asciistring + str(tmp_ASCII) + ","
if i == (length - 1):
asciistring = asciistring + str(tmp_ASCII) + "]"
i = i + 1
print("********字符串转换为ASCII码***********")
print(asciistring)
print("***********************************")
elif mode == "1":
string = input("请输入ASCII码(0~256,逗号分隔): ")
print(string)
# print(type(string))
asc = string.split(',')
print(asc)
length = len(asc)
print("list 长度为(length is : )" + str(length))
string_out = "\""
i = 0
for asc_num in asc:
if int(asc_num) < 256:
print("当前 ASCII 序号:" + str(i))
print('当前 ASCII 码为: ', int(asc_num))
tmp_CHAR = chr(int(asc_num))
print("当前 ASCII码 对应的字符的为: ", tmp_CHAR)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
if i < (length - 1):
string_out = string_out + tmp_CHAR
if i == (length - 1):
string_out = string_out + tmp_CHAR + "\""
i = i + 1
else:
print("ASCII error: ", asc_num)
print("*******ASCII码转换为字符串*********")
print(string_out)
print("***********************************")
else:
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print("重新输入,或退出:control+c")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
(2) ASCII码 与 中文 互转
name = r"\u6697\u88d4\u5251\u9b54"
print(name.encode('ascii').decode('unicode_escape'))
# encode()可以添加'ascii',也可以不加
print(name.encode().decode('unicode_escape'))
暗夜剑魔
二. 字符操作
1. 字符反转,倒序输出
(1)字符串切片
s = "123ABC"
s = s[::-1]
print(s)
def invert(x):
return x[::-1]
print(invert("123ABC"))
(2)reverse
s="123ABC"
print("".join(reversed(s)))
s = "123ABC"
l = list(s)
l.reverse()
print("".join(l))
s = "123ABC"
l = list(s)
print("".join(l[::-1]))
(3)reduce
s="123ABC"
from functools import reduce
result = reduce(lambda x,y:y+x,s)
print(result)
//Python3 ,这个函数从全局命名空间中移除,放在了 functools模块
(4)递归函数
s="123ABC"
def func(s):
if len(s) <1:
return s
return func(s[1:])+s[0]
result = func(s)
print(result)
(5) 栈
s="123ABC"
def func(s):
l = list(s) #模拟全部入栈
result = ""
while len(l)>0:
result += l.pop() #模拟出栈
return result
result = func(s)
print(result)
(6)for循环
s = "123ABC"
print("".join([s[-i] for i in range(1, len(s) + 1)]))
s="123ABC"
def func(s):
result = ""
max_index = len(s)-1
for index,value in enumerate(s):
result += s[max_index-index]
return result
result = func(s)
print(result)