0

I want to use functions of the Winapi.Security.Cryptography.Core_IAsymmetricKeyAlgorithmProvider and it has a parameter of type IBuffer. i dont know how to use an IBuffer or how to give data to it.

i tried allocationg memory and giving the function a pwide char but the ide already told me this wont work. I also didnt find anything useful online.

2
  • Can you post the code you tried so far? Jan 30 at 19:49
  • As far as Windows.Security.Cryptography.Core.AsymmetricKeyAlgorithmProvider.ImportKeyPair concerned, you can Read text from a file by using a buffer(IBuffer) according to Create, write, and read a file. Feb 1 at 1:30

1 Answer 1

3

Winapi.Security.Cryptography contains wrappers for Windows.Security.Cryptography.Core (Windows UWP)

Check this page first: https://learn.microsoft.com/en-us/uwp/api/windows.security.cryptography.core.asymmetrickeyalgorithmprovider?view=winrt-22621

There is a C# app example that demonstrates how to use this interface. Details about IBuffer can be found on this page:

https://learn.microsoft.com/en-us/uwp/api/windows.storage.streams.ibuffer?view=winrt-22621

This is probably your first step, if you want encrypt with RSA PKCS1:

uses
  System.Win.WinRT,
  Winapi.WinRT,      
  Winapi.Security.Cryptography,
  Winapi.Storage.Streams;
const
  KeyLength = 512;
var 
  objAlgProv: Core_IAsymmetricKeyAlgorithmProvider;
  data: TBytes;
  cleardata, encrypted: IBuffer;
  key: Core_ICryptographicKey;
  hs: HSTRING;
  EncryptedBase64: string;
begin
  data := TEncoding.UTF8.GetBytes('clear text');
  objAlgProv := TCore_AsymmetricKeyAlgorithmProvider.OpenAlgorithm(TCore_AsymmetricAlgorithmNames.RsaPkcs1);
  key := objAlgProv.CreateKeyPair(KeyLength);
  cleardata := TCryptographicBuffer.CreateFromByteArray(length(data), @data[0]);
  encrypted := TCore_CryptographicEngine.Encrypt(key, cleardata, nil {IV});
  hs := TCryptographicBuffer.EncodeToBase64String(encrypted); 
  EncryptedBase64 := TWindowsString.HStringToString(hs);
end;

If you have further trouble translating the solution into Delphi, show your code an explain the details what you need to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.