1 Star 1 Fork 2

Franky/ctf-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hill.py 2.18 KB
一键复制 编辑 原始数据 按行查看 历史
truongkma 提交于 2015-10-14 09:12 . ctf tools
# Encrypt and decrypt a clear/ciphertext with the Hill Cipher
# GPL C polym
# Uses: http://stackoverflow.com/questions/4287721/easiest-way-to-perform-modular-matrix-inversion-with-python#answer-4293123
import numpy as np
global debug
debug=0
def modMatInv(A,p): # Finds the inverse of matrix A mod p
n=len(A)
A=np.matrix(A)
adj=np.zeros(shape=(n,n))
for i in range(0,n):
for j in range(0,n):
adj[i][j]=((-1)**(i+j)*int(round(np.linalg.det(minor(A,j,i)))))%p
return (modInv(int(round(np.linalg.det(A))),p)*adj)%p
def modInv(a,p): # Finds the inverse of a mod p, if it exists
for i in range(1,p):
if (i*a)%p==1: return i
raise ValueError(str(a)+" has no inverse mod "+str(p))
def minor(A,i,j): # Return matrix A with the ith row and jth column deleted
A=np.array(A)
minor=np.zeros(shape=(len(A)-1,len(A)-1))
p=0
for s in range(0,len(minor)):
if p==i: p=p+1
q=0
for t in range(0,len(minor)):
if q==j: q=q+1
minor[s][t]=A[p][q]
q=q+1
p=p+1
return minor
def encrypt(msg, key, sz):
triple = [list(msg[i*sz:(i*sz)+sz]) for i in range(0, len (msg)/sz)]
if debug>0: print triple
mul = [i[:] for i in triple]
for x in range(len(triple)):
for i in range(len(triple[x])):
triple[x][i]=ord(triple[x][i])-65
if debug>0: print triple
for x in range(len(triple)):
mul[x] = np.dot(key,triple[x]) % 26
if debug>0: print mul
enc=""
for x in range(len(mul)):
for s in range(0,sz): enc+=chr(mul[x][s]+65)
return enc
def decrypt(msg, key, sz):
try: deckey = modMatInv(key,26)
except ValueError: return
triple = [list(msg[i*sz:(i*sz)+sz]) for i in range(0, len (msg)/sz)]
mul = [i[:] for i in triple]
for x in range(len(triple)):
for i in range(len(triple[x])):
triple[x][i]=ord(triple[x][i])-65
if debug>0: print triple
for x in range(len(triple)):
mul[x] = np.dot(deckey,triple[x]) % 26
if debug>0: print mul
dec=""
for x in range(len(mul)):
for s in range(0,sz): dec+=chr(int(mul[x][s])+65)
return dec
encr="DKBRHSBTJVPHFKYJASSTZBOCOXXMRCKTNRJGGRSCBQJBRZVKPGAJOVANNEIHIKITIHKHUOFYISZBOCSIGEXQZYWEFANOAVEWQBJNYVUUYEAOIRRSWY"
correctkey = [[3,2,7], [4,5,6], [1,9,8]]
correctkey= np.transpose(correctkey)
sz=len(correctkey)
print decrypt(encr,correctkey,sz)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kuorumin/ctf-tools.git
git@gitee.com:kuorumin/ctf-tools.git
kuorumin
ctf-tools
ctf-tools
master

搜索帮助