0% found this document useful (0 votes)
9 views5 pages

Exp 3 Csy

The document outlines the implementation of a digital signature using cryptographic techniques in Python, focusing on the SHA-256 hash function and asymmetric encryption. It details the digital signature process, including message hashing, signature generation, transmission, and verification, while highlighting the advantages of digital signatures such as integrity, authentication, and non-repudiation. Additionally, it provides Python code examples utilizing libraries for generating keys, signing messages, and verifying signatures.

Uploaded by

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

Exp 3 Csy

The document outlines the implementation of a digital signature using cryptographic techniques in Python, focusing on the SHA-256 hash function and asymmetric encryption. It details the digital signature process, including message hashing, signature generation, transmission, and verification, while highlighting the advantages of digital signatures such as integrity, authentication, and non-repudiation. Additionally, it provides Python code examples utilizing libraries for generating keys, signing messages, and verifying signatures.

Uploaded by

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

Experiment 3: Implement a Digital Signature

Aim: To implement a digital signature using cryptographic techniques in Python.

Theory:

Message Digest

A message digest is a fixed-length hash value generated from a message using a


cryptographic hash function. It ensures that even the smallest modification in the original
message results in a completely different hash value. This property is crucial for verifying
data integrity.

Hash Function (SHA-256)

SHA-256 (Secure Hash Algorithm - 256 bit) is a cryptographic hash function that produces a
unique 256-bit (32-byte) hash value for an input message. It is widely used in security
protocols like SSL/TLS, blockchain technology, and digital signatures.

Properties of SHA-256:

• Deterministic: The same input always produces the same hash.

• Fast Computation: Efficiently generates a hash value.

• Preimage Resistance: It is infeasible to determine the original message from the hash.

• Small Change, Big Impact: Even a minor change in input causes a drastic change in the
hash.

Asymmetric Encryption

Asymmetric encryption involves two keys:

1. Public Key: Shared with everyone, used to verify digital signatures.

2. Private Key: Kept secret, used to create digital signatures.

This method provides higher security compared to symmetric encryption, where a single key
is used for both encryption and decryption.
Public & Private Key in Digital Signatures

• The private key is used to sign a message, generating a unique signature.

• The public key is used by the recipient to verify that the message was signed by the
corresponding private key holder.

This ensures authenticity, as only the private key owner can generate a valid signature.

Digital Signature Process:

1. Message Hashing: The sender generates a hash (message digest) using SHA-256.

2. Signature Generation: The hash is encrypted using the sender’s private key to create a
digital signature.

3. Transmission: The original message and the digital signature are sent to the recipient.

4. Verification: The recipient decrypts the signature using the sender’s public key and
compares the extracted hash with the hash of the received message. If they match, the
signature is valid.

Advantages of Digital Signatures:

✔ Ensures integrity – detects message tampering.

✔ Provides authentication – verifies sender’s identity.

✔ Guarantees non-repudiation – sender cannot deny sending the message.


Python Libraries & Methods

1) cryptography.hazmat.primitives

A module providing low-level cryptographic functions such as encryption, hashing, and


digital signature generation. Used for secure cryptographic implementations.

2) rsa

A Python library that allows RSA-based encryption, decryption, key pair generation, and
digital signatures. It is widely used for implementing public-key cryptography.

3) padding

Padding is used to ensure that the input message conforms to a required block size for
encryption. Without padding, RSA encryption may fail for short messages.

Methods Used:

1) rsa.generate_private_key()

• Generates a new RSA private key.

• Used for signing messages and decrypting data.

2) padding.PKCS1v15()

• A widely used padding scheme for RSA encryption and digital signatures.

• Ensures that the input data is formatted correctly before encryption.

Code :

from cryptography.hazmat.primitives.asymmetric import rsa, padding

from cryptography.hazmat.primitives import hashes

private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)

message = b"Hello, this is a signed message!"


public_key = private_key.public_key()

signature = private_key.sign(

message,

padding.PKCS1v15(),

hashes.SHA256()

print("Original message: ", message.decode())

print("Digital Signature: ", signature.hex())

try:

public_key.verify(

signature,

message,

padding.PKCS1v15(),

hashes.SHA256()

print("\nVerification Successful: The Signature is valid.")

except Exception:

print("\nVerification Failed: The signature is invalid.")

Conclusion:

This digital signature using rsa implementation demonstrates securely signing a message with
a private key and verifying its authenticity using the corresponding public key.

You might also like