M7L2 Summary
docx
keyboard_arrow_up
School
Arizona State University, Polytechnic Campus *
*We aren’t endorsed by this school
Course
30555
Subject
Information Systems
Date
Feb 20, 2024
Type
docx
Pages
8
Uploaded by CaptainIbex3617
IFT 510 - Module 7: Lab 2- Data Encryption Cryptography
Keerthana Yadavali
Dinesh Sthapit
12
th
November, 2023.
Encryption Lab Assessment Assignment
Encryption process: Step 1: Key Generation & Distribution: Code: ## Student Name: Keerthana Yadavali
## Student ID: 1230475287
## Date: 11/12/2023
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
# Generate Private Key
private_key = rsa.generate_private_key(
public_exponent
=
65537
,
key_size
=
2048
,
backend
=
default_backend()
)
# Save Private Key to File
with open
(
"private_key.pem"
, "wb"
) as f:
f.write(private_key.private_bytes(
encoding
=
serialization.Encoding.PEM,
format
=
serialization.PrivateFormat.PKCS8,
encryption_algorithm
=
serialization.NoEncryption()
))
# Generate Public Key from Private Key
public_key = private_key.public_key()
# Save Public Key to File
with open
(
"public_key.pem"
, "wb"
) as f:
f.write(public_key.public_bytes(
encoding
=
serialization.Encoding.PEM,
format
=
serialization.PublicFormat.SubjectPublicKeyInfo
))
Explanation:
The process of creating a cryptographic key that will be used to both encrypt and decrypt data
is known as the key creation stage in the encryption process. The confidentiality of the key
ensures the security of the encryption process, thus it's critical to create a strong key that's
challenging for hackers to decipher. A cryptographic procedure is used to create a public key
and a private key, which together create an asymmetric key pair. The private key is kept a
secret while the public key is disclosed. The RSA algorithm is the most widely used method
for creating asymmetric key pairs. The public and private keys are generated via the RSA
algorithm using two big prime numbers.
Output: a.
Execution of code: b.
Public key:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
c.
Private Key: Step 2: Encryption
Code: ## Student Name: Keerthana Yadavali
## Student ID: 1230475287
## Date: 11/12/2023
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
# Read Public Key from File
with open
(
"public_key.pem"
, "rb"
) as f:
public_key = serialization.load_pem_public_key(
f.read(),
backend
=
default_backend()
)
# Read Plaintext File
with open
(
"plaintext.txt"
, "rb"
) as f:
plaintext = f.read()
# Encrypt Plaintext
encrypted_data = public_key.encrypt(
plaintext,
padding.OAEP(
mgf
=
padding.MGF1(
algorithm
=
hashes.SHA256()),
algorithm
=
hashes.SHA256(),
label
=
None
)
)
# Save Encrypted Data to File
with open
(
"encrypted_data.bin"
, "wb"
) as f:
f.write(encrypted_data)
Explanation: You can access the encryption procedure in this phase. Here, we need to create a plain text
file called "plaintext.txt," which will contain the encrypted message. We execute the
aforementioned code to encrypt the file after it has been generated. The previously produced
public key is read by the code from the "public_key.pem" file. The plaintext is encrypted
using the public key encrypt method. This employs SHA-256 as the hash algorithm for both
the encryption algorithm and the mask generation function (MGF), using the Optimal
Asymmetric Encryption Padding (OAEP) scheme. The label has a None setting.
"encrypted_data.bin" is the binary file that contains the encrypted data that was produced.
The created public key is used to perform the encryption.
Output: a.
Encrypted_data.bin file: Step 3: Decryption process: Code:
## Student Name: Keerthana Yadavali
## Student ID: 1230475287
## Date: 11/12/2023
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
# Read Private Key from File
with open
(
"private_key.pem"
, "rb"
) as f:
private_key = serialization.load_pem_private_key(
f.read(),
password
=
None
,
backend
=
default_backend()
)
# Read Encrypted Data
with open
(
"encrypted_data.bin"
, "rb"
) as f:
encrypted_data = f.read()
# Decrypt Encrypted Data
decrypted_data = private_key.decrypt(
encrypted_data,
padding.OAEP(
mgf
=
padding.MGF1(
algorithm
=
hashes.SHA256()),
algorithm
=
hashes.SHA256(),
label
=
None
)
)
# Save Decrypted Data to File
with open
(
"decrypted_text.txt"
, "wb"
) as f:
f.write(decrypted_data) Explanation: We decrypt the message that was kept in encrypted_data.bin in this phase. The private key
created in Step 1 is used for this. This code shows how to read encrypted data from one file,
load a private key from another, use the RSA private key to decode the data, then save the
decrypted data to a file. This procedure is a component of the widely used RSA encryption
system, which protects data while it is in transit. To decode the encrypted data, use the private
key decrypt technique. This employs SHA-256 as the hash algorithm for both the encryption
algorithm and the mask generation function (MGF), using the Optimal Asymmetric
Encryption Padding (OAEP) scheme. The label has a None setting.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Output:
a.
Generation of decrypted_text.txt
Step 4: Verification: Comparing the data present in plaintext.txt & decrypted_data.txt we can say that the assessment of encryption & decryption is successful.
Output of plaintext.txt: Output of decrypted_file.txt
Conclusion: A strong technique for guaranteeing the privacy of sensitive data while it is being transmitted
is encryption. Encryption protects against unwanted access by transforming data into an
unintelligible format that can only be decoded by authorized parties. For encryption to be
effective, key creation, distribution, and storage must be done correctly. The system's overall
security is impacted by the hash functions and encryption methods chosen. Data is kept safe
during network transmission thanks to encryption. Protecting sensitive data, including login
credentials, financial transactions, and private correspondence, requires this. Confidential
information is protected against unwanted access by encryption. It provides an additional
degree of security, particularly in situations when data is susceptible to eavesdropping or
interception.