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     }
  • 相关阅读:
    异步FIFO总结
    异常检测参考
    Java数据库连接技术
    Eclipse Decompiler不生效解决办法
    mysql常用操作
    时间序列预测——Tensorflow.Keras.LSTM
    AR(I)MA时间序列建模过程——步骤和python代码
    MySQL优化实例
    MySQL性能优化经验
    高性能MySQL笔记 第6章 查询性能优化
  • 原文地址:https://www.cnblogs.com/azeri/p/8987294.html
Copyright © 2011-2022 走看看