locked
3-DES algorithm use for Card number in asp.net c# RRS feed

  • Question

  • User-803914957 posted

     I need to solve the below task:

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

    Use the 32-bit key F8660BF9063B1BBEFEA2BC413C8514FE to encrypt card number 5441191000005226.

    If you encrypt the card number using any library, the encrypted value (result) should be 01AE5ED7C78D5C38. When decrypt 01AE5ED7C78D5C38 using the same key, the decrypted value (result) should be 5441191000005226.

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

    I am suggested to use 3-DES. 

    I have found a solution from:

    https://www.aspforums.net/Threads/448300/3-DES-algorithm-use-for-Card-number-in-aspnet-c/Replies/2#Replies

    But I am guided from supervisor that encryption result should be exactly

    5441191000005226   to  01AE5ED7C78D5C38

     and decryption result in reverse way.

    Anybody can help me?

    Thanks in advance.

     

    Wednesday, October 31, 2018 3:53 AM

All replies

  • User-893317190 posted

    Hi Alauddin Dipu,

    You could use AES to encrypt and decrypt your card number.

    Below is a small sample.

    //the first parameter is your card number , the second parameter is the key, 
    //the last parameter is the file path where you want to save your encrypted card number
    public static void Encrypt(string plainText, string password,string outputFilePath) { byte[] salt1 = new byte[] { 0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64 }; string data = plainText; Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(password, salt1); Aes encryptor = Aes.Create(); encryptor.KeySize = 128; encryptor.BlockSize = 128; encryptor.Key = k1.GetBytes(128 / 8); encryptor.IV = k1.GetBytes(128 / 8); FileStream fsinput = new FileStream(outputFilePath, FileMode.Open); CryptoStream encrypt = new CryptoStream(fsinput, encryptor.CreateEncryptor(), CryptoStreamMode.Write); byte[] utfd1 = new System.Text.UTF8Encoding(false).GetBytes(data); encrypt.Write(utfd1, 0, utfd1.Length); encrypt.FlushFinalBlock(); encrypt.Close(); } // the first parameter is the file path where your store your encrypted card number , the second parameter is the file path
    // where you want to save your decrpyted card number, the last parameter is the key public static void Decrypt(string inputFilePath, string outputfilePath, string pwd) { byte[] salt1 = new byte[] { 0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64 }; Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd, salt1); Aes descrypt = Aes.Create(); descrypt.KeySize = 128; descrypt.BlockSize = 128; descrypt.Key = k2.GetBytes(128 / 8); descrypt.IV = k2.GetBytes(128 / 8); FileStream fsOutput = new FileStream(outputfilePath, FileMode.Open); CryptoStream dec = new CryptoStream(fsOutput, descrypt.CreateDecryptor(), CryptoStreamMode.Write); byte[] edata = File.ReadAllBytes(inputFilePath); dec.Write(edata, 0, edata.Length); dec.Flush(); dec.Close(); }

    My  test code.

     protected void Page_Load(object sender, EventArgs e)
            {
                EncryptDecryptUtil.Encrypt("5441191000005226", "F8660BF9063B1BBEFEA2BC413C8514FE", Server.MapPath("/csharpDemo2/code.txt"));
                EncryptDecryptUtil.Decrypt(Server.MapPath("/csharpDemo2/code.txt"), Server.MapPath("/csharpDemo2/result.txt"), "F8660BF9063B1BBEFEA2BC413C8514FE");
            }

    The result.

    Best regards,

    Ackerly Xu

    Thursday, November 1, 2018 5:39 AM
  • User-803914957 posted

    Thanks for your reply.

    But I am guided from supervisor that encryption result should be exactly

    5441191000005226   to  01AE5ED7C78D5C38

     and decryption result in reverse way.

    I am suggested to use 3-DES. 

    Would you please check again.

    Thursday, November 1, 2018 8:06 AM
  • User-893317190 posted

    Hi Alauddin Dipu,

    Do you mean, you want to encrypt 5441191000005226  to  01AE5ED7C78D5C38 exactly instead any other result?

    If so , that is to say you want to specify the encrypted result for a text,which is confusing.

    You could try the online tool

    https://asecuritysite.com/encryption/threedes

    Use your key couldn't get the result you want.

    You are suggest to use 3-DES,but AES is more  secure , you could considering using it if it is not necessary that you should use 3-des.

    https://security.stackexchange.com/questions/26179/security-comparsion-of-3des-and-aes

    Best regards,

    Ackerly Xu

    Thursday, November 1, 2018 9:32 AM
  • User-803914957 posted

    Hi Ackerly Xu,

    Thank you so much for your guidance.

    Thursday, November 1, 2018 11:15 AM