-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
90 lines (70 loc) · 2.41 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import StringIO
import os
import sys
import math
ACSII_A = 65
ACSII_Z = 90
ACSII_a = 97
ACSII_z = 122
def encode_ascii(content, encode_key):
new_content = ''
for x in xrange(0,len(content)):
value = ord(content[x])
key_index = x % len(encode_key)
key_bit_value = ord(encode_key[key_index])
delt_value = 0
if key_bit_value >= ACSII_a and key_bit_value <= ACSII_z:
delt_value = key_bit_value - ACSII_a
pass
elif key_bit_value >= ACSII_A and key_bit_value <= ACSII_Z:
delt_value = key_bit_value - ACSII_A
pass
pass
if value >= ACSII_a and value <= ACSII_z:
value = value + delt_value
if value > ACSII_z:
value = ACSII_a + (value - ACSII_z - 1)
pass
elif value >= ACSII_A and value <= ACSII_Z:
value = value + delt_value
if value > ACSII_Z:
value = ACSII_A + (value - ACSII_Z - 1)
pass
new_content = new_content + str(chr(value))
return new_content
def decode_ascii(content, encode_key):
new_content = ''
for x in xrange(0,len(content)):
value = ord(content[x])
key_index = x % len(encode_key)
key_bit_value = ord(encode_key[key_index])
delt_value = 0
if key_bit_value >= ACSII_a and key_bit_value <= ACSII_z:
delt_value = key_bit_value - ACSII_a
pass
elif key_bit_value >= ACSII_A and key_bit_value <= ACSII_Z:
delt_value = key_bit_value - ACSII_A
pass
pass
if value >= ACSII_a and value <= ACSII_z:
value = value - delt_value
if value < ACSII_a:
value = ACSII_z - (ACSII_a - value - 1)
pass
elif value >= ACSII_A and value <= ACSII_Z:
value = value - delt_value
if value < ACSII_A:
value = ACSII_Z - (ACSII_A - value - 1)
pass
new_content = new_content + str(chr(value))
return new_content
if __name__ == '__main__':
func_type = sys.argv[1]
content = sys.argv[2]
key = sys.argv[3]
if func_type == 'encode':
print(encode_ascii(content, key))
elif func_type == 'decode':
print(decode_ascii(content, key))
# usage
# python code.py encode abcdefghijklmnopqrstuvwxyz test