encrypting string

Discussion in 'ASP .Net' started by JJ, Oct 31, 2006.

  1. JJ

    JJ Guest

    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
     
    JJ, Oct 31, 2006
    #1
    1. Advertising

  2. John Timney \(MVP\), Oct 31, 2006
    #2
    1. Advertising

  3. 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;

    }


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

    *************************************************
    Think outside of the box!
    *************************************************
    "JJ" <> wrote in message
    news:etE0EDU$...
    >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
    >
     
    Cowboy \(Gregory A. Beamer\), Nov 1, 2006
    #3
  4. 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.

    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.

    -----------------------------------------------------------------------------------------
     
    Bill Dekleris, Nov 1, 2006
    #4
  5. 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" <> wrote in message
    news:OUwA17b$...
    > 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.
    >
    > 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.
    >
    > -----------------------------------------------------------------------------------------
     
    John Timney \(MVP\), Nov 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Onur Bozkurt

    encrypting query string

    Onur Bozkurt, Jul 23, 2003, in forum: ASP .Net
    Replies:
    8
    Views:
    598
    Munsifali Rashid
    Jul 24, 2003
  2. VB Programmer

    Encrypting/Decrypting Connection String

    VB Programmer, Nov 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    7,643
    Sahil Malik
    Nov 30, 2004
  3. Andy G

    Encrypting a query string in URL

    Andy G, May 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    8,393
  4. Wendy S
    Replies:
    7
    Views:
    548
    Chris Uppal
    Jan 3, 2005
  5. erikcw

    Encrypting a short string?

    erikcw, Feb 11, 2008, in forum: Python
    Replies:
    17
    Views:
    831
    Steve Holden
    Feb 20, 2008
Loading...

Share This Page