encrypt in .net core with TripleDES

十年热恋 提交于 2019-12-22 17:57:20

问题


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?!


回答1:


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.)



来源:https://stackoverflow.com/questions/45521363/encrypt-in-net-core-with-tripledes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!