Python Forum
Encrypt and decrypt in python using own fixed key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encrypt and decrypt in python using own fixed key
#1
Hi I want to encrypt an d decrypt. But I want to use fixed defined key for example: key = "Abcd123". Can some one help how to do it. Thanks in advance. I use below code.
from cryptography.fernet import Fernet
message = "I am python" 
key = Fernet.generate_key()
fernet = Fernet(key)
encMessage = fernet.encrypt(message.encode())
print("original string: ", message)
print("encrypted string: ", encMessage)
decMessage = fernet.decrypt(encMessage).decode()
 
print("decrypted string: ", decMessage)
Reply
#2
Hi. I can't say that I've ever used the cryptography module, but I got this and modified this from the cryptography website:
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
salt = os.urandom(16)
kdf = PBKDF2HMAC(
    algorithm=hashes.SHA256(),
    length=32,
    salt=salt,
    iterations=390000,
)
message = b"I am python"
password = b"Abcd123"
key = base64.urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
encMessage = f.encrypt(message)
print(encMessage)
decMessage = f.decrypt(encMessage)
print(decMessage.decode('utf-8'))
SriRajesh likes this post
Reply
#3
(Feb-19-2022, 01:03 PM)dboxall123 Wrote: Hi. I can't say that I've ever used the cryptography module, but I got this and modified this from the cryptography website:
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
salt = os.urandom(16)
kdf = PBKDF2HMAC(
    algorithm=hashes.SHA256(),
    length=32,
    salt=salt,
    iterations=390000,
)
message = b"I am python"
password = b"Abcd123"
key = base64.urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
encMessage = f.encrypt(message)
print(encMessage)
decMessage = f.decrypt(encMessage)
print(decMessage.decode('utf-8'))

It show error:
TypeError:__init__() missing 1 positional argument:'backend'
Reply
#4
It's working perfectly here. Post the complete error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 191 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,851 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Fixed colum width for rowLabels i Matplotlib pandabay 0 422 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Play fixed frequency sound in python 3 jpezz 2 2,767 Feb-07-2021, 08:21 PM
Last Post: jpezz
  Referencing a fixed cell Mark17 2 2,058 Dec-17-2020, 07:14 PM
Last Post: Mark17
  Trying to encrypt and decrypt password into a file rpizw 4 3,284 Aug-12-2020, 05:15 PM
Last Post: bowlofred
  encrypt entire project mattc 2 2,415 Jul-21-2020, 07:05 AM
Last Post: mattc
  The code to decrypt Caeser Cipher. lazerwolf101 2 3,135 May-26-2020, 04:01 PM
Last Post: DT2000
  Paul Rubin p3.py lightweight encrypt/decrypt - is there a python3 version? mason28 0 1,599 Feb-19-2020, 03:38 AM
Last Post: mason28
  Zeep lib, encrypt soap body miha1234 0 2,841 Sep-12-2019, 07:52 AM
Last Post: miha1234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020