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 ?
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)
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.
I would start by looking at the Cryptography namespace. You can implement your own decrypt/encrypt string functions. Here is a good example.
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);
}
}
}