算法中DH与ECDH算法用来进行密钥协商算法
- DH
1 class Program_DH 2 { 3 static void Main2(string[] args) 4 { 5 GenKey(p, out aPrivateKey, out aPublicKey); 6 7 GenKey(p, out bPrivateKey, out bPublicKey); 8 bCompute = ComputeKey(p, bPrivateKey, bPublicKey, aPublicKey); 9 10 aCompute = ComputeKey(p, aPrivateKey, aPublicKey, bPublicKey); 11 12 Console.WriteLine(aCompute == bCompute); 13 Console.ReadKey(); 14 } 15 16 /// <summary> 17 /// 完全公开的P(质数),G(底数)值 18 /// </summary> 19 static string p = @"-----BEGIN DH PARAMETERS----- 20 MEYCQQCFO+ureuiANnvFOg79ojIyjVgdxuD4G7ERecH1xD+J7wDbgwZqejsTsgVy 21 yE1aeTXiLvtGNcMLbwgGxkRT9S67AgEC 22 -----END DH PARAMETERS----- 23 "; 24 /// <summary> 25 /// A产生的3个常量 26 /// </summary> 27 static string aPrivateKey, aPublicKey, aCompute; 28 29 /// <summary> 30 /// B产生的3个常量 31 /// </summary> 32 static string bPrivateKey, bPublicKey, bCompute; 33 34 /// <summary> 35 /// 生成D-H算法的随机数 36 /// </summary> 37 /// <param name="source">P,G</param> 38 /// <param name="privateKey">生成隐藏的随机数</param> 39 /// <param name="publicKey">生成公开的随机数</param> 40 public static void GenKey(string source, out string privateKey, out string publicKey) 41 { 42 using (DH dhCrypto = DH.FromParameters(source))//加载源P,G 43 { 44 dhCrypto.GenerateKeys();//生成随机数 45 privateKey = dhCrypto.PrivateKey.ToHexString(); 46 publicKey = dhCrypto.PublicKey.ToHexString(); 47 } 48 } 49 50 /// <summary> 51 /// 计算D-H算法的最终值(相同值) 52 /// </summary> 53 /// <param name="source">P,G</param> 54 /// <param name="fromPrivateKey">A方隐藏的随机数</param> 55 /// <param name="fromPublicKey">A方公开的随机数</param> 56 /// <param name="toPublicKey">B方公开的随机数</param> 57 /// <returns>最终值(相同值)</returns> 58 public static string ComputeKey(string source, string fromPrivateKey, string fromPublicKey, string toPublicKey) 59 { 60 using (DH dhCrypto = DH.FromParameters(source))//加载源P,G 61 { 62 dhCrypto.PrivateKey = BigNumber.FromHexString(fromPrivateKey); 63 dhCrypto.PublicKey = BigNumber.FromHexString(fromPublicKey); 64 byte[] keyBytes = dhCrypto.ComputeKey(BigNumber.FromHexString(toPublicKey)); 65 return BitConverter.ToString(keyBytes); 66 } 67 } 68 }
- ECDH
生成两组EC密钥并相互公开公钥即能完成ECDH算法
1 class Program_ECDH 2 { 3 static void Main2(string[] args) 4 { 5 string aCompute = ComputeKey(aPrivateKey, bPublicKey); 6 string bCompute = ComputeKey(bPrivateKey, aPublicKey); 7 Console.WriteLine(aCompute == bCompute); 8 Console.ReadKey(); 9 } 10 11 /// <summary> 12 /// a方私钥不公开 13 /// </summary> 14 static string aPrivateKey = @"-----BEGIN EC PARAMETERS----- 15 BgUrgQQAIw== 16 -----END EC PARAMETERS----- 17 -----BEGIN EC PRIVATE KEY----- 18 MIHcAgEBBEIAzb3CKEl2y87Q1dbqiOCG0UkBceI9V5nA4N0vXZx7xgJTHtfHCe9S 19 y/72GTZk7PQw89aTU7fdQl2NRC2hYiP2O1WgBwYFK4EEACOhgYkDgYYABAEwtG7T 20 5cGCineqYs3VPHdadOJgIwD0BGkuSEOWt3RD11S5OiBpY0bVYkYHKvySZYPfvFIW 21 EZOTNyNue3JZ0ubWzQDWHULl/P9t8LZrPrIMC43sHuoHDV0BhcsO/HUWKU9QBCYh 22 S++px6BwYrNoFaenJoHOVtDs8veqH1aAAQW1Mbb56A== 23 -----END EC PRIVATE KEY----- 24 "; 25 /// <summary> 26 /// a方公钥对b方公开 27 /// </summary> 28 static string aPublicKey = @"-----BEGIN PUBLIC KEY----- 29 MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBMLRu0+XBgop3qmLN1Tx3WnTiYCMA 30 9ARpLkhDlrd0Q9dUuTogaWNG1WJGByr8kmWD37xSFhGTkzcjbntyWdLm1s0A1h1C 31 5fz/bfC2az6yDAuN7B7qBw1dAYXLDvx1FilPUAQmIUvvqcegcGKzaBWnpyaBzlbQ 32 7PL3qh9WgAEFtTG2+eg= 33 -----END PUBLIC KEY----- 34 "; 35 /// <summary> 36 /// b方私钥不公开 37 /// </summary> 38 static string bPrivateKey = @"-----BEGIN EC PARAMETERS----- 39 BgUrgQQAIw== 40 -----END EC PARAMETERS----- 41 -----BEGIN EC PRIVATE KEY----- 42 MIHcAgEBBEIBkm1mKzVrWaq0oSanR/45y7x6B+W8/PxymW2PCcc11azuZXusXCib 43 48nXvM47Y02py1NsoDFK8lEGUSokRVzKvC2gBwYFK4EEACOhgYkDgYYABADvLnkx 44 ai96mEX1PDcak0B4buXZjSlDgcMSNiPdAC7SaKwCHLvQXc+JCQkBQg8Bi6LNvzS4 45 q/DXKz5BpKYkIlmkuwDs6KlYlBoHTHI7hhneBcGAcrou5ay0+djFyaPcbCQgpSzK 46 z1Ot1nRz8nbqQW3PE7Cc/kB6eRQF4YWsjPiVBXbpiw== 47 -----END EC PRIVATE KEY----- 48 "; 49 /// <summary> 50 /// b方公钥对a方公开 51 /// </summary> 52 static string bPublicKey = @"-----BEGIN PUBLIC KEY----- 53 MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQA7y55MWovephF9Tw3GpNAeG7l2Y0p 54 Q4HDEjYj3QAu0misAhy70F3PiQkJAUIPAYuizb80uKvw1ys+QaSmJCJZpLsA7Oip 55 WJQaB0xyO4YZ3gXBgHK6LuWstPnYxcmj3GwkIKUsys9TrdZ0c/J26kFtzxOwnP5A 56 enkUBeGFrIz4lQV26Ys= 57 -----END PUBLIC KEY----- 58 "; 59 60 /// <summary> 61 /// 签名回调 62 /// </summary> 63 private static byte[] ComputeKeyHandler(byte[] message) 64 { 65 using (MessageDigestContext hashDigest = new MessageDigestContext(HashDigest)) 66 { 67 return hashDigest.Digest(message); 68 } 69 } 70 71 /// <summary> 72 /// 签名算法 73 /// </summary> 74 private static MessageDigest HashDigest 75 { 76 get { return MessageDigest.SHA256; } 77 } 78 79 /// <summary> 80 /// 计算最终值 81 /// </summary> 82 /// <param name="fromPrivateKey">a方私钥</param> 83 /// <param name="toPublicKey">b方公钥</param> 84 /// <returns>最终值</returns> 85 public static string ComputeKey(string fromPrivateKey, string toPublicKey) 86 { 87 using (CryptoKey toCryptoKey = CryptoKey.FromPublicKey(toPublicKey, null)) 88 { 89 using (Key toKey = toCryptoKey.GetEC()) 90 { 91 using (CryptoKey fromCryptoKey = CryptoKey.FromPrivateKey(fromPrivateKey, null)) 92 { 93 using (Key fromKey = fromCryptoKey.GetEC()) 94 { 95 byte[] buffer = new byte[HashDigest.Size]; 96 int aout = fromKey.ComputeKey(toKey, buffer, ComputeKeyHandler); 97 return BitConverter.ToString(buffer); 98 } 99 } 100 } 101 } 102 } 103 104 }