[Tutor] i dont understand this code

Alan Gauld alan.gauld at btinternet.com
Sat Feb 22 20:03:53 CET 2014


On 22/02/14 12:26, piyush joshi wrote:
> Can anyone help me out in understanding this code

Sure, but it's an quite a lot of code to describe line by line.

Is there anything specific you need help with?

> import os
>
> rotationMatrix1 = [7, 1, 5, 3, 0, 6, 2, 5, 2, 3, 0, 6, 1, 7, 6, 1, 5, 2,
> 7, 1, 0, 3, 7, 6, 1, 0, 5, 2, 1, 5, 7, 3, 2, 0, 6]
>
> rotationMatrix2 = [1, 6, 2, 5, 7, 3, 0, 7, 1, 6, 2, 5, 0, 3, 0, 6, 5, 1,
> 1, 7, 2, 5, 2, 3, 7, 6, 2, 1, 3, 7, 6, 5, 0, 1, 7]
>
> keyboardDict = {  2: '1',  3: '2',  4: '3',  5: '4',  6: '5',  7: '6',
> 8: '7',  9: '8', 10: '9', 11: '0',
>                   16: 'q', 17: 'w', 18: 'e', 19: 'r', 20: 't', 21: 'y',
> 22: 'u', 23: 'i', 24: 'o', 25: 'p',
>                   30: 'a', 31: 's', 32: 'd', 33: 'f', 34: 'g', 35: 'h',
> 36: 'j', 37: 'k', 38: 'l',
>                   44: 'z', 45: 'x', 46: 'c', 47: 'v', 48: 'b', 49: 'n',
> 50: 'm' }
>
> def keyboardEncToAscii(inKey):
>                  out = ""
>
>                  for c in inKey:
>                                  if c == 0: return out
>                                  if c in keyboardDict: out +=
> keyboardDict[c]
>                                  else: return ""
>                  return out
>
> def decryptHash(hash, key, rotationMatrix):
>                  outhash = []
>                  for i in range(0, len(hash)):
>                                  outhash.append(((hash[i] <<
> (rotationMatrix[7*key+i])) & 0xFF) | (hash[i] >>
> (8-rotationMatrix[7*key+i])))
>                  return outhash
>
> print("")
> print("hexa decimal code")
> print("hexadecimal code from which the password can be calculated,")
> print("07088120410C0000")
> print("")
> print("Please enter the code: ")
> code = raw_input()
> hash = []
> for i in range(1, len(code) // 2):
>                  hash.append(int(code[2*i]+code[2*i+1],16))
> key = int(code[0:2], 16) % 5
>
> password = keyboardEncToAscii(decryptHash(hash, key, rotationMatrix1))
> if password == "":
>                  password = keyboardEncToAscii(decryptHash(hash, key,
> rotationMatrix2))
> if password == "":
>                  print("The password could not be calculated. Bummer.")
> else:
>                  print("The password is: " + password)
> if (os.name == 'nt'):
>                  print("Press a key to exit...")
>                  raw_input()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list