N = 13
S=[[0 for col in range(N)] for row in range(N)]
for i in range(N):
for j in range(N):
if j<len(sentences[i]):
S[i][j]=ord(sentences[i][j])
key='bbs.pediy.com'
M=Matrix(S)
print 'M is:\n',M #M.rank()
X=vector(map(ord,key))
print 'Key is \''+ key +'\''
enc=''.join(['%#6x'%t for t in M*X]).replace('0x','')
print 'Encrypted text is:\n',enc
y=[]
for i in range(0,len(enc),5):
y.append(int(enc[i:i+5],16))
Y=vector(y)
print 'Key is \''+''.join(map(chr,M.solve_right(Y)))+'\''
def crc64(key):
register = 0
for x in map(ord,key):
idx = (x ^ register ) & 0xff
register = (register >> 8) ^ Qtable[idx]
return long(register)
def crc64_(k):
N = 9 # len of 'bbs.pediy'
M = MatrixSpace(GF(2), 64, N * 7) #每个ascii字符只需要知道7位
X = VectorSpace(GF(2), 63)
base = crc64("\x00" * N)
diffs = {}
for i in range(N):
for j in range(7):
key = [0]*N
key[i] = key[i] | (1 << j)
diffs[i, j] = crc64(''.join(map(chr,key))) ^ base
matrix = M()
for (i, j), vec in diffs.items():
column = i * 7 + 6-j
for row in range(64):
matrix[row, column] = (vec & (1 << row)) >> row
xstr= ''.join(bin(ord(c)).replace('0b','').rjust(7,'0') for c in k)
x = X([int(n) for n in xstr])
v = matrix*x
vstr=''.join([repr(n) for n in v])[::-1]
return int(vstr,2) ^ base
#-*- coding:utf-8 -*-
import os
from sage.all import *
os.environ.__setitem__('SAGE_ROOT', '/home/ty/sage-6.9')
os.environ.__setitem__('PATH', os.environ.__getitem__('SAGE_ROOT') +':' + os.environ.__getitem__('PATH'))
os.environ.__setitem__('LD_LIBRARY_PATH',os.environ.__getitem__('SAGE_ROOT')+'/local/lib')
Qtable = [0x0,...,0x29ddad7541fdeec0, 0x9877abefb2bdafc1L]
N = 9 # len of key
M = MatrixSpace(GF(2), 64, N * 7) #每个ascii字符只需要知道7位
V = VectorSpace(GF(2), 64)
def crc64(key):
register = 0
for x in map(ord,key):
idx = (x ^ register ) & 0xff
register = (register >> 8) ^ Qtable[idx]
return long(register)
base = crc64("\x00" * N)
diffs = {}
for i in range(N):
for j in range(7):
key = [0]*N
key[i] = key[i] | (1 << j)
diffs[i, j] = crc64(''.join(map(chr,key))) ^ base
matrix = M()
for (i, j), vec in diffs.items():
column = i * 7 + 6-j
for row in range(64):
matrix[row, column] = (vec & (1 << row)) >> row
DIFFERENCE = 0x82a320d616b5ef03L ^ base #hex(crc64('bbs.pediy'))
v = V( [(DIFFERENCE & (1 << row)) >> row for row in range(64)] )
x = matrix.solve_right(v)
# s = [0] * N
# for i, v in enumerate(x):
# i, j = divmod(i, 7)
# if v:
# s[i] |= (1 << (6-j))
# key = "".join(map(chr, s))
xstr=''.join([repr(n) for n in x])
key=''
for i in range(0,len(xstr),7):
key+=chr(int(xstr[i:i+7],2))
print key #bbs.pediy