在ECC加密过程中,我(作为发送者)我假设我会做如下:如何正确准备使用.net framework 4.7进行ECC加密?
- 使用椭圆曲线(标识为说,NIST P-256)以 产生短暂(临时)公共和接收器的公共密钥(即对方)的私钥对
- 弄个一些如何
- 使用方案(比如椭圆曲线的Diffie-Hellman,也被称为 ECDH)推演共享秘密使用对方的公钥和 上述步骤1中的临时私钥
- 使用共享密钥 使用密钥导出函数(KDF)推导出一个对称密钥(比如 NIST单步KDF作为记录在 http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf)
- 最后使用对称密钥使用AES对消息进行加密。
问题1:上述程序有问题吗?
在.NET框架4.7,这是我的第一个尝试吧:
var curve = ECCurve.NamedCurves.nistP256;
var ecdhSender = ECDiffieHellman.Create(curve);
X509Certificate2 otherPartyPublicCert = null; //TODO: Get some how from other party and populate this variable
byte[] otherPartyPublicKey = otherPartyPublicCert.GetPublicKey();
ECDiffieHellmanPublicKey otherPartyECDHPublicKey = ECDiffieHellmanCngPublicKey.FromByteArray(otherPartyPublicKey, CngKeyBlobFormat.GenericPublicBlob);
//The DeriveKeyMaterial seem to generate secret agreement, generate key and throw away the secrete agreement
var symmetricKey = ecdhSender.DeriveKeyMaterial(otherPartyECDHPublicKey);
// Or
//The DeriveKeyFromHash seem to generate same key as above because SHA256 was probably implicit in above call
var symmetricKey2 = ecdhSender.DeriveKeyFromHash(otherPartyECDHPublicKey, HashAlgorithmName.SHA256);
//TODO: Perform encryption with above key (either symmetricKey or symmetricKey2, depending on which call we choose) using AES etc.
Q2:请问上面的代码流似乎正确的(包括我的代码中的注释)?
问题3:使用DeriveKeyMaterial还是使用DeriveKeyFromHash有区别? (注:当我比较symmetricKey和symmetricKey2,它们是相同的)
Q4:要正确实现NIST单步KDF(这就要求KDF(Z,OtherInput)其中Z是秘密协议,OtherInput由keydatalen和OtherInfo),应该使用具有secretPrepend和secretAppend字节签名但只填充secreteAppend字节的DeriveKeyFromHash方法?
Q5:如果答案是肯定的,以Q4,如果活动促销包括说信息“AlgorithmIDStuff”,“PartyUInfoStuff”,“PartyVInfoStuff”(如果我选择实现级联格式),我应该用下列字节以下值序列(即比特串)中secretAppend参数(使用DeriveKeyFromHash方法Z字节值):无符号字节
- 256的值(keydatalen)
- LEN( “AlgorithmIDStuff”),这是16,在无符号值字节
- “Algori无符号ASCII值中的“thmIDStuff”字节=>十进制字节值:65 108 103 111 114 105 116 104 109 73 68 83 116 117 102 102
- “PartyUInfoStuff”中的无符号ASCII值bytes =>十进制字节值:80 97 114 116 121 85 73 110 102 111 83 116 117 102 102
- “PartyVInfoStuff” 在无符号的ASCII值中的字节=>十进制字节值:80 97 114 116 121 86 73 110 102 111 83 116 117 102 102
关于对Q5的回答:5.8.1.2节中的算法ID讨论并没有说它是强制性的,而是一个建议。 PartyUInfo和PartyVInfo也是如此。我错了吗?如果没有,那么我的想法是使用这些字段的自定义字符串。假设这些字段的值是有效值,则位串序列是否正确? – Raghu
AlgorithmID:一个必需的非空子字段,用于指示如何分析导出的密钥材料以及将使用哪个或哪些算法衍生的密钥材料。您的值不表示算法,或者如何解析密钥。在线快速搜索不会产生任何推荐值,因此您可能需要在https://crypto.stackexchange.com上提出具体问题。 – bartonjs
我明白了。如果我使用值“id-aes256-GCM”(并更改相应的字节),它会是正确的吗? – Raghu