1

EDIT: because i couldn't get this code to work, i did an little workaround: now i call openssl console via ShellExecute (not much safe, but it works).

Good morning all, i'm getting an headache to get this delphi code work:

(included uTPLb_* libraries)
function Crypto.encodeRSA(msg:String;test:Boolean = False):String;
  var
    file_path:String;
    keyStream:TMemoryStream;
    res:String;
    Codec:TCodec;
    wasAborted:Boolean;
    KeyPair:TAsymetricKeyPair;
    Key:TSymetricKey;
    lib:TCryptographicLibrary;
    signatory:TSignatory;
  begin
    codec:=TCodec.Create(nil);
    signatory:=TSignatory.Create(nil);
    lib:=TCryptoGraphicLibrary.Create(nil);
    try
     if test then
        begin
          file_path:=PUBLIC_KEY_TEST_FILE;
        end
      else
        begin
          file_path:=PUBLIC_KEY_FILE;
        end;
      file_path:=GetCurrentDir+'\'+file_path;
      keyStream:=TMemoryStream.Create;
      keyStream.LoadFromFile(file_path);

      codec.Reset;
      codec.CryptoLibrary:=lib;
      codec.ChainModeId:=ECB_ProgID;
      codec.StreamCipherId:='native.RSA';
      Codec.StreamCipherId:=RSA_ProgID;

      signatory.Codec:=codec;
      signatory.LoadKeysFromStream(keyStream,[partPublic]);


      //stuff to do...
      Result:=res;

    finally
      lib.Free;
      codec.Free;
    end;
  end;

The problem is "signatory.LoadKeysFromStream(keyStream,[partPublic]);" procedure continue to fail with "Stream Error" exception; file is correctly open in reading mode.

Content of the public key file is

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsU4LA0ZLWO+twuoReM41
...
-----END PUBLIC KEY-----

(i have full key, but i prefer not divulgate)

Does anyone can help me? I read about pkcs#1 padding and openssl command (Lockbox 3 load public key not possible - stream reading error)

openssl rsa -pubin -in yourpublickey -RSAPublicKey_out -out publicpkcs1

but "-RSAPublicKey_out" isn't recognized (OpenSSL 0.9.8zh 14 Jan 2016). I have also private key.

Thanks to all which can help me!

update: i tried also generating cryptographic keys, but i cannot read them.

signatory.storeKeysToStream

the result is the same.

I downloaded library here: https://github.com/TurboPack/LockBox

Marco P
  • 61
  • 1
  • 5

0 Answers0