第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OpenSSL的C#版本EVP_BytesToKey方法?

OpenSSL的C#版本EVP_BytesToKey方法?

叮當(dāng)貓咪 2019-10-22 21:17:23
我正在尋找OpenSSL EVP_BytesToKey函數(shù)的直接.NET實現(xiàn)。我找到的最接近的東西是System.Security.Cryptography.PasswordDeriveBytes類(和Rfc2898DeriveBytes),但它似乎略有不同,并且不會生成與EVP_BytesToKey 相同的鍵和iv。我還發(fā)現(xiàn)此實現(xiàn)似乎是一個不錯的開始,但沒有考慮迭代計數(shù)。我意識到這里有OpenSSL.NET,但這只是本機(jī)openssl DLL的包裝,而不是“真正的” .NET實現(xiàn)。
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊

我找到了EVP_BytesToKey方法的偽代碼說明(在openssl源代碼的/doc/ssleay.txt中):


/* M[] is an array of message digests

 * MD() is the message digest function */

M[0]=MD(data . salt);

for (i=1; i<count; i++) M[0]=MD(M[0]);


i=1

while (data still needed for key and iv)

    {

    M[i]=MD(M[i-1] . data . salt);

    for (i=1; i<count; i++) M[i]=MD(M[i]);

    i++;

    }


If the salt is NULL, it is not used.

The digests are concatenated together.

M = M[0] . M[1] . M[2] .......

因此,基于此,我能夠提出這種C#方法(該方法似乎可以滿足我的目的,并假定使用32字節(jié)的密鑰和16字節(jié)的iv):


private static void DeriveKeyAndIV(byte[] data, byte[] salt, int count, out byte[] key, out byte[] iv)

{

    List<byte> hashList = new List<byte>();

    byte[] currentHash = new byte[0];


    int preHashLength = data.Length + ((salt != null) ? salt.Length : 0);

    byte[] preHash = new byte[preHashLength];


    System.Buffer.BlockCopy(data, 0, preHash, 0, data.Length);

    if (salt != null)

        System.Buffer.BlockCopy(salt, 0, preHash, data.Length, salt.Length);


    MD5 hash = MD5.Create();

    currentHash = hash.ComputeHash(preHash);          


    for (int i = 1; i < count; i++)

    {

        currentHash = hash.ComputeHash(currentHash);            

    }


    hashList.AddRange(currentHash);


    while (hashList.Count < 48) // for 32-byte key and 16-byte iv

    {

        preHashLength = currentHash.Length + data.Length + ((salt != null) ? salt.Length : 0);

        preHash = new byte[preHashLength];


        System.Buffer.BlockCopy(currentHash, 0, preHash, 0, currentHash.Length);

        System.Buffer.BlockCopy(data, 0, preHash, currentHash.Length, data.Length);

        if (salt != null)

            System.Buffer.BlockCopy(salt, 0, preHash, currentHash.Length + data.Length, salt.Length);


        currentHash = hash.ComputeHash(preHash);            


        for (int i = 1; i < count; i++)

        {

            currentHash = hash.ComputeHash(currentHash);

        }


        hashList.AddRange(currentHash);

    }

    hash.Clear();

    key = new byte[32];

    iv = new byte[16];

    hashList.CopyTo(0, key, 0, 32);

    hashList.CopyTo(32, iv, 0, 16);

}

更新:這里更多/更少相同的實現(xiàn),但是使用.NET DeriveBytes接口:https ://gist.github.com/1339719


OpenSSL 1.1.0c更改了某些內(nèi)部組件中使用的摘要算法。以前使用MD5,并且1.1.0切換到SHA256。小心的變化不影響你在這兩個EVP_BytesToKey和命令一樣openssl enc。


查看完整回答
反對 回復(fù) 2019-10-22
  • 1 回答
  • 0 關(guān)注
  • 762 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號