encrypting string

J

JJ

I need to encrypt credit card # and store that in a database (and be able to
decrypt it). Any codes that use strong encyption algorithm like AES 256 on
the web that I can copy and paste?

Thanks
 
C

Cowboy \(Gregory A. Beamer\)

The encryption algorithms are not that difficult. There are plenty of triple
DES examples in C# that you can download. To switch to Rijndael (AES), you
simply switch libraries from the TripleDES libs to Rijndael.

Here is a simple algorithm that incorporates salt:

public class RijndaelEncryption

{

public static string EncryptData(string data, string key, string salt)

{

byte[] saltByte = Encoding.ASCII.GetBytes(salt);

PasswordDeriveBytes secretKey = new PasswordDeriveBytes(key, saltByte,
"MD5", 2);

byte[] desIV = secretKey.GetBytes(16);

byte[] keyBytes = secretKey.GetBytes(32);

return EncryptData(keyBytes, desIV, data);

}

public static string DecryptData(string data, string key, string salt)

{

byte[] saltByte = Encoding.ASCII.GetBytes(salt);

PasswordDeriveBytes secretKey = new PasswordDeriveBytes(key, saltByte,
"MD5", 2);

byte[] desIV = secretKey.GetBytes(16);

byte[] keyBytes = secretKey.GetBytes(32);

return DecryptData(data, keyBytes, desIV);

}

public static string EncryptData(byte[] desKey, byte[] desIV, string data)

{

MemoryStream output = new MemoryStream();

byte[] byteData = Encoding.UTF8.GetBytes(data);

//Use the TripleDES symmetric encryption algorithm to encrypt our data.
Without an IV, the

//same input block of plaintext will encrypt to same output block of
ciphertext. IV guarantees

//output of two identical plaintext blocks are different.

RijndaelManaged des = new RijndaelManaged();

ICryptoTransform transform = des.CreateEncryptor(desKey, desIV);

CryptoStream crypt = new CryptoStream(output, transform,
CryptoStreamMode.Write);

crypt.Write(byteData, 0, byteData.Length);

crypt.Close(); output.Close();

return Convert.ToBase64String(output.ToArray());

}



public static string DecryptData(string data, byte[] desKey, byte[] desIV)

{

MemoryStream output = new MemoryStream();

byte[] byteData = Convert.FromBase64String(data);

//Use the TripleDES symmetric encryption algorithm to decrypt our data. In
order for the ciphertext to be

//successfully decrypted, the exact same key and iv must be used when
initially encryted.

RijndaelManaged des = new RijndaelManaged();

CryptoStream crypt = new CryptoStream(output, des.CreateDecryptor(desKey,
desIV), CryptoStreamMode.Write);

crypt.Write(byteData, 0, byteData.Length);

crypt.Close();

output.Close();

return Encoding.UTF8.GetString(output.ToArray());

}

//This works

public static string GenerateSalt()

{

byte[] buffer1 = new byte[0x10];

new RNGCryptoServiceProvider().GetBytes(buffer1);

return Convert.ToBase64String(buffer1);

}

public static string GetKey(HttpContext context)

{

//Request.ApplicationPath

HttpRequest request = context.Request;

Configuration config =
WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);

MachineKeySection section =
(MachineKeySection)config.GetSection("system.web/machineKey");

string key = section.DecryptionKey;

return key;

}
 
B

Bill Dekleris

Provided, of course, that one is willing to use Chilkat.Crypt component,
and pay for it, as well. :)

For a "home made" solution, you could take a look at these links to help
you with AES encryption in .NET, C#. They look quite good:

http://channel9.msdn.com/wiki/default.aspx/SecurityWiki.EncryptFileAESCode
http://msdn.microsoft.com/msdnmag/issues/03/11/AES/

Regards,

Bill Dekleris.


--
-----------------------------------------------------------------------------------------


http://www.infosnap.eu

InfoSnap · the powerful, all-purpose information and knowledge-base manager.

-----------------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top