Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import mysql.connector
  3. from mysql.connector import (connection)
  4. import linecache
  5. import re
  6.  
  7. cnx = connection.MySQLConnection(user='the_dhan', password = 'password', database='test')
  8. cursor = cnx.cursor(buffered=True)
  9.  
  10. def Add_User(i):
  11. cnx = connection.MySQLConnection(user='the_dhan', password = 'password', database='test')
  12. c = 1
  13. while i > 0:
  14. print("Input the name of student %d to add." % c)
  15. name = raw_input()
  16. print("Input the key for the user.")
  17. key = raw_input()
  18. print("Input the Address of %s." %name)
  19. Address = raw_input()
  20. print("Input the Email of %s." %name)
  21. Email = raw_input()
  22. print("Input the Pin of %s." %name)
  23. Pin = raw_input()
  24. print("Input the Exp_Date for %s." %name)
  25. Exp = raw_input()
  26. print("Input the U_Number of %s." %name)
  27. U_num = raw_input()
  28. data = [U_num, key, Address, key, Pin, key, Email, key, Exp, key, name]
  29. curA = cnx.cursor(buffered=True)
  30. curA.execute("INSERT INTO Student (Name) VALUES ('%s')" %name)
  31. curA.execute("INSERT INTO Keeys (Name) values ('%s')" %name)
  32. curA.execute("UPDATE Keeys SET keey = AES_ENCRYPT(%s,'password') where Name = %s", (key,name))
  33. curA.execute("UPDATE Student SET U_Number = AES_ENCRYPT(%s, %s), Address = AES_ENCRYPT(%s, %s), Pin = AES_ENCRYPT( %s, %s ), Email = AES_ENCRYPT(%s, %s), Exp_Date = AES_ENCRYPT(%s,%s) WHERE Name = %s",(data))
  34. i=i-1
  35. c+=1
  36. cnx.commit()
  37. cursor.close()
  38. cnx.close()
  39.  
  40. def Search(pas):
  41. cursor.execute("Select Name from Keeys")
  42. data = cursor.fetchall()
  43. cursor.execute("Select AES_DECRYPT(keey, '%s') from Keeys" %pas)
  44. data2 = cursor.fetchall()
  45. print("Names of all students:")
  46. for row in data:
  47. print("%s" % row[0])
  48. print("The key for each student:")
  49. for rows in data2:
  50. print("%s" % rows[0])
  51.  
  52.  
  53. def Deliver():
  54. students = [1, 2, 3]
  55. for number in students:
  56. invalid = 1
  57. while invalid == 1:
  58. print("Please enter the name of student %d" % number)
  59. name = raw_input()
  60. print("Please enter the key for %s" %name)
  61. key = raw_input()
  62. curA = cnx.cursor(buffered=True)
  63. curAb = cnx.cursor(buffered=True)
  64. curB = cnx.cursor(buffered=True)
  65. curA.execute("SELECT AES_DECRYPT(Email,%s), AES_DECRYPT(Address,%s) FROM Student WHERE Name = %s", (key,key,name))
  66. curAb.execute("SELECT AES_DECRYPT(Email,%s), AES_DECRYPT(Address,%s) FROM Student WHERE Name = %s", (key,key,name))
  67. cursor.execute("SELECT AES_DECRYPT(Address,%s) FROM Student WHERE Name = %s", (key, name))
  68. curB.execute("SELECT Name FROM Student")
  69. name1 = curB.fetchall()
  70. for item in name1:
  71. x = 0
  72. if name == item[0]:
  73. x = 0
  74. if not(curA.fetchone()[0]):
  75. print("Key is wrong")
  76. break
  77. else:
  78. print("Email: %s Address: %s" %curAb.fetchone())
  79. file= open("/home/pi/Desktop/Recipients/student" + str(number),"w+")
  80. file.write(name)
  81. file.write('\n')
  82. file.write(key)
  83. file.write('\n')
  84. file.write(str(cursor.fetchone()[0]))
  85. file.close()
  86. invalid = 0
  87. break
  88. else:
  89. x = 1
  90. if(x==1):
  91. print("Name:%s is not in the database " %name)
  92.  
  93. cursor.close()
  94. cnx.close()
  95.  
  96. def Verify(Student):
  97. for num in Student:
  98. i = 3
  99. while i > 0:
  100. key = linecache.getline("/home/pi/Desktop/Recipients/student" + str(num), 2)
  101. name = linecache.getline("/home/pi/Desktop/Recipients/student" + str(num), 1)
  102. key = re.split('\n',key)[0]
  103. name = re.split('\n',name)[0]
  104. pin = raw_input("Please input the pin for %s\n" %name)
  105. cursor.execute("SELECT AES_DECRYPT(Pin,%s) FROM Student WHERE Name = %s", (key,name))
  106. real_pin = cursor.fetchone()[0]
  107. if pin == real_pin:
  108. print("Unlock.")
  109. break
  110. else:
  111. print("Incorrect Pin.")
  112. i-=1
  113.  
  114. print(" 1) Input a new users to the database.")
  115. print(" 2) See the database results for the users and their keys.")
  116. print(" 3) Deliver Packages.")
  117. print(" 4) Recieve Package.")
  118. choice = int(raw_input())
  119. if (choice == 1):
  120. print("Enter the number of Students to add.")
  121. i = int(raw_input())
  122. Add_User(i)
  123. elif (choice == 2):
  124. print("Input key for mailroom.")
  125. pas = raw_input()
  126. Search(pas)
  127. elif (choice == 3):
  128. Deliver()
  129. elif (choice == 4):
  130. Student = [3,1,2]
  131. Verify(Student)
  132. else:
  133. print("Invalid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement