5

I have a .NET application. I need to store a text value encrypted in a file, then retrieve the encrypted value somewhere else in the code, and decrypt it.

I don't need the strongest or most secure encryption method on earth, just something that will suffice to say - I have the value encrypted, and am able to decrypt it.

I've searched a lot on the net to try and use cryptography, but most of the examples I find, don't clearly define the concepts, and the worst part is they seem to be machine specific.

Essentially, can someone please send a link to an easy to use method of encryption that can encrypt string values to a file, and then retrieve these values.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
JL.
  • 78,954
  • 126
  • 311
  • 459
  • "I don't need the strongest or most secure encryption method on earth" - then why encrypt it? If you don't use strong encryption and the information has value, it's very possible that someone **will** decrypt it. – TrueWill Sep 27 '09 at 14:48
  • 2
    It's like you're asking what security system to put in your building, but you're not telling us what's valuable in the building. The security system in a bank is very different than the security system at an embassy. We can't recommend an encryption algorithm until we know what is being protected and from what threat. Describe the resource you're protecting and the threat you're worried about before you decide to throw encryption at the problem; encryption might not be the right solution. An ACL might be the right solution, or some other security technology. – Eric Lippert Sep 27 '09 at 16:24
  • 2
    Also, don't forget that what makes encryption secure is the security of the key. *How are you going to do secure key management?* That's the hard problem. Using an off-the-shelf encryption algorithm is easy by comparison. – Eric Lippert Sep 27 '09 at 16:25

4 Answers4

4

StackOverflow's Extension library has two nice little extensions to encrypt and decrypt a string with RSA. I have used the topic here a few times myself but haven't tested it really, but it is a StackOverflow Extension library so I assume it is tested and stable.

Encrypt:

public static string Encrypt(this string stringToEncrypt, string key)
{
    if (string.IsNullOrEmpty(stringToEncrypt))
    {
        throw new ArgumentException("An empty string value cannot be encrypted.");
    }

    if (string.IsNullOrEmpty(key))
    {
        throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
    }

    System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
    cspp.KeyContainerName = key;

    System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
    rsa.PersistKeyInCsp = true;

    byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

    return BitConverter.ToString(bytes);
}

Decrypt:

public static string Decrypt(this string stringToDecrypt, string key)
{
    string result = null;

    if (string.IsNullOrEmpty(stringToDecrypt))
    {
        throw new ArgumentException("An empty string value cannot be encrypted.");
    }

    if (string.IsNullOrEmpty(key))
    {
        throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
    }

    try
    {
        System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
        cspp.KeyContainerName = key;

        System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
        rsa.PersistKeyInCsp = true;

        string[] decryptArray = stringToDecrypt.Split(new string[] { "-" }, StringSplitOptions.None);
        byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));

        byte[] bytes = rsa.Decrypt(decryptByteArray, true);

        result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
    }
    finally
    {
        // no need for further processing
    }

    return result;
}
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Tiax
  • 312
  • 3
  • 10
  • Exactly the answer I was looking for, something that works, and not overly complicated. I'll use this as a reference for learning more about RSA. Thanks again Tiax – JL. Sep 28 '09 at 06:36
  • Ok for short strings but it will fail for input strings longer than 86 chars. In general, RSA is not well suited for large bodies of data. – H H Sep 28 '09 at 16:13
1

If you're looking at doing symmetric encryption, then I'd consider the Enterprise Library Cryptography Application Block. David Hayden had a useful blog post about it, though its for Enterprise Library 2.0 (the current is 4.1), I think you will it is still useful.

RichardOD
  • 28,883
  • 9
  • 61
  • 81
  • Thanks Richard, have you used it before? – JL. Sep 27 '09 at 11:17
  • Yes- it was quite easy to use and the configuration tool was quite straight forward. I basically used in the scenario described in Deploying the Cryptography Application Block- http://msdn.microsoft.com/en-us/library/dd203351.aspx – RichardOD Sep 27 '09 at 11:23
  • @Richard, busy following the example in the blog, and then get to this point... http://s637.photobucket.com/albums/uu91/mleppan/?action=view&current=Capture.png, so does this mean that if I create an encrypted file on my local dev machine, I can't decrypt it on a hosted web server? – JL. Sep 27 '09 at 11:29
  • No, that is the mechanism that lets you protect the shared key. The configuration tool lets you export that key to another machine. The deployment section provides details on how you can share this key- http://msdn.microsoft.com/en-us/library/dd203351.aspx – RichardOD Sep 27 '09 at 11:32
1

In .NET you can use an instance of a SymmetricAlgorithm. Here on Stack Overflow there is a question that demonstrates how to encrypt and decrypt strings using a password. How you are going to handle the password is a different matter but I assume that you are not too concerned about that and simply want to "hide" some text from the prying eye.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
0

Here is a blog post using the cryptography library that .NET comes with for a symmetric encryption/decryption.

A symmetric algorithm uses the same key to encrypt and decrypt, much as you use one key to lock and unlock your car door.

A public key algorithm would use one key to encrypt and another to decrypt, so, I can send you a file that is encrypted, and know that only you can decrypt it, as you have kept your key very secure and private.

http://blog.binaryocean.com/2006/01/08/NETSymmetricEncryption.aspx

James Black
  • 41,583
  • 10
  • 86
  • 166