mitnk
mitnk

Reputation: 3307

Reversible encryption in python

Short Question: Is there a proved strong reversible encryption method (in Python)?

Requirement: Do not require 3rd part of Python libraries.
Apply environment: transport data through networks.

I saw a method using str.translate() with a key-generated table. Here is the table generating function:

def get_table(key):
    m = hashlib.md5()
    m.update(key)
    s = m.digest()
    (a, b) = struct.unpack('<QQ', s)
    table = [c for c in string.maketrans('', '')]
    for i in xrange(1, 1024):
        table.sort(lambda x, y: int(a % (ord(x) + i) - a % (ord(y) + i)))
    return ''.join(table)

Questions about this function:

  1. Is this a good/strong reversible encryption?
  2. In the function 1024 is a big number, need we loop so many times to get a table that strong enough?

Thanks in advance.

Upvotes: 2

Views: 6159

Answers (3)

yo halbe
yo halbe

Reputation: 33

you could make your own encrpytion program by using an offset factor. ie; convert each letter into a number using ord(). add to the number using a randomly generated offset key. convert back into letter using chr()

and to decrypt: convert each character into a number using ord() subtract by the offset key convert back into letter using chr() you know have the original message.

hope it helps you

Upvotes: 0

khagler
khagler

Reputation: 4056

If you want strong encryption without a third-party library, you're out of luck--the Python Standard Library only has hash functions. If you want secure encryption you'll have to either implement something like AES yourself (this is not a good idea, as it's really easy for the inexperienced to mess up when implementing an encryption algorithm), or change your requirements and use PyCrypto.

Upvotes: 5

Miles
Miles

Reputation: 1888

An xor cipher would work nicely (if you bitwise-XOR each character of the message with its counterpart in the key, you can get back to the message again by XORing the ciphertext with the key again).

XOR Cipher

EDIT: Exactly how you acquire the key will determine the security of this cipher, but it's a fast, easily reversible cipher.

EDIT2: Specifically, see these lines from the Wiki on how you might make this a secure cipher system...

"If the key is random and is at least as long as the message, the XOR cipher is much more secure than when there is key repetition within a message.[3] When the keystream is generated by a pseudo-random number generator, the result is a stream cipher. With a key that is truly random, the result is a one-time pad, which is unbreakable even in theory."

Upvotes: 2

Related Questions