Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. # Encryption class for encrypting login info
  2. # 81 lines
  3. from Crypto.Cipher import AES
  4. import base64
  5. import os
  6. import hashlib
  7. import binascii
  8.  
  9.  
  10. class LoginEncoding:
  11. def __init__(self):
  12. self.username = ''
  13. self.usernamePlain = ''
  14. self.password = ''
  15. self.key = ''
  16. self.salt = ''
  17. self.dateTime = time.strftime("%I:%M:%S")
  18.  
  19. def setUsername(self, username):
  20. if len(username) == 0:
  21. print('The username passed in is empty or null!')
  22.  
  23. self.usernamePlain = username
  24. encryptedUsername = loginEncryption(self.usernamePlain)
  25. self.username = encryptedUsername
  26.  
  27. def setPassword(self, password):
  28. errorCheckPassword(password)
  29.  
  30. hashedPassword = passwordHashing(self.usernamePlain, password, '')
  31. self.password = hashedPassword
  32.  
  33. def setPasswordWithSalt(self, password, salt):
  34. errorCheckPassword(password)
  35.  
  36. hashedPassword = passwordHashing(self.usernamePlain, password, salt)
  37. self.password = hashedPassword
  38.  
  39. def errorCheckPassword(self, password):
  40. if len(password) == 0:
  41. print('The pasword passed in is empty or null!')
  42.  
  43. def getUsername(self):
  44. return self.username
  45.  
  46. def getPassword(self):
  47. return self.password
  48.  
  49. def getUserKey(self):
  50. return self.key
  51.  
  52. def getPasswordSalt(self):
  53. return self.salt
  54.  
  55. def getDateTime(self):
  56. return self.dateTime
  57.  
  58. def loginEncryption(self, username):
  59. # block size for cipher object (always 16 for AES)
  60. blockSize = 16
  61.  
  62. # padding to ensure that value you encrpyt is a multiple of block size in length
  63. padding = "{"
  64.  
  65. # pad the text to be encrypted
  66. padTheText = lambda s: s + (blockSize - len(s) % blockSize) * padding
  67.  
  68. encodeAES = lambda c, s: base64.b64encode(c.encrypt(padTheText(s)))
  69.  
  70. # creates a random secret key
  71. self.key = 'This is a username secret key12.'
  72.  
  73. # cipher object using the secrey key
  74. cipher = AES.new(self.key)
  75.  
  76. # encrypts username
  77. encodedUsername = encodeAES(cipher, username)
  78.  
  79. return encodedUsername
  80.  
  81. def loginDecryption(self, encodedUsername):
  82. mode = 'utf-8'
  83.  
  84. padding = "{"
  85.  
  86. blockSize = 16
  87.  
  88. padTheText = lambda s: s + (blockSize - len(s) % blockSize) * padding
  89.  
  90. decodeAES = lambda c, e: c.decrypt(base64.b64decode(e))
  91.  
  92. cipher = AES.new('This is a username secret key12.')
  93.  
  94. decodedUsername = decodeAES(cipher, encodedUsername)
  95.  
  96. decodedUsername = str(decodedUsername, mode)
  97.  
  98. decodedUsername = decodedUsername.rstrip(padding)
  99.  
  100. return decodedUsername
  101.  
  102. # call this when user is registering and creating their password
  103. def passwordHashing(self, username, password, saltDB):
  104. mode = 'utf-8'
  105. # uses the username as salt
  106. usernameSalt = username
  107.  
  108. # adds to the username to make a more secure salt
  109. salt = usernameSalt + 'This is CSC 376'
  110.  
  111. salt = str.encode(salt)
  112. # store randomSalt with user login info - each user has own random salt
  113. randomSalt = ''
  114. if saltDB == '':
  115. randomSalt = os.urandom(32)
  116. else:
  117. randomSalt = saltDB
  118.  
  119. finalSalt = randomSalt + salt
  120.  
  121. self.salt = finalSalt
  122.  
  123. iterations = 22000
  124.  
  125. password = str.encode(password)
  126.  
  127. hex = hashlib.pbkdf2_hmac(hash_name='sha256',
  128. password=password,
  129. salt=finalSalt,
  130. iterations=iterations,
  131. dklen=128)
  132.  
  133. hashHex = binascii.hexlify(hex)
  134.  
  135. return hashHex
  136.  
  137.  
  138. a = LoginEncoding()
  139. a.setUsername("jessicahua95")
  140. a.setPassword("hello")
  141.  
  142. print(a.getUsername())
  143. print(a.loginDecryption(a.getUsername()))
  144. print(a.getPassword())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement