Is there any way to limit the number of characters in a crypt?

Asked

Viewed 125 times

-1

I need to encrypt some database keys to decrypt in the future, but all generated messages need to be the same size. md5 has generated messages of the same size, but can not translate back (E is not encryption, but message Digest). I tried to use simple-decrypt, but the generated messages are of different sizes:

import sys
from simplecrypt import encrypt, decrypt
from base64 import b64encode, b64decode
from getpass import getpass

password = getpass()
message = sys.argv[1]

cipher = encrypt(password, message)
encoded_cipher = b64encode(cipher)
print(encoded_cipher)

I wanted to know if it is possible to limit the message size. Or create a crypt method that limits the size of the generated message.

  • 1

    Is that what you want to know? -> https://crypto.stackexchange.com/a/29989

  • Not really, my encoded_cipher I hope is already bigger than the id, but I always want a fixed size. Maximum id size type will always be 8, and the size of my encoded_cipher will always be 20, regardless of whether the id is 1 or 12345678

  • But reading the guy’s answer, I understood some things I need.

1 answer

1


If you want a fixed message size, you have to reverse the logic: just set the size to the largest possible response, and then complete the ones that are smaller with any character.

In the case of the use of base64, the character used to fill is the same = because it is ignored and makes no difference when decoding.

For example, if you want your messages to have all TAMANHO characters:

if len(encoded_cipher) > TAMANHO:
    raise Exception('Mensagem muito grande, nao cabe')
if len(encoded_cipher) < TAMANHO:
    # completa a mensagem
    encoded_cipher += '=' * (TAMANHO - len(encoded_cipher))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.