I've use ssl to encrypt/decrypt strings with D7/ ICS-V5 and want to convert it to D2009
I get an AV when trying to decrypt a string in a call to the dll.

What am I doing wrong here ?
When do I have to use ansi or unicode ?

Paul



I'm using these routines :

Const
 RSA_PKCS1_PADDING                 = 1;

function DecryptPrivateRSA(
   X           : TX509Base;
   InBuf       : Pointer;
   InSize      : Cardinal;
   OutBuf      : Pointer;
   var OutSize : Cardinal): Boolean;
var
   Len       : Word;
   PrivKey   : PEVP_PKEY;
   BlockSize,
   Res       : Integer;
   InBufPtr,
   OutBufPtr : PAnsiChar;
begin
   Result  := FALSE;
if not Assigned(X) then exit; //raise Exception.Create('Cert not assigned');
   PrivKey := X.PrivateKey;
if PrivKey = nil then exit; //raise Exception.Create('Private key not loaded'); if PrivKey^.type_ <> EVP_PKEY_RSA then exit; //raise Exception.Create('Not a RSA key');
   Blocksize := f_EVP_PKEY_size(PrivKey);

   if (OutSize = 0) or (InSize = 0) or (InBuf = nil) or
      (OutBuf = nil) or (InSize > OutSize) then
   begin
       OutSize := InSize;
       Exit;
   end;
   OutSize   := 0;
   InBufPtr  := InBuf;
   OutBufPtr := OutBuf;
   repeat
       if InSize > BlockSize then
           Len := BlockSize
       else
           Len := InSize;
       if Len > 0 then begin
           Res := f_RSA_private_decrypt(  <==== error here
                                        Len,
                                        InBufPtr,
                                        OutBufPtr,
                                        PrivKey^.rsa,
                                        RSA_PKCS1_PADDING);
           Dec(InSize, Len);
           Inc(InBufPtr, Len);
           Inc(OutBufPtr, Res);
           Inc(OutSize, Res);
       end;
   until InSize = 0;
   Result := TRUE;
end;


function DecryptUrl(Password, DecryptPem, UrlFName: ansistring): ansistring;
var
   Buf   : Pointer;
   Len   : cardinal;
   S     : ansistring;
   X     : TX509Base;
   Fname : ansistring;
   Strm  : TFileStream;
   I     : Integer;
begin
   result:= '';
   X := TX509Base.Create(nil);
   try
     try
       if not FileExists(UrlFName) then begin
           ShowMessage('Url file missing');
// RpMessageDlg('RemotePass', msgYes, msgNo, 'Url file missing', rpmOK);
           exit;
       end;

       Strm:= TFileStream.Create(UrlFName, fmOpenRead);
       try
         Strm.ReadBuffer(I, SizeOf(Integer));
         SetLength(S, I);
         Strm.ReadBuffer(S[1],I);
       finally
         Strm.Free;
       end;
     except
     end;
       X.PrivateKeyLoadFromPemFile(DecryptPem, Password);
       Len:= length(s);
       GetMem(Buf, Len);
       Move(S[1], buf^, Len);
       if DecryptPrivateRSA(X, Buf, Len, @S[1], Len) then
       begin
           SetLength(S, Len);
           FreeMem(Buf);
       end
       else begin
           S:= '';
           ShowMessage('Invalif Key file');
       end;
   finally
       X.Free;
   end;
 result:= S;
end;



--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to