4
succ
6y

Noob question

Is it better to implement a cryptpgraphic algo in a function or in a class? Also how?

More info:
I have a cryptography class and I really enjoy implementing the different techniques that we study in class. At first I was just implementing the techniques in a simple function with 3 parameters; key, message and a bool for encryption or decryption. But as they are getting more complex, it is becoming harder to continue implementing them in a single function block. So I thought of using a class but ran into the problem of how do I even do that? Do I make different methods for key generation, encrypting and decrypting?

P.S. It's really just for learning how the crypto technique works and not for anything serious.

Comments
  • 2
    split the function in different functions.
  • 3
    OK no, first of all a bool for decryption or encryption? Separate classes please.

    As for the algorithm itself, keep that as a modular class. You should be able to plug in new features as and when they arrive. The class itself should have methods for encryption and decryption, and the entire assembly should ideally be in its own namespace.
  • 0
    @monr0e So something like,

    xyz = new crypto(key)
    enc = xyz.encrypt(string)
    dec = xyz.decrypt(string)
  • 1
    @succ yes. Google "c# password hashing" for the modularity that I'm referring to. Make everything, or at least what you can, self contained.
  • 2
    As long as you don't do it the java way
  • 1
  • 1
    @succ bear in mind that cryptography is a principle that changes very quickly. Try and make everything that you can adaptable.
  • 0
    @monr0e Thanks for you help. I'll try to make it modular if possible. :)
  • 1
    @succ the more modular you make a given concept, the more easily you can translate it to another project. It cuts down work hours, and if you specialise in blockchain tech it will cut down your hours no end. That's assuming you work in the same crypto fork every time.
  • 2
    @succ
    1. Never roll your own encryption to production. you will end up with encraption.
    2. Use a polymorph based solution that has a lifecycle.
    init/encrypt/decrypt/destroy/gen key/get key/use key for example. all experiment should use that super type as basis.
    3. all IV, cycles, hmacs, hashs and various internals should stay that way.

    have fun!
  • 3
    @magicMirror what he said. It's good for a school project/learning but never use your own crypto in prod!

    I'd suggest too to stay flexible when implememting it. Start with an interface, and then have one class implementing that interface for every algo you implement.
  • 1
    @magicMirror Wow. Exactly what I was looking for. Thank you very much.

    @Wack Well noted. Never going to use for prod. Not like I'm going to be able to anyway. Still have some more years of college before even thinking about a proper job.
Add Comment