EldoS | Feel safer!

Software components for data protection, secure storage and transfer

RSA encoding

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#37297
Posted: 07/25/2016 08:19:14
by BETASI  (Basic support level)
Joined: 07/25/2016
Posts: 3

Hello

I need to encode message using public key (PEM file).

My code:

Code

RSAc := TElRSAPublicKeyCrypto.create (SB_OID_RSAOAEP) ;
  RSAc.CryptoType := rsapktPKCS1;
  StreamRead := TFileStream.Create('C:\pubkey.pem', fmOpenRead Or fmShareDenyWrite);

  KeyMaterial := TElRSAKeyMaterial.Create;
  KeyMaterial.LoadPublic(StreamRead, StreamRead.Size);
  if not KeyMaterial.PublicKey then
    raise Exception.Create('Brak klucza prywatnego!');

  //Cert.LoadFromStreamPEM(StreamRead, StreamRead.Size);
  //KeyMaterial.LoadPublic(StreamRead, StreamRead.Size);
  //KeyMaterial.LoadPublic(@sModulus[1], length(sModulus) , @sExp[1] , length(sExp) );
  //
  sin := TStringStream.Create(Base64(Klucz),TEncoding.UTF8);
  showMessage(sin.DataString);
  //sin.WriteString(Klucz);
  //sout := TStringStream.Create('',TEncoding.UTF8);
  try
    RSAc.KeyMaterial := KeyMaterial;
    RSAc.InputEncoding := pkeBase64;
    RSAc.OutputEncoding := pkeBase64;
    RSAc.Encrypt(sin,sout);
  except
    on e:Exception do
      raise Exception.Create(e.Message);
  end;
#37299
Posted: 07/25/2016 08:25:36
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Its not clear from you message if you have any problems with the code.

Also its not recommended to use TStringStream as it may corrupt data. Please use TMemoryStream instead.
#37300
Posted: 07/25/2016 08:45:08
by BETASI  (Basic support level)
Joined: 07/25/2016
Posts: 3

Hello! I'm sorry, I have not finished writing my previous messages. I changed TStringStream to TMemoryStream and now it is working properly! Thank you a lot!

Best regards,
MichaƂ
#37310
Posted: 07/26/2016 01:46:25
by BETASI  (Basic support level)
Joined: 07/25/2016
Posts: 3

How can I load private key from certificate? It begins with
Quote
-----BEGIN CERTIFICATE-----


My code:
Code
function TfrmExJpk.EncodeRSA(Key: String): String;
var
  rsac : TElRSAPublicKeyCrypto;
  KeyMaterial : TElRSAKeyMaterial;
  StreamRead : TFileStream;
  Sin, Sout : TMemoryStream;
  Cert : TElX509Certificate;
begin
  RSAc := TElRSAPublicKeyCrypto.create (SB_OID_RSAENCRYPTION) ;
  RSAc.CryptoType := rsapktPKCS1;
  RSAc.InputEncoding := pkeBase64;
  RSAc.OutputEncoding := pkeBase64;

  StreamRead := TFileStream.Create('pubkey.pem', fmOpenRead Or fmShareDenyWrite);

  KeyMaterial := TElRSAKeyMaterial.Create;
  KeyMaterial.LoadPublic(StreamRead, StreamRead.Size);
  if not KeyMaterial.PublicKey then
    raise Exception.Create('Brak klucza prywatnego!');

  RSAc.KeyMaterial := KeyMaterial;

  sin := TMemoryStream.Create;
  sin.Write(Pointer(Klucz)^, length(Klucz));
  sout := TMemoryStream.Create;
  try
    RSAc.Encrypt(sin,sout);
  except
    on e:Exception do
      raise Exception.Create(e.Message);
  end;
  Result := MemoryStreamToString(sout);
end;


pubkey.pem begins with

Quote
-----BEGIN PUBLIC KEY-----
#37311
Posted: 07/26/2016 02:35:21
by Vsevolod Ievgiienko (EldoS Corp.)

Quote
How can I load private key from certificate?

You should use TElX509Certificate.LoadFrom*PEM method. Details: https://www.eldos.com/documentation/sb...icate.html
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 389 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!