1

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.

3
  • Sounds like AES is a perfect fit. Commented May 9, 2014 at 8:47
  • How do I get AES to return the same length string? Commented May 9, 2014 at 8:57
  • 1
    rot13 is very fast and preserves length! data = "plaintext".encode("rot13"). Commented May 9, 2014 at 9:34

2 Answers 2

2

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")
Sign up to request clarification or add additional context in comments.

3 Comments

I don't want to use keys, I want to use another string as a secret. Also seems like keyczar is no longer installable from pypi, not a good sign.
copied from the dupe answer.
In future, please flag instead. The whole point of marking questions as duplicates is so we don't have to needlessly copy answers across questions.
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.