locked
Encrypt/Decrypt two strings hexadecimal RRS feed

  • Question

  • I have two value as bellow:

    Key  = 1111111111111111
    Data = 2222222222222222

    I would like to do encrypt the data value by the key to get out: 950973182317F80B and decrypt to get out values: 486A6235A29DADB4 on c# but get exception and i don't know to fix.

    Please help to suggest and advice.

    Thank you so much.

    Simon

    Monday, January 11, 2016 8:13 AM

Answers

  • Dear Kristin,

    Thank you for your suggest and support.

    Best Regards,

    Simon.

    @Simon,

    You are welcome.

    Please remember to close this thread by marking helpful thread as an answer.

    If you have any other issues, please start a new thread in this forum, thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Simon Khoa Thursday, January 14, 2016 9:34 AM
    Wednesday, January 13, 2016 8:43 AM

All replies

  • Hi Simon Khoa,

    This forum discuss the Test tools in Visual Studio including web performance, load testing and of course the hot new Coded UI testing.

    Since your problem is related to C#, I will help you move this thread to C# forum for a better help. It is appropriate and more experts will assist you.

    Best Regards,
    Weiwei

    Tuesday, January 12, 2016 6:54 AM
  • Dear Weiwei,

    Sorry for inconvenience and thank you very much.

    Best Regards,

    Simon Khoa.

    Tuesday, January 12, 2016 7:14 AM
  • Hi Simon ,

    >>I would like to do encrypt the data value by the key to get out: 950973182317F80B and decrypt to get out values: 486A6235A29DADB4 on c# but get exception

    I am afraid your question is not clear to me.  Here are some unclear points as following:

    1. What kind of encryption algorithm you are using?
    2. What  is the excpetion details?

    I would suggest you provide a simplified sample about this issue, It would be better to help us to figure out the root cause.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 12, 2016 8:53 AM
  • Dear Kristin,

    Thank you for your feedback.

    1. What kind of encryption algorithm you are using ?

    [Simon]: I using 3DES to do encrypt and decrypt two string hexadecimal, the value key and data are hexadecimal, mode ECB

    2. What is the exception details?

    [Simon]: I got the message Bad Data after had to do catch the message in decrypt function, the following decrypt function:

    public static string Decrypt (string encodedText, string key)
       {
           TripleDESCryptoServiceProvider desCrytoProvider = new TripleDESCryptoServiceProvider();
           MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();

           byte[] byteHash;
           byte[] byteBuff;

           var bytes = Encoding.UTF8.GetBytes(key);

           byteHash = hashMD5Provider.ComputeHash(bytes);
           desCrytoProvider.Key = byteHash;
           desCrytoProvider.Mode = CipherMode.ECB;
           byteBuff = Convert.FromBase64String(encodedText);

           string plaintext = Encoding.UTF8.GetString(desCrytoProvider.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
           return plaintext;
       }

    Thank you for your suggest,

    Best Regards,

    Simon

    Wednesday, January 13, 2016 2:16 AM
  • Hi Simon,

    I've tested on your code. your code looks fine.

    >>[Simon]: I got the message Bad Data after had to do catch the message in decrypt function, the following decrypt function:

    Based on the error message "Bad Data", because your encodedText doesn't match with your key.

    Please use the following code to make a test, you'll get a="Hello"

      string a = Decrypt("mO89WyCPJQE=", "1111111111111111");

    For more detailed information, your could refer to Encrypt and Decrypt Data with C#

    Have a nice day!

    kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Wednesday, January 13, 2016 7:58 AM
    • Proposed as answer by Kristin Xie Wednesday, January 13, 2016 8:39 AM
    Wednesday, January 13, 2016 7:56 AM
  • Dear Kristin,

    Thank you for your suggest and support.

    Best Regards,

    Simon.

    Wednesday, January 13, 2016 8:38 AM
  • Dear Kristin,

    Thank you for your suggest and support.

    Best Regards,

    Simon.

    @Simon,

    You are welcome.

    Please remember to close this thread by marking helpful thread as an answer.

    If you have any other issues, please start a new thread in this forum, thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Simon Khoa Thursday, January 14, 2016 9:34 AM
    Wednesday, January 13, 2016 8:43 AM