No Description

encrypt_views.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. import random
  4. from django_logit import logit
  5. from django_response import response
  6. from TimeConvert import TimeConvert as tc
  7. from logs.models import MchInfoDecryptLogInfo, MchInfoEncryptLogInfo
  8. from mch.models import ActivityInfo, BrandInfo, ModelInfo
  9. from utils.algorithm.b64 import b64_decrypt, b64_encrypt
  10. from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt
  11. from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
  12. # CIPHER_ALGORITHM = ('CAESAR', 'B64', 'RSA')
  13. CIPHER_ALGORITHM = ('CAESAR', )
  14. CIPHER_PREFIX = {
  15. 'CAESAR': '0',
  16. 'B64': '1',
  17. 'RSA': '2',
  18. }
  19. @logit(res=True)
  20. def encrypt(request):
  21. plaintext = request.POST.get('plaintext', '')
  22. optor_id = request.POST.get('optor_id', '')
  23. # brand_id#model_id#distributor_id#sn#time
  24. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  25. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  26. mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext)
  27. if created_at:
  28. alg = random.choice(CIPHER_ALGORITHM)
  29. if alg == 'CAESAR':
  30. ciphertext = caesar_encrypt(plaintext)
  31. elif alg == 'B64':
  32. ciphertext = b64_encrypt(plaintext)
  33. elif alg == 'RSA':
  34. ciphertext = rsa_encrypt(plaintext)
  35. else:
  36. ciphertext = plaintext
  37. mieli.alg = alg
  38. mieli.ciphertext = ciphertext
  39. mieli.brand_pk = brand_pk
  40. mieli.model_pk = model_pk
  41. mieli.distributor_pk = distributor_pk
  42. mieli.sn = sn
  43. mieli.operator_id = optor_id
  44. mieli.save()
  45. return response(200, data={
  46. 'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
  47. prefix=CIPHER_PREFIX.get(mieli.alg, ''),
  48. cipherlen=len(mieli.ciphertext),
  49. ciphertext=mieli.ciphertext,
  50. ),
  51. })
  52. @logit(res=True)
  53. def decrypt(request):
  54. ciphertext = request.POST.get('ciphertext', '')
  55. prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
  56. ciphertext = ciphertext[:int(cipherlen)]
  57. if prefix == CIPHER_PREFIX['CAESAR']:
  58. plaintext = caesar_decrypt(ciphertext)
  59. elif prefix == CIPHER_PREFIX['B64']:
  60. plaintext = b64_decrypt(ciphertext)
  61. elif prefix == CIPHER_PREFIX['RSA']:
  62. plaintext = rsa_decrypt(ciphertext)
  63. else:
  64. plaintext = ciphertext
  65. # brand_id#model_id#distributor_id#sn#time
  66. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  67. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  68. try:
  69. brand = BrandInfo.objects.get(pk=brand_pk)
  70. except BrandInfo.DoesNotExist:
  71. brand = None
  72. try:
  73. model = ModelInfo.objects.get(pk=model_pk)
  74. except ModelInfo.DoesNotExist:
  75. model = None
  76. mdli, created_at = MchInfoDecryptLogInfo.objects.get_or_create(ciphertext=ciphertext, defaults={
  77. 'brand_pk': brand_pk,
  78. 'model_pk': model_pk,
  79. 'distributor_pk': distributor_pk,
  80. 'sn': sn,
  81. 'decrypt_count': 1,
  82. })
  83. if not created_at:
  84. mdli.decrypt_count += 1
  85. mdli.save()
  86. act = ActivityInfo.objects.filter(status=True).order_by('-pk').first()
  87. has_unexpired_activity = True if act and act.has_unexpired_activity(model.model_uni_name) else False
  88. return response(200, data={
  89. 'plaintext': plaintext,
  90. 'logo_url': brand.brand_logo_url if brand else '',
  91. 'model_imgs': model.images if model else [],
  92. 'goodsInfo': {
  93. 'BrandID': brand_pk,
  94. 'Brand': brand.brand_name if brand else '',
  95. 'ModelID': model_pk,
  96. 'Model': (model.model_full_name or model.model_name) if model else '',
  97. 'DistributorID': distributor_pk,
  98. 'SerialNo': sn,
  99. },
  100. 'has_unexpired_activity': has_unexpired_activity,
  101. })