Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, how to check if value is encrypted using MD5 passphrase?

Tags:

c#

encryption

I have the following code to encrypt a value (listed below). Now I would like to write a bool isEncrypted() method. Is there a fool proof and reliable way to check if a value has been encrypted using this function. I have the decrypt routine and can control the pass phrase, but not sure if that will help.

The reason is - when the app first runs, values in a configuration file are not encrypted, in this case the app should auto encrypt these values. On 2nd run I don't want to encrypt again because obviously that would cause havoc. Lastly I don't want to have to add an isEncrypted attribute to the config value. I want it to work and look as dynamic as possible.

So far I am leaning towards using the len (128) as deciding factor, but there is always a remote chance of the unencrypted value also being this length.

Thanks in advance.

public static string encrypt(string text)
    {
        // Locals
        var passphrase = "5ab394ed-3920-4932-8d70-9c1b08f4ba4e";
        byte[] results;
        var utf8 = new UTF8Encoding();

        // Step 1. We hash the passphrase using MD5
        // We use the MD5 hash generator as the result is a 128 bit byte array
        // which is a valid length for the TripleDES encoder we use below
        var hashProvider = new MD5CryptoServiceProvider();
        var tdesKey = hashProvider.ComputeHash(utf8.GetBytes(passphrase));

        // Step 2. Create a new TripleDESCryptoServiceProvider object
        // Step 3. Setup the encoder
        var tdesAlgorithm = new TripleDESCryptoServiceProvider
        {
            Key = tdesKey,
            Mode = CipherMode.ECB,
            Padding = PaddingMode.PKCS7
        };

        // Step 4. Convert the input string to a byte[]
        var dataToEncrypt = utf8.GetBytes(text);

        // Step 5. Attempt to encrypt the string
        try
        {
            var encryptor = tdesAlgorithm.CreateEncryptor();
            results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
        }
        finally
        {
            // Clear the TripleDes and Hashprovider services of any sensitive information
            tdesAlgorithm.Clear();
            hashProvider.Clear();
        }

        // Step 6. Return the encrypted string as a base64 encoded string
        return Convert.ToBase64String(results);
    }
like image 860
JL. Avatar asked Nov 26 '09 10:11

JL.


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

What you could do in the isEncrypted method is to try to decrypt the message.
Since you are using PKCS7 padding most likely an unencrypted message will fail to decrypt since the padding does not conform to the set padding mode.

The decryption will throw an exception and you'll have to catch this and return false in this case.

There is a remote chance that the decryption will go through (when the message is not encrypted) if the data conforms to the padding mode. This is however most unlikely.

What I would do in this case would be to add some kind of flag in the encrypted data or append some data to encrypted message since I can then remove it in the decryption. This would be the most foolproof way.

like image 65
Sani Singh Huttunen Avatar answered Sep 30 '22 00:09

Sani Singh Huttunen


First, as a serious issue, it's an exceedingly poor idea to use cryptographic primitives on your own. You've chosen to use the Electronic Codebook mode of encryption, which has the property that identical plaintext blocks produce identical cyphertext blocks. Check out the example at Wikipedia.

That said, a simple solution is to prepend a token such as 'ENC:' to the encrypted password. If you need to worry about malicious tampering with the config file, you should proceed to use a message authentication code, such as HMAC.

like image 21
inklesspen Avatar answered Sep 30 '22 01:09

inklesspen