title: getting the error "Invalid DES key size. Key must be exactly 8 bytes long" while encrypting message in python
body: <p>I have the below java code to encode a string with a pass key</p>
  
  <pre><code>     public static String encrypt(String message, String passkey) throws Exception {
       final MessageDigest md = MessageDigest.getInstance("SHA-1");
       final byte[] digestOfPassword = md.digest(passkey.getBytes("utf-8"));
       final byte[] keyBytes = ( byte[])resizeArray(digestOfPassword, 24);
       for (int j = 0, k = 16; j &lt; 8;) {
           keyBytes[k++] = keyBytes[j++];
       }
  
       final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
       final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
       final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
       cipher.init(Cipher.ENCRYPT_MODE, key, iv);
  
       final byte[] plainTextBytes = message.getBytes("utf-8");
       final byte[] cipherText = cipher.doFinal(plainTextBytes);
  
       String encryptedString = Base64.encodeBase64String(cipherText);         
       return encryptedString;
   }
  </code></pre>
  
  <p>Now I converted the same code into python(Python 2.7), and I tried as below.</p>
  
  <pre><code>     def encrypt(message, passkey):
          hash_object = hashlib.sha1(passkey.encode("utf-8"))
          digested_passkey = hash_object.digest() //hashing
          key24 = "{: &lt;24}".format(digested_passkey) // for resizing the   byte array to size 24  
          import pyDes
          des = pyDes.des(key24);(at this line I m getting the error "Invalid DES key size. Key must be exactly 8 bytes long".
          message = message.encode('utf-8')
          message = message + (16 - len(message) % 16) * chr(16 - len(message) % 16)  // this is for padding
          iv = Random.new().read(AES.block_size)
          cipher = AES.new(des, AES.MODE_CBC, iv) 
          return base64.b64encode(iv + cipher.encrypt(message))
  </code></pre>
  
  <p>At the line des = pyDes.des(key24), I am getting the error "Invalid DES key size. Key must be exactly 8 bytes long."</p>
  
  <p>The passkey that I sent as parameter is     "f!16*hw$sda66"
  Can anyone please let me know if there is anything wrong with the line 
  des = pyDes.des(key24)</p>