国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenSSL實(shí)現(xiàn)SSL網(wǎng)絡(luò)通信

這篇具有很好參考價(jià)值的文章主要介紹了OpenSSL實(shí)現(xiàn)SSL網(wǎng)絡(luò)通信。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Certainly! Here are the C language programs for a simple OpenSSL client and server that can establish a secure communication channel between them:
l
inux環(huán)境下
OpenSSL Server Program (server.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#define CERT_FILE "server.pem"
#define KEY_FILE "server.key"

void init_openssl() {
    SSL_load_error_strings();
    OpenSSL_add_ssl_algorithms();
}

SSL_CTX* create_context() {
    const SSL_METHOD *method;
    SSL_CTX *ctx;

    method = SSLv23_server_method();
    ctx = SSL_CTX_new(method);
    if (!ctx) {
        perror("Unable to create SSL context");
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    return ctx;
}

void configure_context(SSL_CTX *ctx) {
    SSL_CTX_set_ecdh_auto(ctx, 1);

    if (SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM) <= 0) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    if (SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM) <= 0) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }
}

int main() {
    int sockfd, clientfd;
    struct sockaddr_in serv_addr, client_addr;
    socklen_t client_len;
    SSL_CTX *ctx;
    SSL *ssl;

    init_openssl();
    ctx = create_context();

    configure_context(ctx);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("Unable to create socket");
        exit(EXIT_FAILURE);
    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(8888);

    if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("Unable to bind socket");
        exit(EXIT_FAILURE);
    }

    if (listen(sockfd, 5) < 0) {
        perror("Unable to listen");
        exit(EXIT_FAILURE);
    }

    printf("Server listening on port 8888...\n");

    while (1) {
        client_len = sizeof(client_addr);
        clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_len);
        if (clientfd < 0) {
            perror("Unable to accept connection");
            exit(EXIT_FAILURE);
        }

        ssl = SSL_new(ctx);
        SSL_set_fd(ssl, clientfd);

        if (SSL_accept(ssl) <= 0) {
            ERR_print_errors_fp(stderr);
        } else {
            printf("SSL connection established\n");

            char buffer[1024];
            memset(buffer, 0, sizeof(buffer));
            SSL_read(ssl, buffer, sizeof(buffer));
            printf("Received: %s\n", buffer);

            char *response = "Hello from server";
            SSL_write(ssl, response, strlen(response));
            printf("Response sent\n");

            SSL_shutdown(ssl);
            SSL_free(ssl);
        }

        close(clientfd);
    }

    close(sockfd);
    SSL_CTX_free(ctx);
    cleanup_openssl();

    return 0;
}

OpenSSL Client Program (client.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888

void init_openssl() {
    SSL_load_error_strings();
    OpenSSL_add_ssl_algorithms();
}

SSL_CTX* create_context() {
    const SSL_METHOD *method;
    SSL_CTX *ctx;

    method = SSLv23_client_method();
    ctx = SSL_CTX_new(method);
    if (!ctx) {
        perror("Unable to create SSL context");
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    return ctx;
}

void configure_context(SSL_CTX *ctx) {
    SSL_CTX_set_default_verify_paths(ctx);
}

int main() {
    int sockfd;
    struct sockaddr_in serv_addr;
    SSL_CTX *ctx;
    SSL *ssl;

    init_openssl();
    ctx = create_context();

    configure_context(ctx);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("Unable to create socket");
        exit(EXIT_FAILURE);
    }

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SERVER_PORTApologies for the abrupt cutoff in the code. Here's the continuation of the OpenSSL client program (client.c):

```c
    if (inet_pton(AF_INET, SERVER_IP, &(serv_addr.sin_addr)) <= 0) {
        perror("Unable to convert server IP address");
        exit(EXIT_FAILURE);
    }

    if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("Unable to connect to server");
        exit(EXIT_FAILURE);
    }

    ssl = SSL_new(ctx);
    SSL_set_fd(ssl, sockfd);

    if (SSL_connect(ssl) <= 0) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    printf("SSL connection established\n");

    char *message = "Hello from client";
    SSL_write(ssl, message, strlen(message));
    printf("Message sent\n");

    char buffer[1024];
    memset(buffer, 0, sizeof(buffer));
    SSL_read(ssl, buffer, sizeof(buffer));
    printf("Received: %s\n", buffer);

    SSL_shutdown(ssl);
    SSL_free(ssl);
    close(sockfd);
    SSL_CTX_free(ctx);
    cleanup_openssl();

    return 0;
}

To compile and run the programs, you’ll need to make sure you have the OpenSSL library installed on your system. Use the following command to compile the server and client programs:

gcc -o server server.c -lssl -lcrypto
gcc -o client client.c -lssl -lcrypto

Once compiled, you can run the server and client programs separately in different terminal windows. First, start the server:

./server

Then, in another terminal window, run the client:

./client

You should see the client and server establish an SSL connection and exchange messages securely.

Please let me know if you need any further explanation or examples. If you’re ready, we can proceed to the next section: “SSL Certificates and Keys.”文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-741487.html

到了這里,關(guān)于OpenSSL實(shí)現(xiàn)SSL網(wǎng)絡(luò)通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 使用 python socket 實(shí)現(xiàn)UDP/TCP網(wǎng)絡(luò)通信

    使用 python socket 實(shí)現(xiàn)UDP/TCP網(wǎng)絡(luò)通信

    目錄 目錄 1.socket簡(jiǎn)介 2.創(chuàng)建socket 2.1創(chuàng)建UDPSocket 2.2創(chuàng)建TCPSocket 3.使用UDPSocket發(fā)送數(shù)據(jù)并接收 4.使用UDPSocket發(fā)送廣播 5.UDPSocket聊天器 (多線程實(shí)現(xiàn)消息的收發(fā)功能) 6.使用TCPSocket建立客戶端 7.使用TCPSocket建立服務(wù)端 ? ? ? ?socket(簡(jiǎn)稱:套接字),是支持TCP和UDP(網(wǎng)絡(luò)傳輸方式

    2023年04月10日
    瀏覽(25)
  • 【網(wǎng)絡(luò)原理】使用Java基于UDP實(shí)現(xiàn)簡(jiǎn)單客戶端與服務(wù)器通信

    【網(wǎng)絡(luò)原理】使用Java基于UDP實(shí)現(xiàn)簡(jiǎn)單客戶端與服務(wù)器通信

    我們用Java實(shí)現(xiàn)UDP數(shù)據(jù)報(bào)套接字編程,需要借用以下API來(lái)實(shí)現(xiàn) 網(wǎng)絡(luò)編程, 本質(zhì)上是要操作網(wǎng)卡. 但是網(wǎng)卡不方便直接操作. 在操作系統(tǒng)內(nèi)核中, 使用了一種特殊的叫做 “socket” 這樣的文件來(lái)抽象表示網(wǎng)卡. 因此進(jìn)行網(wǎng)絡(luò)通信, 勢(shì)必需要先有一個(gè) socket 對(duì)象. DatagramSocket 是UDP Socket,

    2024年03月11日
    瀏覽(26)
  • 【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(3):使用三層交換機(jī)實(shí)現(xiàn)跨VLAN間的通信

    【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(3):使用三層交換機(jī)實(shí)現(xiàn)跨VLAN間的通信

    【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(1):交換機(jī)的VLAN劃分 【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(2):交換機(jī)間的VLAN通信 【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(3):使用三層交換機(jī)實(shí)現(xiàn)跨VLAN間的通信 【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(4):綜合實(shí)驗(yàn)作業(yè)之辦公室的跨VLAN通信 【計(jì)算機(jī)網(wǎng)絡(luò)】思科實(shí)驗(yàn)(5):?jiǎn)伪勐酚煽鏥LAN通信

    2024年02月03日
    瀏覽(95)
  • Java【網(wǎng)絡(luò)編程2】使用 TCP 的 Socket API 實(shí)現(xiàn)客戶端服務(wù)器通信(保姆級(jí)教學(xué), 附代碼)

    Java【網(wǎng)絡(luò)編程2】使用 TCP 的 Socket API 實(shí)現(xiàn)客戶端服務(wù)器通信(保姆級(jí)教學(xué), 附代碼)

    ??各位讀者好, 我是小陳, 這是我的個(gè)人主頁(yè) ??小陳還在持續(xù)努力學(xué)習(xí)編程, 努力通過(guò)博客輸出所學(xué)知識(shí) ??如果本篇對(duì)你有幫助, 煩請(qǐng)點(diǎn)贊關(guān)注支持一波, 感激不盡 ?? 希望我的專欄能夠幫助到你: JavaSE基礎(chǔ): 基礎(chǔ)語(yǔ)法, 類和對(duì)象, 封裝繼承多態(tài), 接口, 綜合小練習(xí)圖書(shū)管理系統(tǒng)

    2024年02月05日
    瀏覽(33)
  • Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)通信

    Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)通信

    在標(biāo)準(zhǔn)C++中沒(méi)有提供專門(mén)用于套接字通信的類,所以只能使用操作系統(tǒng)提供的基于C語(yǔ)言的API函數(shù),基于這些C的API函數(shù)我們也可以封裝自己的C++類?;蛘呶覀兛梢允褂肣t框架,它提供了用于套接字通信的類(TCP、UDP)這樣我們就可以直接調(diào)用相關(guān)API即可。 使用Qt提供的類進(jìn)行基于

    2024年04月17日
    瀏覽(23)
  • Unity實(shí)現(xiàn)網(wǎng)絡(luò)通信(UDP)

    Unity實(shí)現(xiàn)網(wǎng)絡(luò)通信(UDP)

    UDP通信特點(diǎn): ? ? ? ? 無(wú)連接,多對(duì)多 ? ? ? ? 不可靠 ? ? ? ? 面向數(shù)據(jù)報(bào) ? ? ? ? 效率高 UDP中的分包與黏包 分包:一段數(shù)據(jù)被分為兩段或多段傳輸,在UDP通信方式中,因?yàn)閁DP的不可靠性無(wú)法保證有序傳輸,因此盡量避免UDP自動(dòng)分包。 ????????其中一種方式是保證消

    2024年02月04日
    瀏覽(17)
  • Linux對(duì)網(wǎng)絡(luò)通信的實(shí)現(xiàn)

    Linux對(duì)網(wǎng)絡(luò)通信的實(shí)現(xiàn)

    1、OP_WRITE觸發(fā)條件:當(dāng)操作系統(tǒng)寫(xiě)緩沖區(qū)有空閑時(shí)就緒。一般情況下寫(xiě)緩沖區(qū)都有空閑空間,小塊數(shù)據(jù)直接寫(xiě)入即可,沒(méi)必要注冊(cè)該操作類型,否則該條件不斷就緒浪費(fèi)cpu;但如果是寫(xiě)密集型的任務(wù),比如文件下載等,緩沖很可能滿,注冊(cè)該操作類型很有必要,同時(shí)注意寫(xiě)完

    2024年02月08日
    瀏覽(20)
  • C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

    C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

    ??SOCKET網(wǎng)絡(luò)通信系列文章鏈接如下:?? ??【小沐學(xué)python】(一)Python簡(jiǎn)介和安裝?? ??Python實(shí)現(xiàn)socket網(wǎng)絡(luò)通信?? ??C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信?? ??Android實(shí)現(xiàn)socket網(wǎng)絡(luò)通信?? ??nodejs實(shí)現(xiàn)socket網(wǎng)絡(luò)通信?? 《斗詩(shī)篇》 陳獻(xiàn)章: 窗外竹青青,窗間人獨(dú)坐。 究竟竹與人,原來(lái)無(wú)兩

    2023年04月09日
    瀏覽(20)
  • Java 網(wǎng)絡(luò)編程詳解:實(shí)現(xiàn)網(wǎng)絡(luò)通信的核心技術(shù)

    網(wǎng)絡(luò)編程是指利用計(jì)算機(jī)網(wǎng)絡(luò)進(jìn)行數(shù)據(jù)交換和通信的過(guò)程。它涉及到在不同主機(jī)之間傳輸數(shù)據(jù),并允許不同設(shè)備之間進(jìn)行連接和通信。網(wǎng)絡(luò)編程不僅限于互聯(lián)網(wǎng),也可以包括局域網(wǎng)或廣域網(wǎng)等各種網(wǎng)絡(luò)環(huán)境。 在當(dāng)今的互聯(lián)網(wǎng)時(shí)代,幾乎所有的應(yīng)用都需要在不同設(shè)備之間進(jìn)行數(shù)

    2024年02月11日
    瀏覽(20)
  • 安全通信設(shè)置:使用 OpenSSL 為 Logstash 和 Filebeat 提供 SSL 證書(shū)

    安全通信設(shè)置:使用 OpenSSL 為 Logstash 和 Filebeat 提供 SSL 證書(shū)

    在為 Elasticsearch 采集數(shù)據(jù)時(shí),我們經(jīng)常使用到 Filebeat 及 Logstash。在我們之前的很多教程中,我們通常不為 Filebeat 和 Logstash 之前的通信做安全配置。 如何為 Filebeat 及 Logstash 直接建立安全的鏈接?這個(gè)在很多的情況下是非常有用的。在我之前的文章 “Elasticsearch:為日志分析

    2024年02月21日
    瀏覽(46)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包