2
public static string Encrypt(string toEncrypt, string secretKey)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        var md5Serv = System.Security.Cryptography.MD5.Create();
        keyArray = md5Serv.ComputeHash(UTF8Encoding.UTF8.GetBytes(secretKey));
        md5Serv.Dispose();


        var tdes = System.Security.Cryptography.TripleDES.Create();


        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)

        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        byte[] resultArray =
          cTransform.TransformFinalBlock(toEncryptArray, 0,
          toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Dispose();
        //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

secretkey = 16 character of string

in this line :

tdes.Key = keyArray;

i get this error: Message = "Specified key is not a valid size for this algorithm."

error Message screen shot

how to solved this problem in asp.net core 1.1.0? how to convert byte[16] to byte[24]?

Updated Post

thanks For Help :) but!

I use this code in .Net Framework 4.6.2 for encrypt:

public static string Encrypt(string toEncrypt, string secretKey)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();


        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(secretKey));

        hashmd5.Clear();



    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    //set the secret key for the tripleDES algorithm
    tdes.Key = keyArray;
    //mode of operation. there are other 4 modes.
    //We choose ECB(Electronic code Book)
    tdes.Mode = CipherMode.ECB;
    //padding mode(if any extra byte added)

    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateEncryptor();
    //transform the specified region of bytes array to resultArray
    byte[] resultArray =
      cTransform.TransformFinalBlock(toEncryptArray, 0,
      toEncryptArray.Length);
    //Release resources held by TripleDes Encryptor
    tdes.Clear();
    //Return the encrypted data into unreadable string format
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

and Use this in .Net Core 1.1 :

public static string Encrypt(string toEncrypt, string secretKey)
{
    byte[] keyArray;
    byte[] resultArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    using (var md5Serv = System.Security.Cryptography.MD5.Create())
    {
        keyArray = md5Serv.ComputeHash(UTF8Encoding.Unicode.GetBytes(secretKey));
        if(keyArray.Length==16)
        {
            byte[] tmp = new byte[24];
            Buffer.BlockCopy(keyArray, 0, tmp, 0, keyArray.Length);
            Buffer.BlockCopy(keyArray, 0, tmp, keyArray.Length, 8);
            keyArray = tmp;
        }
    }

    using (var tdes = System.Security.Cryptography.TripleDES.Create())
    {
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)

        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        resultArray =
          cTransform.TransformFinalBlock(toEncryptArray, 0,
          toEncryptArray.Length);
    }

    //Return the encrypted data into unreadable string format
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

but i don't know why this methods give me different result?!

  • The [documentation](https://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledes.key(v=vs.110).aspx) for the key parameter states *This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.* but we don't know what is in the var *toEncrypt* as it wasn't included in the question. – Jay Aug 05 '17 at 11:40
  • In general the first 8-bytes are duplicated to bytes 16-23. Some 3DES implementations do this automatically, some do not. Some implementations will triplicate an 8-byte key to 24-bits. It is always best to fully specify inputs to encryption functions. Of course it is best not to use 3DES if at all possible and 2-key 3DES is no longer considered secure. – zaph Aug 05 '17 at 12:26
  • 3DES, ECB mode and MD5 are all deprecated as insecure. Why are you not using AES in a secure mode with SHA2 or SHA3 hashing? – rossum Aug 05 '17 at 14:23
  • @rossum 1. 3-key 3DES is not deprecated, 2-key 3DES is deprecated by NIST, see 3DES [keying options](https://en.wikipedia.org/wiki/Triple_DES#Keying_options) . You are correct that 3DES should not be used in new work. 2. SHA2 or SHA3 are marginally better for deriving a password and should not be used, they are to fast. Instead a password derivation function such as [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2) with an iteration count/cost of ~100ms. – zaph Aug 07 '17 at 12:51
  • @PoPo In your NetFx example you use `UTF8Encoding.UTF8`, in CoreFx you use `UTF8Encoding.Unicode`. Assuming you have aliased UTF8Encoding to be System.Text.Encoding, Unicode is UCS-2, a very different byte pattern than UTF-8, so you're not using the same input to MD5. – bartonjs Aug 07 '17 at 15:43
  • thank you, your code helped me a lot. – Bhimbim Nov 29 '17 at 14:38

1 Answers1

4
if (key.Length == 16)
{
    byte[] tmp = new byte[24];
    Buffer.BlockCopy(key, 0, tmp, 0, key.Length);
    Buffer.BlockCopy(key, 0, tmp, key.Length, 8);
    key = tmp;
}

That will turn your 2DES key (k1, k2) into the 3DES key (k1, k2, k1). FWIW, this has been fixed for .NET Core 2.0 (https://github.com/dotnet/corefx/issues/9966).

So, now your code will work again. Though, as others have pointed out in comments, there's a lot going on in your code which is not considered cryptologically sound by modern standards. You should strongly consider taking this as an opportunity to enhance your encryption. (If you can't "because then it can't work with already existing data" then you should take this opportunity to add crypto-agility to your data, to permit you to move to different key schemes and/or algorithms over time.)

bartonjs
  • 30,352
  • 2
  • 71
  • 111