How to encrypt and decrypt numeric data in c#. [Resolved]

Posted by Allemahesh under C# on 7/25/2013 | Points: 10 | Views : 10283 | Status : [Member] [MVP] | Replies : 3
I have integer data. Now I want to encrypt and decrypt this data in c#. Can any one tell me what is best way to do this?




Responses

Posted by: Jitendrasoft09 on: 7/25/2013 [Member] [MVP] Starter | Points: 50

Up
0
Down

Resolved
Hi Allemahesh,

You need to pass one parameter after converting integer to string in below code is mentioned.

To encrypt-

public static string Encrypt(string plainText)

{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

byte[] cipherTextBytes;

using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}


To decrypt-

public static string Decrypt(string encryptedText)

{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}


Refer below link for more inforamtion-
http://social.msdn.microsoft.com/Forums/vstudio/en-US/d6a2836a-d587-4068-8630-94f4fb2a2aeb/encrypt-and-decrypt-a-string-in-c

Jitendra Kumar
If my post helps you, plz mark as an answer.

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 7/25/2013 [Member] Starter | Points: 25

Up
0
Down
HI Alle,

refer this links it is very clear i hope it might be help full to you

http://stackoverflow.com/questions/1200132/encrypting-and-decrypting-numbers-with-net http://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 7/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C
http://www.codeproject.com/Articles/14150/Encrypt-and-Decrypt-Data-with-C

If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response