PS - Practical 2 PDF
PS - Practical 2 PDF
PS - Practical 2 PDF
A digital signature is a mathematical technique used to validate the authenticity and integrity of a message, software, or
digital document.
Key Generation Algorithms: Digital signature is electronic signatures, which assure that the message was sent by a
particular sender. While performing digital transactions authenticity and integrity should be assured, otherwise, the data
can be altered or someone can also act as if he was the sender and expect a reply.
Signing Algorithms: To create a digital signature, signing algorithms like email programs create a one-way hash of the
electronic data which is to be signed. The signing algorithm then encrypts the hash value using the private key (signature
key). This encrypted hash along with other information like the hashing algorithm is the digital signature. This digital
signature is appended with the data and sent to the verifier. The reason for encrypting the hash instead of the entire message
or document is that a hash function converts any arbitrary input into a much shorter fixed-length value. This saves time as
now instead of signing a long message a shorter hash value has to be signed and moreover hashing is much faster than
signing.
Signature Verification Algorithms : Verifier receives Digital Signature along with the data. It then uses Verification
algorithm to process on the digital signature and the public key (verification key) and generates some value. It also applies
the same hash function on the received data and generates a hash value. Then the hash value and the output of the
verification algorithm are compared. If they both are equal, then the digital signature is valid else it is invalid.
Program:
# DigitalSignatures Practical 2
# Creating a signature
def sign(message, private):
message = bytes(str(message), 'utf-8')
sig = private.sign(
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
return sig
# Verification of Signature
def verify(message, sig, public) :
message = bytes(str(message), 'utf-8')
try:
retval = public.verify(
sig,
message,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
return True
except InvalidSignature:
return False
except:
print("Error Executing public_key.verify")
return False
# Main Function
if __name__ == '__main__' :
print("Generation of Private and Public keys using RSA algorithm")
pr, pu = generate_keys()
print(pr)
print(pu)
message = "This is a secret message"
print("Creating a signature")
sig = sign(message, pr)
print(sig)
print("Verification of Signature")
correct = verify(message, sig, pu)
print(correct)
if correct:
print("Success! Signature Verified")
else:
print("Error! Signature not verified")
###Various Imports###
def generate_keys():
Defines a function generate_keys.
private = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())
➢ private is a variable.
➢ generate_private_key is a member function of class rsa
Parameters:
➢ public_exponent (int) – The public exponent of the new key. Almost everyone should use 65537.
➢ key_size (int) – The length of the modulus in bits. It is strongly recommended to be at in 512, 2048 or 4096.
➢ backend=default_backend() - cryptography was originally designed to support multiple backends, but this design
has been deprecated. You can get the default backend by calling default_backend().
public = private.public_key() - Public_key() is a member function of class private. Returns an RSA public key object
corresponding to the values of the private key.
return private, public - Returns private and public variables when the function generate_keys() is called.
### Creating a signature###
PSS (Probabilistic Signature Scheme) is a signature scheme defined in RFC 3447. This is the recommended padding algorithm
for RSA signatures. It cannot be used with RSA encryption.
Parameters:
mgf – A mask generation function object. At this time the only supported MGF is MGF1.
salt_length (int) – The length of the salt. It is recommended that this be set to PSS.MAX_LENGTH to get the maximum salt
length available.
algorithm – An instance of HashAlgorithm. SHA-256 is a cryptographic hash function from the SHA-2 family. It produces a
256-bit message digest. A cryptographic hash function takes an arbitrary block of data and calculates a fixed-size bit string
(a digest), such that different data results (with a high probability) in different digests.
Verify one block of data was signed by the private key associated with this public key.
Parameters:
➢ signature (bytes) – The signature to verify. (Same as Sign parameters)
➢ data (bytes) – The message string that was signed. (Same as Sign parameters)
➢ padding – An instance of AsymmetricPadding. (Same as Sign parameters)
➢ algorithm – An instance of HashAlgorithm or Prehashed if the data you want to verify has already been hashed. (Same
as Sign parameters)
➢ Returns:None
if __name__ == '__main__' :
Before executing code, Python interpreter reads source file and define few special variables/global variables.
If the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable
to have a value “__main__”.
pr, pu = generate_keys()
➢ Public and private keys are generated using generate_keys() function and values returned are stored in pr and pu
variable.
print(pr)
➢ Value of pr is printed.
print(pu) -
➢ Value of pu is printed.
if correct:
print("Success! Signature Verified")
else:
print("Error! Signature not verified")