Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > encrypting string

Reply
Thread Tools

encrypting string

 
 
JJ
Guest
Posts: n/a
 
      10-31-2006
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


 
Reply With Quote
 
 
 
 
John Timney \(MVP\)
Guest
Posts: n/a
 
      10-31-2006
quite an easy google search that one
http://www.example-code.com/csharp/aesEncryptCC.asp

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog


"JJ" <(E-Mail Removed)> wrote in message
news:etE0EDU$(E-Mail Removed)...
>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
>



 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      11-01-2006
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(reque st.ApplicationPath);

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

string key = section.DecryptionKey;

return key;

}


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"JJ" <(E-Mail Removed)> wrote in message
news:etE0EDU$(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Bill Dekleris
Guest
Posts: n/a
 
      11-01-2006
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/defaul...yptFileAESCode
http://msdn.microsoft.com/msdnmag/issues/03/11/AES/

Regards,

Bill Dekleris.

John Timney (MVP) wrote:
> quite an easy google search that one
> http://www.example-code.com/csharp/aesEncryptCC.asp
>



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


http://www.infosnap.eu

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

-----------------------------------------------------------------------------------------
 
Reply With Quote
 
John Timney \(MVP\)
Guest
Posts: n/a
 
      11-01-2006
Now I never saw that.....well spotted!!

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog


"Bill Dekleris" <(E-Mail Removed)> wrote in message
news:OUwA17b$(E-Mail Removed)...
> 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/defaul...yptFileAESCode
> http://msdn.microsoft.com/msdnmag/issues/03/11/AES/
>
> Regards,
>
> Bill Dekleris.
>
> John Timney (MVP) wrote:
>> quite an easy google search that one
>> http://www.example-code.com/csharp/aesEncryptCC.asp
>>

>
>
> --
> -----------------------------------------------------------------------------------------
>
>
> http://www.infosnap.eu
>
> InfoSnap the powerful, all-purpose information and knowledge-base
> manager.
>
> -----------------------------------------------------------------------------------------


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encrypting a short string? erikcw Python 17 02-20-2008 03:06 AM
Encrypting a query string in URL Andy G ASP .Net 4 05-03-2005 09:35 PM
compressing or encrypting a String Wendy S Java 7 01-03-2005 10:49 AM
Encrypting/Decrypting Connection String VB Programmer ASP .Net 3 11-30-2004 06:08 AM
encrypting query string Onur Bozkurt ASP .Net 8 07-24-2003 11:01 AM



Advertisments