I'm attempting to use the KMS GenerateDataKeyPairAsync in order to get the public and private key out for testing (Once it works I will switch to the GenerateDataKeyPairWithoutPlaintextAsync).
The GenerateDataKeyPairResponse has three memory streams for the Public Key, Private Key Ciphertext and Private Key plaintext.
I can't seem to convert any of these memory streams to string so I can actually then use the keys.
The SDK docs (https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html) say it will be Base64 encoded if using the HTTP api, does the SDK use the HTTP api? I can't seem to tell.
I have tried using StreamReader.ReadToEnd() and using Encoding.ENCODING.FromString(stream.ToArray()) using all the encodings but I can't seem to get a reasonable value out.
Am I missing something important here?
Thanks
Adding the code:
# USING: AWSSDK.KeyManagementService VERSION: 3.5.0-beta
using System;
using System.IO;
using System.Threading.Tasks;
using Amazon;
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
using Amazon.Runtime;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var credentials = new BasicAWSCredentials("AccessKey", "SecretKey");
var kmsClient = new AmazonKeyManagementServiceClient(credentials, RegionEndpoint.EUCentral1);
const string keyId = "CMKKey";
var dataKeyRequest = new GenerateDataKeyPairRequest
{
KeyId = keyId,
KeyPairSpec = DataKeyPairSpec.RSA_2048
};
var dataKeyPairResponse = await kmsClient.GenerateDataKeyPairAsync(dataKeyRequest);
var publicKeyStream = dataKeyPairResponse.PublicKey;
var privateKeyStream = dataKeyPairResponse.PrivateKeyPlaintext;
var publicReader = new StreamReader( publicKeyStream );
var publicKey = publicReader.ReadToEnd();
var privateReader = new StreamReader( privateKeyStream );
var privateKey = privateReader.ReadToEnd();
Console.WriteLine(publicKey);
Console.WriteLine(privateKey);
}
}
}