Beispiel #1
0
static int mod_decrypt(hcrypt_plaintext_t *pt, const hcrypt_ciphertext_t *ct,
	hcrypt_prvkey_t *sk)
{
	OPENSSL_assert(pt && ct && sk);
	OPENSSL_assert(pt->algor == hcrypt_elgamal());
	OPENSSL_assert(ct->algor == hcrypt_elgamal());
	OPENSSL_assert(sk->algor == hcrypt_elgamal());
	OPENSSL_assert(pt->u.elgamal);
	OPENSSL_assert(ct->u.elgamal);
	OPENSSL_assert(sk->u.elgamal);

	if (elgamal_decrypt(pt->u.elgamal, ct->u.elgamal, sk->u.elgamal) < 0) {
		fprintf(stderr, "decrypt failed\n");
		return -1;
	}

	return 0;
}
Beispiel #2
0
int main(void)
{
    int listenfd = 0, connfd = 0, i = 0, m_len, ack;
    struct sockaddr_in serv_addr;

    int sendBuff[mx_l], recvBuff[mx_l], message[mx_l];
    char str[mx_l], dummy;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    system("clear");
    printf("Server Initialized\n");
    printf("1. Put server on auto reply\n2. Put server on custom reply mode\n");
    scanf("%d",&ack);
    dummy=getchar();

    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff));
    memset(recvBuff, '0', sizeof(recvBuff));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(port);

    bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));

    if(listen(listenfd, 10) == -1) {
        printf("Failed to listen\n");
        return -1;
    }

    init_keys(); // Initialize crypto keys

    while(1) {
        connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL); // accept awaiting request
        puts("Connection with Client established\n");

        write(connfd, &pub_key, sizeof(pub_key));
        write(connfd, &ser_pub_key, sizeof(ser_pub_key));
        read(connfd, &cli_pub_key, sizeof(cli_pub_key));

        printf("pubic key = %d\n", pub_key);
        printf("Server public key = %d\n", ser_pub_key);
        printf("Client public key = %d\n", cli_pub_key);

        while(read(connfd, recvBuff, sizeof(recvBuff)-1)) {
            m_len=0;
            for (i=0;i<recvBuff[i]!=0;i++) {
                m_len++;
            }

            printf("\nMessage from client- (Encrypted message): \n");
            elgamal_decrypt(recvBuff, message, m_len, cli_pub_key, ser_pri_key);
            printf("Decrypted message from client: ");

            disp(message, m_len);

            if (ack==1) {
                strcpy(str, "Message recieved successfully");
            }

            else if (ack==2) {
                printf("Reply to client: ");
                gets(str);
            }

            write(connfd, str, sizeof(str)-1);
        }
    }
return 0;
}