zoukankan      html  css  js  c++  java
  • OpenSSL.Net使用随记(四)

    ECDSA签名算法

    • ECDSA
     1 class Program_ECDSA
     2     {
     3         static void Main(string[] args)
     4         {
     5             byte[] messageBytes = Encoding.UTF8.GetBytes("ECDSA签名。");
     6 
     7             byte[] signBytes = Sign(privateKey, messageBytes);
     8             bool result = Verify(publicKey, messageBytes, signBytes);
     9             Console.WriteLine(result);
    10 
    11             signBytes = Sign512(privateKey, messageBytes);
    12             result = Verify512(publicKey, messageBytes, signBytes);
    13             Console.WriteLine(result);
    14             Console.ReadKey();
    15         }
    16 
    17         static string privateKey = @"-----BEGIN EC PARAMETERS-----
    18 BgUrgQQAIw==
    19 -----END EC PARAMETERS-----
    20 -----BEGIN EC PRIVATE KEY-----
    21 MIHcAgEBBEIAzb3CKEl2y87Q1dbqiOCG0UkBceI9V5nA4N0vXZx7xgJTHtfHCe9S
    22 y/72GTZk7PQw89aTU7fdQl2NRC2hYiP2O1WgBwYFK4EEACOhgYkDgYYABAEwtG7T
    23 5cGCineqYs3VPHdadOJgIwD0BGkuSEOWt3RD11S5OiBpY0bVYkYHKvySZYPfvFIW
    24 EZOTNyNue3JZ0ubWzQDWHULl/P9t8LZrPrIMC43sHuoHDV0BhcsO/HUWKU9QBCYh
    25 S++px6BwYrNoFaenJoHOVtDs8veqH1aAAQW1Mbb56A==
    26 -----END EC PRIVATE KEY-----
    27 ";
    28 
    29         static string publicKey = @"-----BEGIN PUBLIC KEY-----
    30 MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBMLRu0+XBgop3qmLN1Tx3WnTiYCMA
    31 9ARpLkhDlrd0Q9dUuTogaWNG1WJGByr8kmWD37xSFhGTkzcjbntyWdLm1s0A1h1C
    32 5fz/bfC2az6yDAuN7B7qBw1dAYXLDvx1FilPUAQmIUvvqcegcGKzaBWnpyaBzlbQ
    33 7PL3qh9WgAEFtTG2+eg=
    34 -----END PUBLIC KEY-----
    35 ";
    36 
    37         public static byte[] Sign(string privateKey, byte[] messageBytes)
    38         {
    39             using (CryptoKey cryptoKey = CryptoKey.FromPrivateKey(privateKey, null))
    40             {
    41                 using (MessageDigestContext hashDigest = new MessageDigestContext(MessageDigest.ECDSA))
    42                 {
    43                     byte[] hashBytes = hashDigest.Digest(messageBytes);
    44                     return hashDigest.Sign(messageBytes, cryptoKey);
    45                 }
    46             }
    47         }
    48 
    49         public static bool Verify(string publicKey, byte[] messageBytes, byte[] signBytes)
    50         {
    51             using (CryptoKey cryptoKey = CryptoKey.FromPublicKey(publicKey, null))
    52             {
    53                 using (MessageDigestContext hashDigest = new MessageDigestContext(MessageDigest.ECDSA))
    54                 {
    55                     byte[] hashBytes = hashDigest.Digest(messageBytes);
    56                     return hashDigest.Verify(messageBytes, signBytes, cryptoKey);
    57                 }
    58             }
    59         }
    60 
    61         public static byte[] Sign512(string privateKey, byte[] messageBytes)
    62         {
    63             using (CryptoKey cryptoKey = CryptoKey.FromPrivateKey(privateKey, null))
    64             {
    65                 using (MessageDigestContext hashDigest = new MessageDigestContext(MessageDigest.SHA512))
    66                 {
    67                     byte[] hashBytes = hashDigest.Digest(messageBytes);
    68                     return hashDigest.Sign(messageBytes, cryptoKey);
    69                 }
    70             }
    71         }
    72 
    73         public static bool Verify512(string publicKey, byte[] messageBytes, byte[] signBytes)
    74         {
    75             using (CryptoKey cryptoKey = CryptoKey.FromPublicKey(publicKey, null))
    76             {
    77                 using (MessageDigestContext hashDigest = new MessageDigestContext(MessageDigest.SHA512))
    78                 {
    79                     byte[] hashBytes = hashDigest.Digest(messageBytes);
    80                     return hashDigest.Verify(messageBytes, signBytes, cryptoKey);
    81                 }
    82             }
    83         }
    84 
    85     }
  • 相关阅读:
    1137 Final Grading (25 分)
    1136 A Delayed Palindrome (20 分)
    1135 Is It A Red-Black Tree (30 分)
    1134 Vertex Cover (25 分)
    1133 Splitting A Linked List (25 分)
    1074 Reversing Linked List (25 分)
    1132 Cut Integer (20 分)
    HDU 3342 Legal or Not
    IDEA解决JSP页面无法使用EL表达式问题
    25. Bootstreap 下拉菜单
  • 原文地址:https://www.cnblogs.com/azeri/p/8987294.html
Copyright © 2011-2022 走看看