Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6651957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:04:24+00:00 2026-05-26T01:04:24+00:00

Is this possible in C#? How would I accomplish this? Two-key triple DES is

  • 0

Is this possible in C#? How would I accomplish this?

Two-key triple DES is where we encrypt with K1, then decrypt with K2 and finally encrypt again with K1. The keyspace is thus 2 x 56 = 112 bits.

For example, with K1=0x0123456789ABCDEF and K2=0xFEDCBA9876543210 you would set the triple DES key to be 0x0123456789ABCDEFFEDCBA98765432100123456789ABCDEF.

0123456789ABCDEF FEDCBA9876543210 0123456789ABCDEF
|<------K1------>|<------K2------>|<------K3------>|

It accepts A9993E364706816A and the 2 keys that it must use is K1 = 0123456789ABCDEF and K2 = FEDCBA9876543210. The end result must be: 6E5271A3F3F5C418 which I am not getting.

UPDATE:

I am trying to create the concatenated key that I need to use. The 2 keys used above is converted to a byte array and seems to have a length of 16 each. And when the 2 are concatenated then the length is 32. Then my code bombs out. The key has to have a length of 16 or 24. What do I need to do in this case?

UTF8Encoding characterEncoding = new UTF8Encoding();
byte[] accessKey1ByteArray = characterEncoding.GetBytes(accessKey1);
byte[] accessKey2ByteArray = characterEncoding.GetBytes(accessKey2);
byte[] accessKeysArray = accessKey1ByteArray.Concat(accessKey2ByteArray).ToArray();

Here is where I try to set my values:

public byte[] ComputeTripleDesEncryption(byte[] plainText, byte[] key)
{
     TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

     des.Key = key;
     des.GenerateIV();
     des.Mode = CipherMode.ECB;
     des.Padding = PaddingMode.None;

     ICryptoTransform ic = des.CreateEncryptor();

     byte[] enc = ic.TransformFinalBlock(plainText, 0, plainText.Length);

     return enc;
}

UPDATE 2

Do I need to set the size? The byte array key that I am sending through is K1 + K2 + K1.

The text that I am sending through, do I need convert this to bytes like what you recommended, or can the following also do the trick?

UTF8Encoding characterEncoding = new UTF8Encoding();
byte[] block1ByteArray = characterEncoding.GetBytes(block1);

The value of block1 is: A9993E364706816A.

How I got A9993E364706816A was from my SHA-1 hashed result. The first 16 characters of this hashed result of my string that I want to encode.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:04:25+00:00Added an answer on May 26, 2026 at 1:04 am

    This sounds like you just want to set a 128 bit key for the triple des key.

    I believe in this case if you provide a 128 bit key it splits it into two 64 bit keys and uses the first as K1 and K3 and the second as K2 which is exactly what you want.

    Unfortunately I can’t find a source to quote on this but I did a lot of reading on the subject recently when implementing some crypto stuff myself and finding out about key lengths and this is what I discovered.

    If you have K1 and K2 as byte arrays already then you should be able to just use a nice little linq extension method and do:

    SymmetricAlgorithm cryptoService = new TripleDESCryptoServiceProvider();
    byte[] myKey = K1.Concat(K2).ToArray();
    cryptoService.Key = mKey;
    

    That will then do as you want.

    In response to your updated part of the question the two keys you have are hexdecimal representations of a sequence of bytes. 0x0123456789ABCDEF is 16 characters of hexdecimal but this is equivalent to 8 bytes of information since it is 4 bits in each character – two making up a byte.

    To convert that string to a byte array the following function can be used:

    public static byte[] StringToByteArray(String hex)
    {
        if (hex.Substring(0,2)=="0x")
        hex = hex.Substring(2);
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }
    

    (From How do you convert Byte Array to Hexadecimal String, and vice versa?)

    This will then be used like this:

    string K1="0x0123456789ABCDEF";
    string K2="0xFEDCBA9876543210";
    byte[] key = StringToByteArray(K1).Concat(StringToByteArray(K2)).ToArray();
    

    When implementing TDES you will need to agree a key, Block Cipher mode, Padding method and in most Block modes you will need an initialisation Vector. You’ll possibly also want to use a Message Authentication Code.

    To get an initialisation vector you’ll want to do something like:

    cryptoService.GenerateIV();
    byte[] iv = cryptoService.IV;
    

    I strongly advise reading pages on encryption to understand better what the various things you are doing actually are rather than just writing the code. It will make you more confident in your security and make you sound more confident while dealing with others. I’ve taken the liberty of including some links, most of which can be found by just googling.

    Useful links:

    http://en.wikipedia.org/wiki/Initialization_vector – all about initialisation vectors

    http://en.wikipedia.org/wiki/Triple_DES – on the TDES algorithm

    http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation – How consecutive blocks of data interact with each other.

    http://en.wikipedia.org/wiki/Padding_%28cryptography%29 – Not massively important except there are different ways of padding and both sides need to be using the same one (of course).

    http://chargen.matasano.com/chargen/2009/7/22/if-youre-typing-the-letters-a-e-s-into-your-code-youre-doing.html – An excellent and amusing commentary on the use of encryption and where there are weaknesses and what encryption can and cannot do.

    http://en.wikipedia.org/wiki/Message_authentication_code – How to confirm that your message hasn’t been tampered with

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible? Or would I miss something? (Fixtures,..?) Because: When I use autotest
Is this possible? We have WinForms (CF) apps which use the EMDK, and would
First of all, is this possible? If so: What challenges would I encounter in
Whether this is possible I don't know, but it would mighty useful! I have
Not sure this is possible, but looking to write a script that would return
I have no idea if this is possible ... but it would be cool.
I have no idea if this is possible, but if it is, what would
I would like to animate things in 3D space. I know this is possible
I would like to know whether this is actually possible in Android? When Android
I was wondering if it would be possible to do this: $var = '$something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.