2

Lets say I have the following strings

a = "123456"
b = "#$%[{\"
c = "ABCDEFG"

I need to convert these three string into a "d" string with the following properties

  • The "d" string is obfuscate (it does not need to be encrypted)
  • The "d" string can be converted into the a,b,c string (it is reversible)
  • The "d" string should be fast to compute
  • The "d" string should be as short as possible

So far what I do is something like this

d = a+"|"+b+"|"+c
d = base64.encode(d)

So far this accomplishes the first three requirements, but not the third one, as base64 tends to make strings pretty big.

I have been also looking at other solutions

  • Use XOR encryption
  • Consider using CRC32 as some questions (Reversing CRC32) states that it might be possible to revert it, however, I am not sure about it.

Finally note that the "obfuscation" part is done by python and the "restoration" part is done by php.

Any ideas?

Community
  • 1
  • 1
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
  • 1
    Instead of base64-encoding, just add a constant to each character's ordinal value. – Waleed Khan Jun 06 '13 at 05:35
  • @WaleedKhan Do you mean something like the rot13 encoding? – Juan Antonio Gomez Moriano Jun 06 '13 at 05:36
  • Base64 is only 33% larger, is it that big of a deal? – Patashu Jun 06 '13 at 05:47
  • How obfuscated do you need your strings to be? After `d` is created, you could use the regex `(.)(.)` and replace it with `\2\1`. To reverse, use the exact same regex/replacement. The size of `d` wouldn't change at all. – Daniel Haley Jun 06 '13 at 05:52
  • @DanielHaley What is that `(.)(.)` ?? It looks like .. you know ... `( . Y . ) ԅ(ˆ⌣ˆԅ)` – wim Jun 06 '13 at 05:54
  • :-) Find two characters and swap them. Example: `This string is now obfuscated.` becomes `hTsis rtni gsin woo fbsuacet.d`. It's not a high level of obfuscation, but might be sufficient for this question? – Daniel Haley Jun 06 '13 at 06:01
  • @Patashu Yes, it is, specially because the longer the string gets, the bigger than 33% is. In the given example strings are short, but they might grow in other cases. – Juan Antonio Gomez Moriano Jun 06 '13 at 06:19

2 Answers2

1

If your strings are a little longer than your examples, then the zlib module might be a good fit:

>>> import zlib
>>> zlib.compress("123456789123456789")
'x\x9c3426153\xb7\xb04\x841\x00#\x01\x03\xbb'
>>> zlib.decompress(_)
'123456789123456789'

However, short strings will tend to get longer due to the format's overhead:

>>> zlib.compress("1234")
'x\x9c3426\x01\x00\x01\xf8\x00\xcb'

Decompression can be done with PHP's gzuncompress.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

You could just do a simple Caesar Cypher:

from string import printable as alphabet 
import string

def caesar(plaintext, shift):
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)

shift=3
for s in ("123456","#$%[{\\","ABCDEFG"):
    coded=caesar(s,shift)
    print s
    print coded
    print caesar(coded,-shift)
    print 

Prints:

123456
456789
123456

#$%[{\
&'(^~_
#$%[{\

ABCDEFG
DEFGHIJ
ABCDEFG

By definition, the strings get no longer or shorter. I have tweaked the code to handle your examples.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • I have tried with rot13 but I discarded it, as it only works for letters, if you see the examples I put, i need to obfuscate not only numbers but also other characters such as #$%^ ... – Juan Antonio Gomez Moriano Jun 06 '13 at 06:08
  • @JuanAntonioGomezMoriano: I added support for your examples. Not extensively tested, but it does work with a defined and stable character set like all ascii letters. You may need to limit the end letters though since the translated letter may pick up additional meaning like DEL – dawg Jun 06 '13 at 06:13