Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168693
Image: ubuntu2004
# set up Key, PlainText("hold") and find Cipher d={0:"a",1:"b",2:"c",3:"d",4:"e",5:"f", 6:"g",7:"h",8:"i",9:"j",10:"k",11:"l", 12:"m", 13:"n",14:"o",15:"p",16:"q", 17:"r",18:"s",19:"t",20:"u",21:"v", 22:"w",23:"x",24:"y",25:"z"} a={"a":0,"b":1,"c":2,"d":3,"e":4,"f":5, "g":6,"h":7,"i":8,"j":9,"k":10,"l":11, "m":12, "n":13,"o":14,"p":15,"q":16, "r":17,"s":18,"t":19,"u":20,"v":21, "w":22,"x":23,"y":24,"z":25} K=Matrix([[3,10],[5,7]]) print "key:" print K print print "plaintext:" print P print P=Matrix([[a["h"],a["o"]],[a["l"],a["d"]]]) C=(P*K) C=C.mod(26) print "Ciphertext:" print C print # Compute K inverse Mod(K.determinant(),26) Di=Mod(Mod(K.determinant(),26)^-1,26) Ki=Di*Matrix([[7,-10],[-5,3]]) print "Compute K inverse:" print Ki print # Confirm that Ki is indeed the inverse of K print "Confirm that Ki is indeed the inverse of K: " print K*Ki print # Get the plaintext from the cipher text usin # Ki P=C*Ki print "Get the plaintext from the cipher text using K:" print (d[P[0][0]],d[P[0][1]],d[P[1][0]],d[P[1][1]]) print # Crack the hill cipher K=Matrix([[3,10],[5,7]]) P=Matrix([[a["h"],a["i"]],[a["l"],a["l"]],[a["c"],a["i"]],[a["p"],a["h"]],[a["e"],a["r"]]]) C=(P*K).mod(26) print "Crack the hill cipher:" print C print Y=Matrix([[9,22],[10,5]]) X=Matrix([[a["h"],a["i"]],[a["l"],a["l"]]]) print "Crack the hill cipher:" print X print # Compute X inverse Di=Mod(Mod(X.determinant(),26)^-1,26) Xi=Di*Matrix([[11,-8],[-11,7]]) print "Compute X inverse" print Xi*X print print "Compute the Key" print Xi*Y
key: [ 3 10] [ 5 7] plaintext: [ 7 8] [11 11] [ 2 8] [15 7] [ 4 17] Ciphertext: [13 12] [22 1] Compute K inverse: [15 12] [19 25] Confirm that Ki is indeed the inverse of K: [1 0] [0 1] Get the plaintext from the cipher text using K: ('h', 'o', 'l', 'd') Crack the hill cipher: [ 9 22] [10 5] [20 24] [ 2 17] [19 3] Crack the hill cipher: [ 7 8] [11 11] Compute X inverse [1 0] [0 1] Compute the Key [ 3 10] [ 5 7]