123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- from __future__ import division
- import random
- from django_logit import logit
- from django_response import response
- from TimeConvert import TimeConvert as tc
- from logs.models import MchInfoDecryptLogInfo, MchInfoEncryptLogInfo
- from mch.models import ActivityInfo, BrandInfo, ModelInfo
- from utils.algorithm.b64 import b64_decrypt, b64_encrypt
- from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt
- from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
- CIPHER_ALGORITHM = ('CAESAR', )
- CIPHER_PREFIX = {
- 'CAESAR': '0',
- 'B64': '1',
- 'RSA': '2',
- }
- @logit(res=True)
- def encrypt(request):
- plaintext = request.POST.get('plaintext', '')
- optor_id = request.POST.get('optor_id', '')
-
-
- brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
- mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext)
- if created_at:
- alg = random.choice(CIPHER_ALGORITHM)
- if alg == 'CAESAR':
- ciphertext = caesar_encrypt(plaintext)
- elif alg == 'B64':
- ciphertext = b64_encrypt(plaintext)
- elif alg == 'RSA':
- ciphertext = rsa_encrypt(plaintext)
- else:
- ciphertext = plaintext
- mieli.alg = alg
- mieli.ciphertext = ciphertext
- mieli.brand_pk = brand_pk
- mieli.model_pk = model_pk
- mieli.distributor_pk = distributor_pk
- mieli.sn = sn
- mieli.operator_id = optor_id
- mieli.save()
- return response(200, data={
- 'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
- prefix=CIPHER_PREFIX.get(mieli.alg, ''),
- cipherlen=len(mieli.ciphertext),
- ciphertext=mieli.ciphertext,
- ),
- })
- @logit(res=True)
- def decrypt(request):
- ciphertext = request.POST.get('ciphertext', '')
- prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
- ciphertext = ciphertext[:int(cipherlen)]
- if prefix == CIPHER_PREFIX['CAESAR']:
- plaintext = caesar_decrypt(ciphertext)
- elif prefix == CIPHER_PREFIX['B64']:
- plaintext = b64_decrypt(ciphertext)
- elif prefix == CIPHER_PREFIX['RSA']:
- plaintext = rsa_decrypt(ciphertext)
- else:
- plaintext = ciphertext
-
-
- brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
- try:
- brand = BrandInfo.objects.get(pk=brand_pk)
- except BrandInfo.DoesNotExist:
- brand = None
- try:
- model = ModelInfo.objects.get(pk=model_pk)
- except ModelInfo.DoesNotExist:
- model = None
- mdli, created_at = MchInfoDecryptLogInfo.objects.get_or_create(ciphertext=ciphertext, defaults={
- 'brand_pk': brand_pk,
- 'model_pk': model_pk,
- 'distributor_pk': distributor_pk,
- 'sn': sn,
- 'decrypt_count': 1,
- })
- if not created_at:
- mdli.decrypt_count += 1
- mdli.save()
- act = ActivityInfo.objects.filter(status=True).order_by('-pk').first()
- has_unexpired_activity = True if act and act.has_unexpired_activity(model.model_uni_name) else False
- return response(200, data={
- 'plaintext': plaintext,
- 'logo_url': brand.brand_logo_url if brand else '',
- 'model_imgs': model.images if model else [],
- 'goodsInfo': {
- 'BrandID': brand_pk,
- 'Brand': brand.brand_name if brand else '',
- 'ModelID': model_pk,
- 'Model': (model.model_full_name or model.model_name) if model else '',
- 'DistributorID': distributor_pk,
- 'SerialNo': sn,
- },
- 'has_unexpired_activity': has_unexpired_activity,
- })
|