-1

Very brief background on the question: I'm a CS student and just started a new apprentice software developer position. I'm mostly working with C# which I do not have much experience with (most of what I have written for class/projects was in Java), which my manager is aware of so I don't think it'd be a problem to just ask him or one of the other senior developers. However, I would like to demonstrate that I am able to figure some things out on my own so I figured I'd ask here first.

Essentially what I have been tasked with doing is to modify a class designed to encrypt and decrypt various pieces of information like customer information. My manager has asked that I modify the program to be able to initiate the encryption using one of several salt values, each of which is static and associated with a given category of information. He recommended I use an enumerator to contain the salt values which I could then pull into the encrypt and decrypt methods based on the type of information being encrypted, which is passed to those methods as a string parameter.

I have browsed a number of threads similar to this question which have given me a decent idea of how to go about creating an enumerator-like structure to contain a number of pairs of strings (with the key being the information type and the value being the associated salt). However, I'm not sure how I can then pass those values to the other methods in the class, as I haven't been able to find a way of directly passing them as parameters to the encrypt and decrypt methods. There are a few messy ways I think might work but I imagine there has to be a more refined solution that I'm not aware of due to my relative inexperience with c#.

If I can provide any additional information please let me know. Sorry if the question is hard to understand, I've lurked on Stack Overflow for years but this is my first time posting a question so I may not have expressed my issue clearly. Thank you in advance for any help you can provide.

Nick
  • 33
  • 5
  • 1
    What are the types of the values? Could you use a pattern similar to [`Color`](https://msdn.microsoft.com/en-us/library/system.drawing.color.aspx) with several pre-defined "constant" values? – D Stanley Aug 28 '17 at 22:12
  • The values that I need to pull are strings. I will give your suggestion a shot and get back to you - I saw a few people recommending that as a means of creating a kind of string-typed enumerator but have not tried to implement that particular methodology just yet. – Nick Aug 28 '17 at 22:14

1 Answers1

0

Encryption does not use salt. It uses keys, and possibly random padding. Salt is for hashes, and in general should be different each and every time, so storing them in code does not make sense.

Crypto keys don't change as often, but they they should be capable of being changed too (in case of compromise), so storing those in code doesn't make sense either. They should go into config, or if they are sensitive keys they should go into a secure key store. If you don't have a secure place to store them, you can encrypt and decrypt them using DPAPI.

Once you have determined where they are to be stored, you can write some code to read them into a Dictionary<enum, string> or Dictionary<enum, byte[]>, and use that. Example:

enum KeyType
{   
    Customer = 1,
    Administrator = 2,
    Application = 3
}

class Encryptor
{
    static readonly Dictionary<KeyType, string> keyLookup = new Dictinary<KeyType, string>();

    static public Encryptor()
    {
        keyLookup.Add(KeyType.Customer, GetCustomerCryptoKey());
        keyLookup.Add(KeyType.Administrator, GetAdminCryptoKey());
        keyLookup.Add(KeyType.Application, GetApplicationCryptoKey());
    }

    public string Encrypt(KeyType keyType, string input)
    {
        var key = keyLookup[keyType];
        return EncryptInternal(input, key);
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Thank you for your input, in response I'll try to explain why this request confused me as well. I know very little about encryption/cryptography so I had to go through a kind of crash course on creating hashed values using salts (this is what this class is intended to do, I may be misusing the terms "encrypt" and "decrypt"). Based on my limited understanding I spent the morning writing code to generate random salt values to be combined with a text value to create the hashed value. As such I was pretty confused when my manager told me that the salt values were supposed to remain constant. – Nick Aug 28 '17 at 22:31