python中的ord,chr函数

python中的ord,chr函数

通过help 查看相关函数的帮助文档

help (chr)

chr(…)

chr(i) -> character    

Return a string of one character with ordinal i; 0 <= i < 256.

参数是0 - 256 的一个整数,返回值是当前整数对应的ascii字符。参数可以是10进制也可以是16进制的形式

十六进制:

print chr(0x30), chr(0x31), chr(0x61)
0 1 a
十进制:

print chr(48), chr(49), chr(97)
0 1 a
unichr(…)

unichr(i) -> Unicode character    
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.

unichr()和chr()函数功能基本一样, 只不过是返回unicode的字符

ord(…)
ord(c) -> integer
Return the integer ordinal of a one-character string.

参数是一个ascii字符,返回值是对应的十进制整数

print ord(‘a’), ord(‘0’), ord(‘1’)
97 48 49
print “%x %x %x” % (ord(‘a’), ord(‘0’), ord(‘1’))
61 30 31
print “%#x %#x %#x” % (ord(‘a’), ord(‘0’), ord(‘1’))
0x61 0x30 0x31
通过chr()和ord()联合起来使用,我们就可以对字符串进行相关运算的转换

比如一个字符串str1,转化成另一个字符串str2, 使得 str2[i] = str1[i] - i

str1 = “eb;3ej8h”

for i in range(0, len(str1)):
… print chr((ord(str1[i])-i)),

e a 9 0 a e 2 a

FindKey writeup

题目:FLAG就是你输入的key

点击下载 附件

下载之后发现居然不知道是什么格式的,这怎么反编译,最后在Ubuntu里执行file命令发现是python写的。

findkey: python 2.7 byte-compiled

将后缀修改为.pyc直接使用http://tool.lu/pyc进行反编译。

!/usr/bin/env python
 encoding: utf-8
 如果觉得不错,可以推荐给你的朋友!http://tool.lu/pyc
import sys

lookup = [
196,
153,
149,
206,
17,
221,
10,
217,
167,
18,
36,
135,
103,
61,
111,
31,
92,
152,
21,
228,
105,
191,
173,
41,
2,
245,
23,
144,
1,
246,
89,
178,
182,
119,
38,
85,
48,
226,
165,
241,
166,
214,
71,
90,
151,
3,
109,
169,
150,
224,
69,
156,
158,
57,
181,
29,
200,
37,
51,
252,
227,
93,
65,
82,
66,
80,
170,
77,
49,
177,
81,
94,
202,
107,
25,
73,
148,
98,
129,
231,
212,
14,
84,
121,
174,
171,
64,
180,
233,
74,
140,
242,
75,
104,
253,
44,
39,
87,
86,
27,
68,
22,
55,
76,
35,
248,
96,
5,
56,
20,
161,
213,
238,
220,
72,
100,
247,
8,
63,
249,
145,
243,
155,
222,
122,
32,
43,
186,
0,
102,
216,
126,
15,
42,
115,
138,
240,
147,
229,
204,
117,
223,
141,
159,
131,
232,
124,
254,
60,
116,
46,
113,
79,
16,
128,
6,
251,
40,
205,
137,
199,
83,
54,
188,
19,
184,
201,
110,
255,
26,
91,
211,
132,
160,
168,
154,
185,
183,
244,
78,
33,
123,
28,
59,
12,
210,
218,
47,
163,
215,
209,
108,
235,
237,
118,
101,
24,
234,
106,
143,
88,
9,
136,
95,
30,
193,
176,
225,
198,
197,
194,
239,
134,
162,
192,
11,
70,
58,
187,
50,
67,
236,
230,
13,
99,
190,
208,
207,
7,
53,
219,
203,
62,
114,
127,
125,
164,
179,
175,
112,
172,
250,
133,
130,
52,
189,
97,
146,
34,
157,
120,
195,
45,
4,
142,
139]

pwda = [
188,
155,
11,
58,
251,
208,
204,
202,
150,
120,
206,
237,
114,
92,
126,
6,
42]

pwdb = [
53,
222,
230,
35,
67,
248,
226,
216,
17,
209,
32,
2,
181,
200,
171,
60,
108]

flag = raw_input('Input your Key:').strip()
if len(flag) != 17:
print 'Wrong Key!!'
sys.exit(1)
flag = flag[::-1]
for i in range(0, len(flag)):
if ord(flag[i]) + pwda[i] & 255 != lookup[i + pwdb[i]]:
    print 'Wrong Key!!'
    sys.exit(1)

print 'Congratulations!!'

反编译之后写出脚本

#!/usr/bin/env python 
# encoding: utf-8 
# 访问 http://tool.lu/pyc/ 查看更多信息 
import sys 
lookup = [196,153,149,206,17,221,10,217,167,18,36,135, 
103,61,111,31,92,152,21,228,105,191,173,41,2,245, 
23,144,1,246,89,178,182,119,38,85,48,226,165,241, 
166,214,71,90,151,3,109,169,150,224,69,156,158,57, 
181,29,200,37,51,252,227,93,65,82,66,80,170,77,49, 
177,81,94,202,107,25,73,148,98,129,231,212,14,84, 
121,174,171,64,180,233,74,140,242,75,104,253,44,39, 
87,86,27,68,22,55,76,35,248,96,5,56,20,161,213,238, 
220,72,100,247,8,63,249,145,243,155,222,122,32,43, 
186,0,102,216,126,15,42,115,138,240,147,229,204, 
117,223,141,159,131,232,124,254,60,116,46,113,79, 
16,128,6,251,40,205,137,199,83,54,188,19,184,201, 
110,255,26,91,211,132,160,168,154,185,183,244,78, 
33,123,28,59,12,210,218,47,163,215,209,108,235,237, 
118,101,24,234,106,143,88,9,136,95,30,193,176,225, 
198,197,194,239,134,162,192,11,70,58,187,50,67,236, 
230,13,99,190,208,207,7,53,219,203,62,114,127,125, 
164,179,175,112,172,250,133,130,52,189,97,146,34, 
157,120,195,45,4,142,139] 
pwda = [188,155,11,58,251,208,204,202,150,120,206,237,114,92,126,6,42] 
pwdb = [53,222,230,35,67,248,226,216,17,209,32,2,181,200,171,60,108] 

flag='' 
for i in range(0, 17): 
flag=chr( lookup[i + pwdb[i]] - pwda[i] & 255 )+flag 
print flag 
文章目录
  1. 1. python中的ord,chr函数
    1. 1.1. FindKey writeup
|