title: ValueError: Message too large
body: <p>Client.py   </p>
  
  <pre><code>import socket 
  from Crypto.Cipher import AES
  import base64
  import os
  import Crypto              
  import os, shutil
  import sys
  from stat import ST_SIZE
  sys.path.insert(0, '/home/ubuntu/finalpro/server')
  import keygen
  
  
      # Create a socket object
      s = socket.socket()         
      host = socket.gethostname() 
      port = 12345                
      s.connect((host, port))
      print s.recv(1024)
  
      #Taking input from User
      print("----------------------------------------------------------------------")
      print("*******************IMPROVED AUTHENTICATION PROTOCOL*******************")
      print("----------------------------------------------------------------------")
      username=raw_input("ENTER CLIENT ID:       ")
      password=raw_input("ENTER CLIENT PASSWORD: ")
      print("----------------------------------------------------------------------")
      print("Authentication in progress...")
      print("Encrypting Data...")
  
      #Encrypting Data
      BLOCK_SIZE = 16
  
      # the character used for padding--with a block cipher such as AES, the value
      # you encrypt must be a multiple of BLOCK_SIZE in length.  This character is
      # used to ensure that your value is always a multiple of BLOCK_SIZE
      PADDING = '{'
  
      # one-liner to sufficiently pad the text to be encrypted
      pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
  
      # one-liners to encrypt/encode and decrypt/decode a string
      # encrypt with AES, encode with base64
      EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
      DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  
      # generate a random secret key
      secret = os.urandom(BLOCK_SIZE)
  
  
      # create a cipher object using the random secret
      cipher = AES.new(secret)
  
      # encode a string
      encrypted_username = EncodeAES(cipher, username)
      print 'Encrypted string:', encrypted_username
      print("Encrypting CLIENT ID...                                         [Done]")
  
      encrypted_password = EncodeAES(cipher, password)
      print 'Encrypted string:', encrypted_password
      print("Encrypting CLIENT PASSWORD...                                   [Done]")
  
      encrypted_username = keygen.publickey.encrypt(username, 32)
      #print(encrypted_username)
      print("Encrypting CLIENT ID...                                         [Done]")
      encrypted_password = keygen.publickey.encrypt(password, 32)
      #print(encrypted_password)
      print("Encrypting CLIENT PASSWORD...                                   [Done]")
  
      #Copy encrypted data into file
      f=open("encrypt.txt",'w')
      f.write(chr(len(encrypted_username)))
      f.write(str(encrypted_username))
      f.write("\n")
      f.write(chr(len(encrypted_password)))
      f.write(str(encrypted_password))
      f.close()
  
      print("Sending Data to server for Validation...")
      f=open("encrypt.txt",'rb')
      fsize=os.stat(f.name)[ST_SIZE]
      s.sendall(str(fsize).zfill(8))
      sfile = s.makefile("wb")
      shutil.copyfileobj(f, sfile)
      sfile.close()
      f.close()
      print("Sending Data to server for Validation...                        [DONE]")
  
      print("Encrypting Secret Key...                                        [Done]")
      encrypted_key=keygen.publickey.encrypt(secret, 32)
      f1=open("encrypted_key.txt",'w')
      f1.write(chr(len(encrypted_key)))
      f1.write(str(encrypted_key))
      f1.close()
  
      print("Sending Secret Key to server for Validation...")
      f1=open("encrypted_key.txt",'rb')
      f1size=os.stat(f1.name)[ST_SIZE]
      s.sendall(str(f1size).zfill(8))
      s1file = s.makefile("wb")
      shutil.copyfileobj(f1, s1file)
      s1file.close()
      f1.close()
      print("Sending Secret Key to server for Validation...                  [DONE]")
  
  
      #Sending Data to Server
  
      s.close()  
  </code></pre>
  
  <p>Server.py</p>
  
  <pre><code>import socket 
  from Crypto.Cipher import AES
  import base64
  import os
  import Crypto             
  import shutil
  import keygen
  import sys
  readsize = 127
  writesize = 128
  
  s = socket.socket()         
  host = socket.gethostname() 
  port = 12345                
  s.bind((host, port))        
  
  print("-----------------------------------------------------------------------------")
  print("*******************IMPROVED AUTHENTICATION PROTOCOL SERVER*******************")
  print("-----------------------------------------------------------------------------")
  
  # Now wait for client connection.
  s.listen(5)                
  conn, addr = s.accept()                
  print("Got Connection at address:",addr)
  conn.sendall('READY')
  
  # the block size for the cipher object; must be 16 per FIPS-197
  BLOCK_SIZE = 16
  
  # the character used for padding--with a block cipher such as AES, the value
  # you encrypt must be a multiple of BLOCK_SIZE in length.  This character is
  # used to ensure that your value is always a multiple of BLOCK_SIZE
  PADDING = '{'
  
  # one-liner to sufficiently pad the text to be encrypted
  pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
  
  # one-liners to encrypt/encode and decrypt/decode a string
  # encrypt with AES, encode with base64
  EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
  DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  
  #Reading all data and creating a file on server
  
  f = open("encrypt_key.txt",'wb')
  fsize=int(conn.recv(8))               
  print("Recieved File size:",fsize)
  sfile = conn.makefile("rb")
  shutil.copyfileobj(sfile, f)
  sfile.close()
  f.write(conn.recv(fsize))           
  f.close()
  
  #Again reading file,storing it in variables and decrypting it
  with open("encrypt_key.txt",'r') as myfile:
      data=myfile.readlines()
  encrypted_key=str(data)
  dc=keygen.privatekey.decrypt(encrypted_key)
  cipher1 = AES.new(dc)
  myfile.close()
  
  #Reading all data and creating a file on server
  f1 = open("encrypt.txt",'wb')
  f1size=int(conn.recv(8))               
  print("Recieved File size:",f1size)
  s1file = conn.makefile("rb")
  shutil.copyfileobj(s1file, f1)
  s1file.close()
  f1.write(conn.recv(f1size))           
  f1.close()
  
  #Again reading file and storing it in variables
  with open("encrypt.txt",'r') as myfile1:
      data=myfile1.readlines()
  encrypted_username=str(data[0])
  encrypted_password=str(data[1])
  
  
  #Decrypting username and password
  decoded = DecodeAES(cipher1, encoded)
  print 'Decrypted string:', decoded
  
  decoded1 = DecodeAES(cipher1, encoded1)
  print 'Decrypted string:', decoded1
  
  conn.close()
  s.close()
  </code></pre>
  
  <pre>
  Traceback (most recent call last):
    File "server.py", line 58, in 
      dc=keygen.privatekey.decrypt(encrypted_key)
    File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 174, in decrypt
      return pubkey.pubkey.decrypt(self, ciphertext)
    File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/pubkey.py", line 93, in decrypt
      plaintext=self._decrypt(ciphertext)
    File "/usr/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 237, in _decrypt
      cp = self.key._blind(ciphertext, r)
  ValueError: Message too large
  </pre>
  
  <p>keygen.py</p>
  
  <pre><code>#Key Generation Program
  import Crypto
  from Crypto.PublicKey import RSA
  from Crypto import Random
  
  
  
  
  random_generator = Random.new().read
  key = RSA.generate(1024, random_generator) #generate pub and priv key
  
  publickey = key.publickey() # pub key 
  privatekey= key # pub key 
  </code></pre>