You have been soliciting code changes from everyone that responds. This is not about fixing bugs, it’s about you not having the required background to write such software safely. I’m not going to teach you.
My suggestion is you delete the codebase until you know what you are doing.
Maybe take a look at a book like https://www.manning.com/books/real-world-cryptography or https://nostarch.com/seriouscrypto