No Description

encrypt_views.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 logs.models import MchInfoEncryptLogInfo
  7. from mch.models import BrandInfo, ModelInfo
  8. from utils.algorithm.b64 import b64_decrypt, b64_encrypt
  9. from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt
  10. from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
  11. # CIPHER_ALGORITHM = ('CAESAR', 'B64', 'RSA')
  12. CIPHER_ALGORITHM = ('CAESAR', )
  13. CIPHER_PREFIX = {
  14. 'CAESAR': '0',
  15. 'B64': '1',
  16. 'RSA': '2',
  17. }
  18. @logit(res=True)
  19. def encrypt(request):
  20. plaintext = request.POST.get('plaintext', '')
  21. optor_id = request.POST.get('optor_id', '')
  22. # brand_id#model_id#distributor_id#sn#time
  23. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  24. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  25. mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext)
  26. if created_at:
  27. alg = random.choice(CIPHER_ALGORITHM)
  28. if alg == 'CAESAR':
  29. ciphertext = caesar_encrypt(plaintext)
  30. elif alg == 'B64':
  31. ciphertext = b64_encrypt(plaintext)
  32. elif alg == 'RSA':
  33. ciphertext = rsa_encrypt(plaintext)
  34. else:
  35. ciphertext = plaintext
  36. mieli.alg = alg
  37. mieli.ciphertext = ciphertext
  38. mieli.brand_pk = brand_pk
  39. mieli.model_pk = model_pk
  40. mieli.distributor_pk = distributor_pk
  41. mieli.sn = sn
  42. mieli.operator_id = optor_id
  43. mieli.save()
  44. return response(200, data={
  45. 'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
  46. prefix=CIPHER_PREFIX.get(mieli.alg, ''),
  47. cipherlen=len(mieli.ciphertext),
  48. ciphertext=mieli.ciphertext,
  49. ),
  50. })
  51. @logit(res=True)
  52. def decrypt(request):
  53. ciphertext = request.POST.get('ciphertext', '')
  54. prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
  55. ciphertext = ciphertext[:int(cipherlen)]
  56. if prefix == CIPHER_PREFIX['CAESAR']:
  57. plaintext = caesar_decrypt(ciphertext)
  58. elif prefix == CIPHER_PREFIX['B64']:
  59. plaintext = b64_decrypt(ciphertext)
  60. elif prefix == CIPHER_PREFIX['RSA']:
  61. plaintext = rsa_decrypt(ciphertext)
  62. else:
  63. plaintext = ciphertext
  64. # brand_id#model_id#distributor_id#sn#time
  65. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  66. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  67. try:
  68. brand = BrandInfo.objects.get(pk=brand_pk)
  69. except BrandInfo.DoesNotExist:
  70. brand = None
  71. try:
  72. model = ModelInfo.objects.get(pk=model_pk)
  73. except ModelInfo.DoesNotExist:
  74. model = None
  75. return response(200, data={
  76. 'plaintext': plaintext,
  77. 'logo_url': brand.brand_logo_url if brand else '',
  78. 'model_imgs': model.images if model else [],
  79. 'goodsInfo': {
  80. 'BrandID': brand_pk,
  81. 'Brand': brand.brand_name if brand else '',
  82. 'ModelID': model_pk,
  83. 'Model': (model.model_full_name or model.model_name) if model else '',
  84. 'DistributorID': distributor_pk,
  85. 'SerialNo': sn,
  86. }
  87. })