今天抽空把第二题也做出来了,不过python有精度的问题,生成的注册码我也不保证一定正确,随机测试了几组没啥问题,不想用C重写一次了。。。
使用前python要安装opencv库,并将py文件放在第二题的目录执行
[Python] 纯文本查看 复制代码 import re
import cv2
import numpy as np
import hashlib
import hmac
def hmacsha256(key,message):
return hmac.new(key, message, digestmod=hashlib.sha256).hexdigest()
def codeappend(code):
s="".join(code)
fxorl1=(ord(code[0][0])^ord(code[-1][0]))<<8
fmull2=(ord(code[0][1])*ord(code[-1][1]))+fxorl1
fdivl3=(ord(code[0][2])/ord(code[-1][2]))+fmull2
fmodl3=(ord(code[0][2])%ord(code[-1][2]))+fdivl3
smuls1=(ord(code[1][0])*ord(code[-2][0]))<<8
sxors2=(ord(code[1][1])^ord(code[-2][1]))+smuls1
smods3=(ord(code[1][2])%ord(code[-2][2]))+sxors2+32
sdivs3=(ord(code[1][2])/ord(code[-2][2]))+smods3
fmod2=(ord(code[0][1])%ord(code[0][-1]))<<8
sdiv2=(ord(code[1][1])/ord(code[1][-1]))+fmod2
tmul2=(ord(code[2][1])*ord(code[2][-1]))+sdiv2+8
foxor2=(ord(code[3][1])^ord(code[3][-1]))+tmul2
fxorl14=(ord(code[0][0])^ord(code[-1][-1]))<<8
sdivt23=(ord(code[1][1])/ord(code[2][2]))+fxorl14
smult23=(ord(code[1][1])*ord(code[2][2]))+sdivt23
fmodl14=(ord(code[-1][-1])%ord(code[0][0]))+smult23
ret1=str(sdivs3*30+(foxor2*2+fmodl3)*20+fmodl14*72)
ret2=str(23*fmodl3+ 32*sdivs3+42*foxor2+54*fmodl14)
ret3=(25*sdivs3 + 38*foxor2 + 67*fmodl14+fmodl3*18)%20
ret4=(45*sdivs3 + 33*fmodl14 + foxor2*64+fmodl3*12)%20
if ret3>ret4:
ret3,ret4=ret4,ret3
ret5=0
for i in s:
ret5+=int(i)
s+=ret1+ret2+str(ret3)+str(ret4)+str(ret5)
## print s
return s
def genbin(codesum):
img = cv2.imread('./flag.jpg')
rows,cols = img.shape[:2]
M = cv2.getRotationMatrix2D((float(cols)/2,float(rows)/2),codesum,1)
dst = cv2.warpAffine(img,M,(cols,rows))
gray = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY)
gray = np.float64(gray)
res = cv2.resize(gray,(8, 8),fx=0, fy=0, interpolation = cv2.INTER_LINEAR)
r=cv2.dct(res)
result=0.0
s=""
for i in r:
for k in i:
result+=k*0.015625
## print result
for i in r:
for k in i:
if k<result:
s+="0"
else:
s+="1"
## print s
return s
def fullmatch(regex, string, flags=0):
"""Emulate python-3.4 re.fullmatch()."""
return re.match("(?:" + regex + r")\Z", string, flags=flags)
if __name__=='__main__':
name=raw_input()
if fullmatch("(\d{4})-(\d{4})-(\d{4})-(\d{4})",name)!=None and re.search("0000",name)==None:
code=name.split('-')
truename=codeappend(code)
bins=genbin(int(truename[-2:]))
truename+=bins
print hmacsha256(bins, truename)
else:
print "invaild name"
相比第一题,第二题只是在用户名末尾再增加了一串字符串,还有hmac的key变为增加的字符串,其他地方基本一样,具体算法看python代码吧
|