KEMBAR78
Module 3 | PDF
0% found this document useful (0 votes)
252 views2 pages

Module 3

The document defines a function that encrypts and decrypts strings using the Python cryptography module. It takes a key, string to encrypt, and string to decrypt as parameters. It encrypts the first string, decrypts the second string, and returns them in a list. It then calls the function to encrypt and decrypt sample input strings and writes the results to a file.

Uploaded by

sach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views2 pages

Module 3

The document defines a function that encrypts and decrypts strings using the Python cryptography module. It takes a key, string to encrypt, and string to decrypt as parameters. It encrypts the first string, decrypts the second string, and returns them in a list. It then calls the function to encrypt and decrypt sample input strings and writes the results to a file.

Uploaded by

sach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'encrdecr' function below.
#
# The function is expected to return a LIST.
# The function accepts following parameters:
# 1. STRING keyval
# 2. STRING textencr
# 3. Byte-code textdecr
#

from cryptography.fernet import Fernet


def encrdecr(keyval, textencr, textdecr):

l1=[]
f=Fernet(keyval)
l1.append(f.encrypt(textencr))
l1.append(f.decrypt(textdecr).decode())
return l1

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

file = open('key.key', 'rb')


key = file.read() # The key will be type bytes
file.close()

keyval = key

textencr = str(input()).encode()

textdecr = str(input()).encode()

result = encrdecr(keyval, textencr, textdecr)


bk=[]
f = Fernet(key)
val = f.decrypt(result[0])
bk.append(val.decode())
bk.append(result[1])

fptr.write(str(bk) + '\n')

fptr.close()
'''

This text is to be encrypted with cryptography module


gAAAAABe2LzlIGxTehNE5gqwgDzJb_5y72fKiFhNwjuIGWCN1KSUrLXiq8rV3p4ZRmK-
km6JI9oezOgGG0nIp8r6KejTcn09J70bVJ6Y04h0w8zztr9w9Ts=
['This text is to be encrypted with cryptography module', 'my deep dark secret']

The Earth is a beautiful place to live


gAAAAABe2Mc0umqGP-
nA6qNyTKsN9h49KuEKtVvec_6F2WJE2HCHYRdNG5PDsTHmUNoR9XCpaQkHz9N1hKPhoAC9TjGzTAbM0w97p
vQVM2uz-3QaPiVEzDhUodcaNE_Fw6rouhD88Pd0

['The Earth is a beautiful place to live', 'The Earth is a beautiful place to


live']'''

You might also like