Encrypt a python string

Asked

Viewed 60 times

-1

I wonder if there is any encryption algorithm where it is possible to choose which key or part of it will be used to encrypt/decrypt the message

An example:

mensagem = 'string qualquer'

chave = 'chave escolhida'

mensagem_encriptada = encrypt(mensagem, key = chave)

print(mensagem_encriptada)

mensagem_recuperada = decrypt(mensagem_encriptada, key = chave)

print(mensagem_recuperada)

Expected exit would be this:

(any string like dfg1tre*vf2d3jnh&& /vfdfhgf34@@cfr%ws76z!)
any string

2 answers

-2

You can use some library to do the encryption. It has several libraries such as the pycrypto, cryptocode etc. There are dozens of libraries out there. A simple example of this same program using the cryptocode library:

import cryptocode

chave = "423338233093093"
mensagem = "Esta eh a minha mensagem! :)"

MensagemCriptografada = cryptocode.encrypt(mensagem, chave)
print("Sua mensagem criptografada: " + MensagemCriptografada)

MensagemDescriptografada = cryptocode.decrypt(MensagemCriptografada, chave)
print("Sua mensagem descriptografada: " + MensagemDescriptografada)

The exit will be:

Your encrypted message: gSjdIssy9DOYfPnACctSaoNn4/Vctrzllyh4pq==*dv9iN+Uwsad+v8QMBUgF7A==*4G2FKJ2n+Wjxwak9lpyaww==*lvCp3brLC0nJVd1g4lf3Vg==
Your decrypted message: This is my message! :)

-2


Depends on symmetric or asymmetric encryption.

There are libraries that help create good encryption using AES and RSA.

a simple example using the xor function:
The bigger the safer the key, the smaller the easier it will be to identify the message

import string
import random


chave = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits + '^!\$%&/()=?{[]}+~#-_.:,;<>|\\') for _ in range(0, 1024))


print(chave)

mensagem = 'python'

print("Msg: " + mensagem + '\n')




def str_xor(s1, s2):
    return "".join([chr(ord(c1) ^ ord(c2)) for (c1, c2) in zip(s1,s2)])



secreta = str_xor(mensagem, chave)

print("Mensagem secreta :"+secreta + "\n")

recuperando_mensagem_secreta = str_xor(secreta, chave)
print("Mensagem descriptografada " + "\n" + recuperando_mensagem_secreta + "\n")

Each time the code is started a new encryption will be created

Browser other questions tagged

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