print("Plaintext size is "+str(len(plaintext))+", with "+str(chunks)+" chunks")
ciphertext="";
forchunkinrange(chunks):
start=chunk*chunksize
end=(chunk+1)*chunksize
if(chunk+1)*chunksize>len(plaintext):
end=len(plaintext)
print("Processing chunk "+str(chunk+1)+" of "+str(chunks)+". Starting at "+str(start)+" and stopping at "+str(end)+". The length is "+str(len(plaintext[start:end])))
ciphertext+=self.pub.encrypt(
plaintext[start:end],
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
print("Plaintext encrypted, ciphertext length is "+str(len(ciphertext))+" bytes.")
returnciphertext
else:
raiseKeyError("Encryption failed because identity does not hold a private key")
defdecrypt(self,ciphertext):
ifself.prv!=None:
print("Ciphertext length is "+str(len(ciphertext))+". ")
print("Processing chunk "+str(chunk+1)+" of "+str(chunks)+". Starting at "+str(start)+" and stopping at "+str(end)+". The length is "+str(len(ciphertext[start:end])))
plaintext+=self.prv.decrypt(
ciphertext[start:end],
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
returnplaintext;
else:
raiseKeyError("Decryption failed because identity does not hold a private key")
defsign(self,message):
ifself.prv!=None:
signer=self.prv.signer(
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
signer.update(message)
returnsigner.finalize()
else:
raiseKeyError("Signing failed because identity does not hold a private key")