栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

OpenSSL的C#版本EVP_BytesToKey方法?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenSSL的C#版本EVP_BytesToKey方法?

我找到了 EVP_BytesToKey
方法的伪代码说明(在openssl源代码的/doc/ssleay.txt中):

M[0]=MD(data . salt);for (i=1; i<count; i++) M[0]=MD(M[0]);i=1while (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字节的密钥和16字节的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);}

更新 :这里更多/更少相同的实现,但是使用.NET
DeriveBytes接口:https
://gist.github.com/1339719


OpenSSL
1.1.0c更改了某些内部组件中使用的摘要算法。以前使用MD5,并且1.1.0切换到SHA256。小心的变化不影响你在这两个

EVP_BytesToKey
和命令状
opensslenc



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/416896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号