Digital Rights Management für elektronische Patientenakten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto_functions.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import os
  2. import codecs
  3. from Crypto.PublicKey import RSA
  4. from Crypto.Cipher import PKCS1_OAEP, AES
  5. from Crypto.Hash import SHA256
  6. from Crypto.Signature import PKCS1_v1_5
  7. from Crypto import Random
  8. from base64 import b64decode, b64encode
  9. BS = 16
  10. pub_key_contentserver = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYI/jGk6f0LAT2Z6AAQUR7izbi\na5O4Zzaz4WtK00jr3AqbZMVeVZAs+As5RS35PY2BlCuEEza/J5XX1tlbUhGk/Nzu\nyYqlID6ILEk9kUqh1A6EAuNVrcCL174BRLy620pU5m+E61za0tIr1lU+Jhy4ikVK\niGQ+na5a5g0kuzZTHwIDAQAB\n-----END PUBLIC KEY-----'
  11. private_key_contentserver = '-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQDYI/jGk6f0LAT2Z6AAQUR7izbia5O4Zzaz4WtK00jr3AqbZMVe\nVZAs+As5RS35PY2BlCuEEza/J5XX1tlbUhGk/NzuyYqlID6ILEk9kUqh1A6EAuNV\nrcCL174BRLy620pU5m+E61za0tIr1lU+Jhy4ikVKiGQ+na5a5g0kuzZTHwIDAQAB\nAoGAFuf9CgKFBrYRkpGZ3d0M5nDzEJzpC1546ChoKAZrUH/B6gUMe7pirLle6yNf\nQ25YDFcJI5arsyd9VGITJ//zGZC1CC9MBL1v4Ey5Vm3OAwcn7aoiPYoZhtNUDGhE\nCKzIrKWlq5wQDfYCjL2zQnCIAq+nZWACI/X+k/bPd37FZpUCQQDdQNQ3Azaq0owx\nd2crResCW4OryvhTPipLNz8AaW2IMuDfxLUKEaTTkU3NOFRRhIddRRA9/vfcHbrw\nlgZ3Vo5NAkEA+hWX0tItz4lL4pSJTc38Tzjb9WyGQPuAINlntnDwOhtvvRYPfTLi\nLb/22btAfgQWSBICxFdO5Ze0A5Dx140VGwJARwVhWYtZh/nv8I0Ae/6EkowntwR/\nM9FXqC9CtPIiq76ROqMc7e999j/FNqPnRQeCoCjkLtJiY7DTahjuWG5bXQJBAIAD\nS5scAV0pz5FlLT+JgGzhEx729WYQF085WjB2cVGdN75Xq4gP4t0+VVKw2ltnJiyw\nI4BznKxD0l689D69Nh8CQQDLkom4Fhkl8CZQLB1cIX9PjNw+KCBkTV92dJmzdmR5\nGE+p7jO849HPS83W21AV6/eyL8VMqDyE6CBzHNr2ITJT\n-----END RSA PRIVATE KEY-----'
  12. pub_key_licenseserver= "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCG54kIgf8wi+WLrmg0OLXTsMF7BsYjJ8+mmx1/Ml1Hts2oVoisf8iKbfcC0jWBc1aEibi7EIZK9AWEmgpIHPZpf2Ezt36KX7pjA7CDgZBye/bw+G1CaDGxrN8a18db8c16hNxHMTMiqPm7WAgoyekOzlCT6/rqF+3w2oYuGx1tmQIDAQAB-----END PUBLIC KEY-----"
  13. def make_key_pair_rsa(username):
  14. random_generator = Random.new().read
  15. priv_key = RSA.generate(1024, random_generator)
  16. pub_key = priv_key.publickey()
  17. print("new key pair")
  18. print(priv_key.exportKey())
  19. print(pub_key.exportKey())
  20. user_smartcard_path = "/tmp/smartcards/" + str(username) + ".txt"
  21. file_object = open(user_smartcard_path, "w+")
  22. file_object.write(priv_key.exportKey().decode('utf-8'))
  23. file_object.write("\n")
  24. file_object.write(pub_key.exportKey().decode('utf-8'))
  25. file_object.close()
  26. return pub_key.exportKey().decode('utf-8')
  27. def make_rand_key_aes():
  28. return os.urandom(32)
  29. def pad(s):
  30. return s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode()
  31. def unpad(s):
  32. return s[:-ord(s[len(s)-1:])]
  33. def encrypt_aes(message, key):
  34. message = message.encode()
  35. raw = pad(message)
  36. cipher = AES.new(key, AES.MODE_ECB)
  37. enc = cipher.encrypt(raw)
  38. return b64encode(enc).decode('utf-8')
  39. def decrypt_aes(enc, key):
  40. enc = b64decode(enc)
  41. cipher = AES.new(key, AES.MODE_ECB)
  42. dec = cipher.decrypt(enc)
  43. return dec.decode('utf-8')
  44. def make_encrypted_key_content_server():
  45. random_key = make_rand_key_aes()
  46. key = RSA.importKey(pub_key_contentserver)
  47. pub_key = key.publickey()
  48. encrypted_key = encrypt(random_key, pub_key)
  49. return encrypted_key.hex()
  50. def decrypt(ciphertext):
  51. key = RSA.importKey(private_key_contentserver)
  52. cipher = PKCS1_OAEP.new(key)
  53. ciphertext = codecs.decode(ciphertext, 'hex')
  54. return cipher.decrypt(ciphertext)
  55. def encrypt(message):
  56. cipher = PKCS1_OAEP.new(pub_key_contentserver)
  57. return cipher.encrypt(message)
  58. def sign(message, priv_key):
  59. priv_key = RSA.importKey(priv_key)
  60. signer = PKCS1_v1_5.new(priv_key)
  61. hash_gen = SHA256.new()
  62. hash_gen.update(message.encode())
  63. return b64encode(signer.sign(hash_gen)).decode('utf-8')
  64. def verify(message, signature, pub_key):
  65. pub_key = pub_key.replace("-----BEGIN PUBLIC KEY-----", "")
  66. pub_key = pub_key.replace("-----END PUBLIC KEY-----", "")
  67. pub_key = pub_key.replace(" ", "")
  68. pub_key = ('\n'.join(pub_key[i:i + 64] for i in range(0, len(pub_key), 64)))
  69. pub_key = "-----BEGIN PUBLIC KEY-----\n" + pub_key + "\n-----END PUBLIC KEY-----"
  70. signature = b64decode(signature)
  71. pub_key = RSA.importKey(pub_key)
  72. verifier = PKCS1_v1_5.new(pub_key)
  73. hash_gen = SHA256.new(message.encode())
  74. return verifier.verify(hash_gen, signature)