在文件加密协议中使用ECIES,我需要对发送方进行身份验证,所以我使用了ECDSA,并且我想知道签署临时公钥(接收方需要用于派生共享对称密钥)是否可行?目的是防止MitM伪造和发送假的临时公钥。代码(C#)使用Inferno,但是考虑到显式变量名和注释,很容易理解:
internal static void Encrypt(CngKey receiverPublicDhm, CngKey senderDsa, string file, string text)
{
var plainTextBytes = text.ToBytes();
var ephemeralBundle = receiverPublicDhm.GetSharedEphemeralDhmSecret();
var ephemeralPublic = ephemeralBundle.EphemeralDhmPublicKeyBlob;
var sharedSymmetric = ephemeralBundle.SharedSecret;
// sign emphemeral public blob and plainText(so that MitM cannot forge a fake ephemeral public key)
var toSign = Utils.Combine(ephemeralPublic, plainTextBytes);
byte[] signed;
using (var ecdsa = new ECDsaCng(senderDsa) { HashAlgorithm = CngAlgorithm.Sha384 })
signed = ecdsa.SignData(toSign);
// ETM signature and plaintext
var toEncrypt = Utils.Combine(signed, plainTextBytes);
var encrypted = SuiteB.Encrypt(sharedSymmetric, toEncrypt.AsArraySegment());
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
{
// so the format is:
// [ephemeral public] (signed but not encrypted)
// [signature] [ciphertext] (both encrypted and MAC'd)
fs.Write(ephemeralPublic, 0, ephemeralPublic.Length);
fs.Write(encrypted, 0, encrypted.Length);
}
}编辑:修正了文件格式描述中的错误
发布于 2016-12-28 14:56:26
我不同意Maarten Bodewes对您的特定场景/代码的回答:
我相信你所采取的方法没有什么不对。我不对您的文件格式/代码提供任何评论。
https://crypto.stackexchange.com/questions/42553
复制相似问题