0

A novice programmer here trying to encrypt a data string using pycrypto AES. After careful analysis and trying it out in python shell, I have coded crypt.py and tried running it. The AES function which was running perfectly in the shell started giving out strange symbols as output when i run the file. Can someone shed some light on where I am doing wrong.

Here is my crypt.py:

#!/usr/bin/env python

from Crypto.Cipher import AES
from Crypto import Random
import os
import md5
import sys
import binascii
import base64

password = "fault_tolerant_system"
md5obj = md5.new()
md5obj.update(password)
key = md5obj.digest()

def pad(data, width):
  needed = width - ((len(data) + 1) % width)
  return data + ('\x00' * needed) + chr(needed + 1)

def unpad(data):
  padding = ord(data[-1])
  return data[:-padding]

def main() :
  val = "Hi I am ked"
  crcval = binascii.crc32(val) & 0xffffffff
  lencrc = len(str(crcval))
  finalval = str(lencrc) + "|" + val + str(crcval)
  print 'finalval after crc appending: ', finalval
  padded_finalval = pad(finalval, 16)
  print "padded final val : ", padded_finalval

  #encrypt using AES
  print Random.new().read(16)
  ivp = binascii.hexlify(Random.new().read(16))
  aes_encryptor = AES.new(key, AES.MODE_CBC, ivp[:16])
  cipher2 = aes_encryptor.encrypt(padded_finalval)
  print "ivp is:", ivp
  print "cipher2: ", cipher2
  cipher = ivp[:16] + aes_encryptor.encrypt(padded_finalval)
  print "cipher: ", (cipher)
  print "\n\n******"

  res_enc_iv_aes = cipher
  ivg = res_enc_iv_aes[:16]
  print ivg
  aes_decryptor = AES.new(key, AES.MODE_CBC, ivg)
  print binascii.hexlify(aes_decryptor.decrypt(res_enc_iv_aes[16:]))
  resvalue = unpad(aes_decryptor.decrypt(res_enc_iv_aes[16:]))

  print "\ndecrpted: ", resvalue      
  crclen = int(resvalue.split("|", 1)[0])
  print "crclen: ", crclen
  crc_cksum = resvalue[-crclen:]
  print crc_cksum
  recv_data = finalval[len(str(crclen))+1 : -crclen]
  print "recv_data: ", recv_data
#compute checksum of recv_data
  crc_recv_data = binascii.crc32(recv_data) & 0xffffffff
  print crc_recv_data      
  if str(crc_recv_data) == crc_cksum:
        #no error in data
    print "recv_data: ", recv_data
  else:
    print "error :("
    print "recdata:", recv_data

if __name__ == "__main__":
  main() 

And the output is: finalval after crc appending: 10|Hi1293356558

padded final val : 10|Hi1293356558

A0>�̔�ˈ�_Y/�u

ivp is: 95f9a5238701703b8a58933bb48e9015

mqMher2: ����]6� �75��՘�+�2=��P�H�� T��ڕu'5/�Q�9a5238701703b5�U���T���fQ�


95f9a5238701703b

edd08277e52fc138369bb557e24d88d000000000000000000000000000000011

decrpted: C���)�{��I3,p

Traceback (most recent call last):
  File "cry1.py.py", line 70, in <module>
    main()
  File "cry1.py.py", line 53, in main
  crclen = int(resvalue.split("|", 1)[0])

ValueError: invalid literal for int() with base 10: 'C\xe0\xe9\x1a'
kate
  • 113
  • 1
  • 5
  • 17
  • 1
    That's working as intended. Once you have the crypt text, you need to ASCII armor it or else encode it to a format that preserves binary data. – rdodev Nov 30 '13 at 02:17
  • Check this question and answer to be more specific: http://stackoverflow.com/questions/16220016/app-engine-ndb-stringproperty-and-string-hashes – rdodev Nov 30 '13 at 02:30
  • I tried base64 encoding, yet decrypted data is not coming out as expected. If this is not a proper way to deal with this, can you suggest using what it will work. Also, any idea why I'm getting the invalid literal error? Thanks @rdodev – kate Nov 30 '13 at 02:30
  • but, he(in the link) is not getting the strange symbols in encrypted data like I do..how do I get in that form @rdodev – kate Nov 30 '13 at 02:37
  • @kedari-tehja, I was getting same weird symbols (I asked that question a bit ago) and that's why I couldn't store it in the DB. It's just not in the question itself, but my logs looked very similar to yours. – rdodev Nov 30 '13 at 02:40
  • hmm..it worked perfectly when I tried in python shell..but doesnt work when I run it as a file.. :( have you solved this problem? @rdodev – kate Nov 30 '13 at 02:45
  • yes, once I encoded/decoded to the right format things worked out. – rdodev Nov 30 '13 at 02:47
  • which format did you use? Please share @rdodev – kate Nov 30 '13 at 02:51
  • 1
    unicode_key = key.decode('iso-8859-1') bytes_key = unicode_key.encode('iso-8859-1') – rdodev Nov 30 '13 at 02:53
  • 1
    FWIW, those weird symbols are the [Unicode replacement character](http://graphemica.com/%EF%BF%BD), "used to replace an unknown or unrepresentable character". – robertklep Nov 30 '13 at 21:19

1 Answers1

-1

Thanks to Kyle. This explains and answers everything you want to know of AES pycrypto. http://kyleisom.net/downloads/crypto_intro.pdf

kate
  • 113
  • 1
  • 5
  • 17