I have strings about 500 chars long and I'd like to encrypt them so that resulting string has the same length (or just slightly different). I need two way enryption and decryption using another "secret" string known to both sides. It doesn't need to be very secure, I prefer faster solutions.
2 Answers
From the post here: Encrypt string in Python
using http://code.google.com/p/keyczar/
crypter = Crypter.Read("/path/to/your/keys")
ciphertext = crypter.Encrypt("Secret message")
3 Comments
If you want to preserve the length, then you want to use a stream cipher
Note that both parties need to know key (secret) and initialization vector (not secret, but must be unique).
It is not safe to reuse same key without a different IV, thus you'd need to communicate new IV across somehow, which effectively lengthens your encrypted package.
Furthermore, you really need to consider replay attacks -- what happens if someone intercepts your message and then sends it again and again.
Here's simple example for RC4, using pycrypto, no IV (RC4 doesn't support it):
import Crypto.Cipher.ARC4
plaintext = "x" * 13
c1 = Crypto.Cipher.ARC4.new(key="1")
ciphertext = c.encrypt(plaintext)
assert len(ciphertext) == len(plaintext)
c2 = Crypto.Cipher.ARC4.new(key="1")
result = c2.descrypt(ciphertext)
assert result == plaintext
data = "plaintext".encode("rot13").