Python 3

求补码

代码作用:
输入一个十进制的整数
输出它的八位二进制补码
其中8可以修改为其他数,例如改成32就会输出32位的二进制数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
n=int(input())
if n >0:
b=bin(n)[2:]
list_b = [0] * (8 - len(b))
list_b=list_b+list(map(int,b))
str_b="".join('%d'%i for i in list_b)
print(str_b)
elif n<0:
b=bin(n)[3:]
list_b=[1]*(8-len(b))
list_b=list_b+list(map(lambda x: (int(x) + 1) % 2, b))
str_b = "".join('%d' % i for i in list_b)
int_b=int(str_b,base=2)+1
ans=bin(int_b)[2:]
print(ans)
else:
print("0"*8)

base加密

代码作用:
读取1.txt中需要加密的明文
输出经过base64加密后的密文到2.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding:utf-8 -*-
import random
from base64 import *

process = {
'16': lambda x: b16encode(x),
'32': lambda x: b32encode(x),
'64': lambda x: b64encode(x),
}
s = ""
with open('1.txt', 'r', encoding='UTF-8') as f: # 读取需要编码的内容
s = "".join(f.readlines()).encode('utf-8')

for i in range(random.randint(10, 20)): # 随机循环
s = process[random.choice(['16', '32', '64'])](s) # 随机编码

with open('2.txt', 'w', encoding='UTF-8') as f: # 保存编码后的结果
f.write(str(s, 'utf-8'))

base解密

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
#-*- coding:utf-8 -*-
import base64

s=''
with open('easybase-4-现代.txt', 'r', encoding='UTF-8') as f:
s="".join(f.readlines()).encode('utf-8')
src=s
while True:
try:
src=s
s=base64.b16decode(s)
str(s,'utf-8')
continue
except:
pass
try:
src=s
s=base64.b32decode(s)
str(s,'utf-8')
continue
except:
pass
try:
src=s
s=base64.b64decode(s)
str(s,'utf-8')
continue
except:
pass
break
with open('321.txt','w', encoding='utf-8') as file:
file.write(str(src,'utf-8'))
print("ok!")

md5解密脚本(已知部分明文和密文)

1
2
3
4
5
6
7
8
9
10
11
12
13
import string
import hashlib
a='s3c{P7?Y0OG?0XPC?ZPK}'
b='b235????f2da???874???63007?4b897'
dic1=string.digits+string.ascii_lowercase+string.ascii_uppercase
for i1 in dic1:
for i2 in dic1:
for i3 in dic1:
bb='s3c{P7'+i1+'Y0OG'+i2+'0XPC'+i3+'ZPK}'
aa=hashlib.md5(bb.encode('utf-8'))
bbb=aa.hexdigest()
if (bbb[:4]=='b235'):
print(i1,i2,i3)

可变换码表base64解密脚本

1