且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Synapse建立安全连接?

更新时间:2023-11-29 14:42:58

是的您可以使用Synapse随附的插件之一。正如其中也提到的那样,***的方法是使用 ssl_openssl.pas 。如果你决定遵循这一个,你将需要,除了 Sysapse OpenSSL库。作者建议 OpenSSL 0.9.7 ,但正如他在本地所说论坛似乎也与OpenSSL 1.0.0d一起工作。



注意,如果您使用D2009,您将需要一个Unicode支持,该版本不完全支持。请下载最新版本



以下示例代码接收前1024个字节作为使用SSL加密的安全网站的 HTTP GET 方法的响应。我用过它 OpenSSL 0.9.8h Synapse的最新版本注意,您需要从 OpenSSL $中放入 libssl32.dll libeay32.dll c $ c>包到您的输出目录,使其正常工作。我们有一个带有按钮和备忘录的表单,我们收到一个结果。

 使用blcksock,synautil,synsock,ssl_openssl,ssl_openssl_lib; 

procedure TForm1.Button1Click(Sender:TObject);
var Socket:TTCPBlockSocket;

begin
Socket:= TTCPBlockSocket.Create;

try
Socket.Connect('www.yousendit.com','443'); //连接到主机
Socket.SSLDoConnect; //启动SSL连接;只有服务器有证书

如果Socket.LastError = 0然后
begin
Socket.SendString('GET'+ CRLF); // request GET method
Memo1.Text:= Socket.RecvBufferStr(1024,1000); //接收1024个字节
end;

终于
Socket.Free;
结束
结束


I'm testing Synapse and want to know how can I establish a secure connection. I noticed it supports SSL, but I'm not sure whether it suits my needs. I don't have a certificate from CA. I just want to encrypt all data between my server program and client program. Sure, I can encrypt the data myself before sending out. But if SSL can encrypt the data, maybe I can just use it. From what I know, SSL is for "encryption" and "authentication". What I need is only "encryption". Is it possible with Synapse?

UPDATE:

Thanks for helping from daemon_x and the author of Synapse, Lukas Gebauer, I think I finally make it work. Here are what I did:

Server Side:

1) Uses ssl_openssl in your unit and put 'libeay32.dll' and 'ssleay32.dll' to the same directory of the exe file

2) After a connection is accepted, add following lines of code for the newly created socket.

fclient.SSLAcceptConnection;

Client side:

1) Uses ssl_openssl in your unit and put 'libeay32.dll' and 'ssleay32.dll' to the same directory of the exe file

2) After connected to the server, add following line.

fclient.SSLDoConnect;

If no error occurred, the connection is secure now. But when you run your code, as said in document of Synapse, you may notice that the SSLAcceptConnection takes some time to return. So if you want to speed things up, you better create a certificate file and private key file upfront. And add following code before SSLAcceptConnection

  fclient.SSL.CertificateFile := 'bs-cert';
  fclient.SSL.PrivateKeyFile := 'bs-privatekey';

If you don't have a certificate and private key, please refer to "CreateSelfSignedCert" in ssl_openssl for getting a self-signed certificate and private key. You can save, by WriteStrToStream for example, FCertificate and FPrivatekey to files and use them later.

Yes it is; you can use one of the plugins shipped with Synapse. As it's also mentioned there, the best is to use the ssl_openssl.pas. If you decide to follow this one you will need except Sysapse also the OpenSSL library. Author recommends OpenSSL 0.9.7 but as he said on our local forum it seems to works also with OpenSSL 1.0.0d.

Note if you are using D2009 up you will need a Unicode support which is not fully supported in version. Download the latest version instead.

The following sample code receives first 1024 bytes as a response to the HTTP GET method of a secured website using SSL encryption. I've used for it OpenSSL 0.9.8h with the latest version of Synapse. Note you need to put libssl32.dll and libeay32.dll from the OpenSSL package into your output directory to make it work properly. Let's have a form with a button and memo where we receive a result.

uses blcksock, synautil, synsock, ssl_openssl, ssl_openssl_lib;

procedure TForm1.Button1Click(Sender: TObject);
var Socket: TTCPBlockSocket;

begin
  Socket := TTCPBlockSocket.Create;

  try
    Socket.Connect('www.yousendit.com', '443'); // connect to the host
    Socket.SSLDoConnect; // start SSL connection; only server has a certificate

    if Socket.LastError = 0 then
      begin
        Socket.SendString('GET' + CRLF); // request GET method
        Memo1.Text := Socket.RecvBufferStr(1024, 1000); // receive 1024 bytes
      end;

  finally
    Socket.Free;
  end;
end;