Encoding/Encrypting from stdin in Python
<p>I'm trying to encrypt data from stdin to send across a socket, so for testing I'm trying to decrypt what I just encrypted and it's not producing the original message as is.</p>

<pre><code>BS = 16
client_cipher = AES.new(client_conf_key, AES.MODE_CBC, randIV)
message = sys.stdin.readline()

padded_message = message + '$' + (BS - ((len(message)+1)%16))*'#'
client_message = base64.b64encode(client_cipher.encrypt(padded_message))

print "Encrypted: " + client_message

enc_str = base64.b64decode(client_message)
dec_str = client_cipher.decrypt(enc_str)

print "Decrypted: " + dec_str
</code></pre>

<p>Here's what my output looks like now
<a href="http://i.stack.imgur.com/XBHHu.png" rel="nofollow"><img src="http://i.stack.imgur.com/XBHHu.png" alt="enter image description here"></a></p>

<p>I feel like I'm messing up the encoding somehow, any help would be appreciated</p>

<p>Edit: client_cipher is an AES cipher, client_cipher = AES.new(client_conf_key, AES.MODE_CBC, randIV)</p>

<p>Here is some code I once wrote to a similar effect;</p>



<pre><code>BS = 16

pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]


def encrypt(plaintext, key, iv):
    cipher = AES.new(key, AES.MODE_CBC,iv)
    plaintext = pad(plaintext)
    return cipher.encrypt(plaintext)

def decrypt(ciphertext, key, iv):
    cipher = AES.new(key, AES.MODE_CBC, iv)
    decrypted = cipher.decrypt(ciphertext)
    return unpad(decrypted)
</code></pre>



<p>This got the job done for me, make sure you pass in appropriate params!</p>

<p>HTH</p>

python encoding aes