0

I am trying to hide an actual string. So, I am choosing encoding mechanism to encode it. Is there any best Encryption/Decryption Algorithms to deal with it ?

How can I do it in Python? The encoded string should be of length 10 (encoded string should be alphanumeric).

I just tried with some code available in Google. How can I format it to get desired output needed ?

from Crypto.Cipher import AES
import base64

MASTER_KEY="Some-long-base-key-to-use-as-encyrption-key"

def encrypt_val(clear_text):
    enc_secret = AES.new(MASTER_KEY[:32])
    tag_string = (str(clear_text) +
                  (AES.block_size -
                   len(str(clear_text)) % AES.block_size) * "\0")
    cipher_text = base64.b64encode(enc_secret.encrypt(tag_string))

    print cipher_text

encrypt_val("abcd_10.10.10.10_abcdefghijk")

Output for above : ytZyd3PGRg7POl80qlF8Oi79gwj7U31D/rsC50EDCt4=
jww
  • 97,681
  • 90
  • 411
  • 885
Jackie
  • 129
  • 17
  • 3
    1) "couldn't obtain desired output needed" is useless. Please say specifically what output or error you got vs what you expected to get. 2) Since AES is not shipped with Python, I need to verify whether you did, in fact, install pycrypto module which lets you use AES? – Amadan Nov 30 '16 at 08:19
  • 1
    Truncating the master key to 32 bytes is not a good idea. Use a key derivation function. – r3mainer Nov 30 '16 at 09:03
  • @Amadan : Thanks for pointing it. I have corrected it. And edited the question with input and output too. Can you please help me in this regard. – Jackie Nov 30 '16 at 09:04
  • Is that desired output or actual output? (And you never answered about pycrypto.) – Amadan Nov 30 '16 at 09:05
  • @Jackie You're encrypting a 28-byte string and you want the result to be 10 bytes long? This makes no sense. – r3mainer Nov 30 '16 at 09:07
  • @squeamishossifrage: oh right, i didn't even notice that, i assumed plaintext was 10 bytes. That's not encryption any more, that's compression... – Amadan Nov 30 '16 at 09:13
  • @Amadan: Output mentioned above is actual output of the code. – Jackie Nov 30 '16 at 09:15
  • @All : Is there any such mechanism of encoding a string to characters of length 10 (encoded string) ? Or any better way to handle these kinds of stuffs ? – Jackie Nov 30 '16 at 09:22
  • 3
    @Jackie Voting to close now, for multiple reasons: (1) You still haven't adequately explained why the output of this code isn't what you expected. (2) You appear to be asking for an encryption function that compresses strings of any length to just 10 bytes. No such function exists. (3) The code you provided actually works (albeit with a truncated master key, which is a bad idea as I pointed out already). – r3mainer Nov 30 '16 at 09:27

0 Answers0