• 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     }
  • 相关阅读:
    Mybatis多层嵌套查询
    UUID 唯一性实现原理
    oracle 多实例启动
    orcal启动多实例是报 ORA-00845: MEMORY_TARGET not supported onthis system
    java调用quartz 2.2.2方法总结。
    mybatis中like的使用(模糊查询)
    Orcal数据库实现主键ID自增
    spring cloud分布式关于熔断器
    spring cloud分布式健康检查
    spring cloud分布式整合zipkin的链路跟踪
  • 原文地址:https://www.cnblogs.com/azeri/p/8987294.html
Copyright © 2020-2023  润新知