473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

encrypting string

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
Oct 31 '06 #1
4 1614
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" <no****@nospam.comwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
>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

Oct 31 '06 #2
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" <no****@nospam.comwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
>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

Nov 1 '06 #3
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.

-----------------------------------------------------------------------------------------
Nov 1 '06 #4
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" <qu****@hol.grwrote in message
news:OU**************@TK2MSFTNGP03.phx.gbl...
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.

-----------------------------------------------------------------------------------------
Nov 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2009
by: steve | last post by:
Hi, I know there are a few free and paid php source code encryption scripts around. Has anyone used one, and any feedback? I am interested in encrypting source that is placed on a remote host....
6
2063
by: Dayne | last post by:
Guys, I am writing a database application(vb.net , sql server) and is presently storing the connection settings in a xml file...not very secure though. What is a safer method in a dynamic...
4
7023
by: Andy G | last post by:
If users forget there passwords I want to send a link to them through email so they can click on a link and go to a change password page. eBay does this by sending you a url that looks something...
7
1324
by: Bob Hollness | last post by:
OK, this has me pulling my hair out. All I want to do is encrypt/decrypt strings. They may be up to 400 characters in length though. So, I assume System.Security.Cryptography is the one to use....
11
1923
by: frizzle | last post by:
Hi there, I need an encrypting function, but haven't got a clue where to start. First a string has to be encrypted with two different encryption keys. Both output should be anything a-z / A-Z /...
3
1748
by: Thirsty Traveler | last post by:
I hear that MD5 is not recommended for encrypting database passwords in that it can be compromised. Does anyone have a recomendation (SHA-1, etc.) on an algorithm that would be more appropriate.
5
2457
by: Chris Dunaway | last post by:
I have an application which is installed on a network share to be run from one or more workstations. I have granted trust to the applications on the workstations and the appropriate permissions on...
2
1843
by: Parrot | last post by:
I cannot get an answer as to why my session state no longer remains active between webpages after working for 2 years. So I want to try to pass data thru query strings in my url. I tried to use...
2
2256
by: SeeSharp Bint | last post by:
Visual Studio 2005, dotnet, c#. Microsoft SQL Server. Windows XP forms application. Temporarily, for my database application, I have been storing the various elements of database connection...
3
2456
by: Tery | last post by:
I'm trying to implement the Handango.com HTTP POST registration method. The instructions are here: http://www.handango.com/marketing/developerTeam/HTTP_Post_Reg_Model_How2.doc I'm stuck on how...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.