【Azure Developer】使用Azure Key Vault 的Key签名后,离线验证的一些参考资料

简介: 【Azure Developer】使用Azure Key Vault 的Key签名后,离线验证的一些参考资料

问题描述

使用 key Vault 的sign接口,Request Body中的 Value 是要传什么呢? 签名后的内容如何在本地离线验证呢?

Azure Key Vault Sign 接口:https://docs.microsoft.com/zh-cn/rest/api/keyvault/sign/sign#jsonwebkeysignaturealgorithm

 

问题答案

Azure Key Vault Sign 方法的目的是:使用指定的键从摘要创建签名。它的Body中Value的值为使用Base64编码后的内容.

 

 

签名并验证:严格来讲,此操作应该为“签名哈希”或“验证哈希”,因为 Key Vault 不支持创建签名过程中的内容哈希。 所以需要在调用Sign方法前,进行内容哈希,然后请求 Key Vault 对哈希内容进行签名。

 

C#的本地离线签名验证代码:

 

 

Git Hub链接为:https://github.com/rahulpnath/Blog/blob/master/VerifySignatureOffline/VerifySignatureOffline/Program.cs , 如不能访问,可以参考以下的部分源码:

using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.WebKey;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace VerifySignatureOffline
{
    class Program
    {
        private static string applicationId = "ApplicationID";
        private static string applicationSecret = "ApplicationSecret";
        static void Main(string[] args)
        {
            var client = new KeyVaultClient(Authenticate);
            GetKeys(client);
            Console.ReadKey();
        }
        private static async Task<string> GetKeys(KeyVaultClient keyVaultClient)
        {
            var keyIdentifier = "keyIdentifier";
            var textToEncrypt = "This is a test message";
            var byteData = Encoding.Unicode.GetBytes(textToEncrypt);
            var hasher = new SHA256CryptoServiceProvider();
            var digest = hasher.ComputeHash(byteData);
            var signedResult = await keyVaultClient.SignAsync(
                keyIdentifier, JsonWebKeySignatureAlgorithm.RS256, digest);
            var isVerified = await keyVaultClient.VerifyAsync(keyIdentifier, "RS256", digest, signedResult.Result);
            var keyResult = await keyVaultClient.GetKeyAsync(keyIdentifier);
            var jsonWebKey = keyResult.Key.ToString();
            var key = JsonConvert.DeserializeObject<JsonWebKey>(jsonWebKey);
            var rsa = new RSACryptoServiceProvider();
            var p = new RSAParameters() { Modulus = key.N, Exponent = key.E };
            rsa.ImportParameters(p);
            
            isVerified = rsa.VerifyHash(digest, "Sha256", signedResult.Result);
            return null;
        }
        private static async Task<string> Authenticate(string authority, string resource, string scope)
        {
            var adCredential = new ClientCredential(applicationId, applicationSecret);
            var authenticationContext = new AuthenticationContext(authority, null);
            return (await authenticationContext.AcquireTokenAsync(resource, adCredential)).AccessToken;
        }
    }
}

原文参考 Azure Key Vault: Digital Signatures and Offline Verificationhttps://www.rahulpnath.com/blog/azure-key-vault-digital-signatures-and-offline-verification/

相关文章
|
2月前
|
存储 数据安全/隐私保护
【Azure 环境】把OpenSSL生产的自签名证书导入到Azure Key Vault Certificate中报错
【Azure 环境】把OpenSSL生产的自签名证书导入到Azure Key Vault Certificate中报错
|
2月前
|
API Python
【Azure Developer】AAD API如何获取用户“Block sign in”信息(accountEnabled)
【Azure Developer】AAD API如何获取用户“Block sign in”信息(accountEnabled)
|
2月前
|
存储 API
【Azure API 管理】APIM中证书更新问题
【Azure API 管理】APIM中证书更新问题
|
2月前
|
运维 Kubernetes 容器
【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client secret keys for app ****** are expired)
【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client secret keys for app ****** are expired)
【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client secret keys for app ****** are expired)
|
2月前
|
安全 Linux API
【Azure Key Vault】使用Azure CLI获取Key Vault 机密遇见问题后使用curl命令来获取机密内容
【Azure Key Vault】使用Azure CLI获取Key Vault 机密遇见问题后使用curl命令来获取机密内容
|
2月前
|
存储 数据安全/隐私保护
【Azure Key Vault】在Alteryx中使用Azure Key Vault存储账号和密码并实现无交互登录
【Azure Key Vault】在Alteryx中使用Azure Key Vault存储账号和密码并实现无交互登录
|
2月前
|
安全 Java 开发工具
【Azure Key Vault】是否有直接方法将Azure Key Vault中的机密名称/机密值到处成文件呢?
【Azure Key Vault】是否有直接方法将Azure Key Vault中的机密名称/机密值到处成文件呢?
|
2月前
|
存储 安全 API
【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
|
2月前
|
存储 安全 Python
【Azure Key Vault】在Azure Databricks上获取Azure Key Vault中所存储的机密(secret)的两种方式
【Azure Key Vault】在Azure Databricks上获取Azure Key Vault中所存储的机密(secret)的两种方式
|
2月前
|
存储 安全 API
【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)