Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.4 >>> from capLetCode import * >>> from gaussJordan2 import * >>> pt = 'POWERS' >>> ct = 'ANDREW' >>> bl = 2 >>> pn = plainToCodelist(pt) >>> pn [ZMod(26)(15), ZMod(26)(14), ZMod(26)(22), ZMod(26)(4), ZMod(26)(17), ZMod(26)(18)] >>> cn = plainToCodelist(ct) >>> cn [ZMod(26)(0), ZMod(26)(13), ZMod(26)(3), ZMod(26)(17), ZMod(26)(4), ZMod(26)(22)] >>> # Av + b >>> pnb = toBlocks(pn, bl) >>> pnb [[ZMod(26)(15), ZMod(26)(14)], [ZMod(26)(22), ZMod(26)(4)], [ZMod(26)(17), ZMod(26)(18)]] >>> for i in range(1, 3): print sub(pnb[i],pnb[0]) [ZMod(26)(7), ZMod(26)(16)] [ZMod(26)(2), ZMod(26)(4)] >>> cnb = toBlocks(cn, bl) >>> cnb [[ZMod(26)(0), ZMod(26)(13)], [ZMod(26)(3), ZMod(26)(17)], [ZMod(26)(4), ZMod(26)(22)]] >>> for i in range(1, 3): print sub(cnb[i], cnb[0]) [ZMod(26)(3), ZMod(26)(4)] [ZMod(26)(4), ZMod(26)(9)] >>> wt = [[ZMod(26)(7), ZMod(26)(16)], [ZMod(26)(2), ZMod(26)(4)]] >>> ct = [[ZMod(26)(3), ZMod(26)(4)], [ZMod(26)(4), ZMod(26)(9)]] >>> at = copyMat(ct) >>> #mxvSolve(wt, at, gj = gauss_jordan) >>> pt = 'POWERT' >>> a = [[1, 16], [2, 1]] >>> w0 = transpose([15, 14]) >>> w0 [[15], [14]] >>> x = mul(a, w0) >>> x [[239], [44]] >>> c0 = transpose([0, 13]) >>> b = sub(c0, x) >>> b [[-239], [-31]] >>> matConvert(b, ZMod(26)) [[ZMod(26)(21)], [ZMod(26)(21)]] >>> b =matConvert(b, ZMod(26)) >>> b [[ZMod(26)(21)], [ZMod(26)(21)]] >>> # [ZMod(26)(15), ZMod(26)(14), ZMod(26)(22), ZMod(26)(4), >>> # ZMod(26)(17), ZMod(26)(19)] >>> pnb = toBlocks([ZMod(26)(15), ZMod(26)(14), ZMod(26)(22), ZMod(26)(4), ZMod(26)(17), ZMod(26)(19)], bl) >>> pnb [[ZMod(26)(15), ZMod(26)(14)], [ZMod(26)(22), ZMod(26)(4)], [ZMod(26)(17), ZMod(26)(19)]] >>> cnb [[ZMod(26)(0), ZMod(26)(13)], [ZMod(26)(3), ZMod(26)(17)], [ZMod(26)(4), ZMod(26)(22)]] >>> a = matConvert(a, ZMod(26)) >>> a [[ZMod(26)(1), ZMod(26)(16)], [ZMod(26)(2), ZMod(26)(1)]] >>> calt = [add(mul(a, p), b) for p in cnb] #WRONG cnb! a, b convert pnb! >>> calt [[[ZMod(26)(21)], [ZMod(26)(8)]], [[ZMod(26)(10)], [ZMod(26)(18)]], [[ZMod(26)(13)], [ZMod(26)(25)]]] >>> >>> # The rest is the correction and addition. ##################### >>> >>> calt = [add(mul(a, p), b) for p in pnb] #NEW! a, b convert pnb! >>> calt # extra level of lists for column matrices [[[ZMod(26)(0)], [ZMod(26)(13)]], [[ZMod(26)(22)], [ZMod(26)(4)]], [[ZMod(26)(17)], [ZMod(26)(19)]]] >>> # Of course for decryption it is A inverse you want! >>> # I could have done it in that direction (using the extra credit)-> >>> # if c = Ap+b, p = Ainverse(c-b) >>> ainv = copyMat(a) >>> invert(a, gj=gauss_jordanMod) >>> pnbalt = [mul(ainv, sub(c, b)) for c in cnb] #NEW! decrypting! >>> pnbalt # extra level of [] du to column matrices: take row 0 or transpose [[[ZMod(26)(15),] [ZMod(26)(14)]], [[ZMod(26)(22)], [ZMod(26)(4)]], [[ZMod(26)(17)], [ZMod(26)(19)]]] >>> pnbalt = [transpose(c)[0] for c in pnbalt] >>> pnbalt [[ZMod(26)(15), ZMod(26)(14)], [ZMod(26)(22), ZMod(26)(4)], [ZMod(26)(17), ZMod(26)(19)]] >>> # These are the numeric pairs for the plaintext. >>> pnalt = sum(pnbalt, []) # quick way to reverse toBlocks!!! >>> pnalt [ZMod(26)(15), ZMod(26)(14), ZMod(26)(22), ZMod(26)(4), ZMod(26)(17), ZMod(26)(19)] >>> print codelistToPlain(pnalt) ANDREW