views:

442

answers:

4

What are Encryption techniques available with .NET (using C#). I have a numeric value with me which I want to encrypt to a string representation. Which one has decrypting support ?

+5  A: 

Encryption (which is provided by the .NET framework / BCL, not C# the language) typically works on bytes. But that is fine; numbers are easy to represent as bytes, and the output bytes can be written as a string via Convert.ToBase64String.

So "all of them, indirectly"...

See System.Security.Cryptography on MSDN

(re decrypting: an encryption can be decrypted; a hash cannot (hopefully); so as long as you don't look at hashing functions, you should be fine)

Marc Gravell
Thanks, Marc, I was just going to mention that C# doesn't do encryption.
John Saunders
Agreed that the .NET is semantically correct. However, I think you may want to leave a reference to C# in the original question. People coding in C# will be looking for a solution written out in that language, regardless of the location of the libraries. Since "Google is the homepage" there is value in allowing questions to be worded in what may be a popular phrasing, even if not semantically perfect.
Traples
Does that keep everyone happy?
Marc Gravell
Yes, my happiness is directly tied to the inclusion of two words in a obscure post on SO :) Achieving this level of satiation, I'm off to the lecture circuit to capitalize on it.
Traples
+3  A: 

System.Security.Cryptography -

The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data, as well as many other operations, such as hashing, random number generation, and message authentication.

Example Walkthrough demonstrates how to encrypt and decrypt content.

gimel
+1  A: 

I would start by looking at the Cryptography namespace. You can implement your own decrypt/encrypt string functions. Here is a good example.

SwDevMan81
+3  A: 

Whatever you do, don't roll your own encryption algorithm. The System.Security.Cryptography namespace will contain everything you need:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String secret = "Zomg!";
            byte[] secretBytes = ASCIIEncoding.ASCII.GetBytes(secret);

            // One-way hashing
            String hashedSecret =
                BitConverter.ToString(
                    SHA512Managed.Create().ComputeHash(secretBytes)
                );

            // Encryption using symmetric key
            Rijndael rijndael = RijndaelManaged.Create();
            ICryptoTransform rijEncryptor = rijndael.CreateEncryptor();
            ICryptoTransform rijDecryptor = rijndael.CreateDecryptor();
            byte[] rijndaelEncrypted = rijEncryptor.TransformFinalBlock(secretBytes, 0, secretBytes.Length);
            String rijndaelDecrypted =
                ASCIIEncoding.ASCII.GetString(
                    rijDecryptor.TransformFinalBlock(rijndaelEncrypted, 0, rijndaelEncrypted.Length)
                );

            // Encryption using asymmetric key
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string rsaParams = rsa.ToXmlString(true); // you can store the public key in a config file
                                                      // which allows you to recreate the file later

            byte[] rsaEncrypted = rsa.Encrypt(secretBytes, false);
            String decrypted =
                ASCIIEncoding.ASCII.GetString(
                    rsa.Decrypt(rsaEncrypted, false)
                );

            // Signing data using the rsaEncryptor we just created
            byte[] signedData = rsa.SignData(secretBytes, new SHA1CryptoServiceProvider());
            bool verifiedData = rsa.VerifyData(secretBytes, new SHA1CryptoServiceProvider(), signedData);
        }
    }
}
Juliet