Encrypt and decrypt numbers with .NET

What are the encryption methods available with .NET (using C #). I have a numeric value with me that I want to encrypt into a string representation. Who has the decryption support?

+4
source share
4 answers

Encryption (which is provided by the .NET / BCL platform, not C #), usually works with bytes. But it normal; numbers are easy to represent as bytes, and output bytes can be written as strings through Convert.ToBase64String .

So, "all of them, indirectly" ...

See System.Security.Cryptography on MSDN

(re-decryption: encryption can be decrypted, the hash cannot (hopefully), so until you look at the hashing functions, you should be fine)

+5
source

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.

An example pass shows how to encrypt and decrypt content.

+5
source

Whatever you do, do not roll back 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); } } } 
+3
source

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

+1
source

All Articles