Add cipherlen for ciphertext

Brightcells 7 gadi atpakaļ
vecāks
revīzija
1796c9c28c
1 mainītis faili ar 11 papildinājumiem un 5 dzēšanām
  1. 11 5
      api/encrypt_views.py

+ 11 - 5
api/encrypt_views.py

@@ -39,7 +39,11 @@ def encrypt(request):
39 39
         ciphertext = plaintext
40 40
 
41 41
     return response(200, data={
42
-        'ciphertext': u'%s+%s' % (CIPHER_PREFIX.get(alg, ''), ciphertext),
42
+        'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
43
+            prefix=CIPHER_PREFIX.get(alg, ''),
44
+            cipherlen=len(ciphertext),
45
+            ciphertext=ciphertext,
46
+        ),
43 47
     })
44 48
 
45 49
 
@@ -47,13 +51,15 @@ def encrypt(request):
47 51
 def decrypt(request):
48 52
     ciphertext = request.POST.get('ciphertext', '')
49 53
 
50
-    alg, ciphertext = ciphertext.split('+', 1)
54
+    prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
51 55
 
52
-    if alg == CIPHER_PREFIX['CAESAR']:
56
+    ciphertext = ciphertext[:int(cipherlen)]
57
+
58
+    if prefix == CIPHER_PREFIX['CAESAR']:
53 59
         plaintext = caesar_decrypt(ciphertext)
54
-    elif alg == CIPHER_PREFIX['B64']:
60
+    elif prefix == CIPHER_PREFIX['B64']:
55 61
         plaintext = b64_decrypt(ciphertext)
56
-    elif alg == CIPHER_PREFIX['RSA']:
62
+    elif prefix == CIPHER_PREFIX['RSA']:
57 63
         plaintext = rsa_decrypt(ciphertext)
58 64
     else:
59 65
         plaintext = ciphertext