简体   繁体   English

Azure Key Vault - 检索本地加密的 RSA 公钥

[英]Azure Key Vault - retrieving RSA public key for on-premises encryption

I'm trying to use an RSA key I have already generated on my Azure Key Vault in the following way:我正在尝试使用我已经通过以下方式在我的 Azure Key Vault 上生成的 RSA 密钥:

  1. Retrieve the public key检索公钥
  2. Encrypt some textual data with it (-locally-)用它加密一些文本数据(-locally-)
  3. Decrypt it (in a different app) using Azure Key Vault使用 Azure Key Vault 对其进行解密(在不同的应用程序中)

What I already managed to do is:我已经设法做到的是:

            string clientId = "XYZ";
            string tenantId = "ABC";
            string clientSecret = "123";

            string keyVaultName = "kvn";
            string keyVaultKeyName = "kvkn";
            string textToEncrypt = "StuffIDoNotWantYouToKnow";

            ClientSecretCredential clientSecretCredential = new ClientSecretCredential(
                tenantId, // your tenant id
                clientId, // your AD application appId
                clientSecret // your AD application app secret
            );


            //get key
            KeyClient keyClient = new KeyClient(new Uri($"https://{keyVaultName}.vault.azure.net/"), clientSecretCredential); ;
            var key = keyClient.GetKey(keyVaultKeyName);

What I'm currently struggling to understand is how to use the retrieved key to encrypt the textual data.我目前正在努力理解的是如何使用检索到的密钥来加密文本数据。

Any help would be appreciated!任何帮助,将不胜感激!

PS I use .NET framework 4.6.1 PS 我使用 .NET 框架 4.6.1

Solved it解决了

private static string clientId;
private static string tenantId;
private static string clientSecret;

private static string keyVaultName;
private static string keyVaultKeyName;

private static ClientSecretCredential clientSecretCredential;

public static void Main(string[] args)
{
    PopulateParams();

    KeyClient keyClient = new KeyClient(new Uri($"https://{keyVaultName}.vault.azure.net/"), clientSecretCredential); ;
    var key = keyClient.GetKey(keyVaultKeyName);

    byte[] N = key.Value.Key.N; //modulus
    byte[] E = key.Value.Key.E; //exponent

    string textToEncrypt = "StuffIDoNotWantYouToKnow";
    byte[] encryptedData = EncryptLocally(textToEncrypt, N, E);
    string res = DecryptRemotely(key.Value.Id, encryptedData);
    Console.WriteLine(res);
}

public static void PopulateParams()
{
    //TODO not hard coded
    clientId = "XYZ";
    tenantId = "ABC";
    clientSecret = "123";

    keyVaultName = "kvm";
    keyVaultKeyName = "kvkm";

    clientSecretCredential = new ClientSecretCredential(
        tenantId,
        clientId,
        clientSecret
    );
}

public static byte[] EncryptLocally(string data, byte[] N, byte[] E)
{
    byte[] encryptedData = null;

    try
    {
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        RSAParameters RSAKeyInfo = new RSAParameters();

        //Set RSAKeyInfo to the public key values. 
        RSAKeyInfo.Modulus = N;
        RSAKeyInfo.Exponent = E;

        RSA.ImportParameters(RSAKeyInfo);

        byte[] dataBytes = Encoding.ASCII.GetBytes(data);

        encryptedData = RSA.Encrypt(dataBytes, true);
    }
    catch (CryptographicException e)
    {
        Console.WriteLine(e);
    }

    return encryptedData;
}

public static string DecryptRemotely(Uri keyId, byte[] encryptedData)
{
    string decryptedText = null;

    CryptographyClient cryptoClient = new CryptographyClient(keyId, clientSecretCredential);

    var decryptedBytes = cryptoClient.Decrypt(EncryptionAlgorithm.RsaOaep, encryptedData);
    decryptedText = System.Text.Encoding.UTF8.GetString(decryptedBytes.Plaintext);

    return decryptedText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM