2147144708:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
И
e_param->data_len = chph.size()-1;
тоже какая-то хрень. Зачем тут -1 ?
Вы нигде не используете len который вам возвращает EVP_EncryptUpdate
и почему-то считаете, что с out_buffer можно обращаться как с C-строкой (что неверно)
ты тут не докопировал или как? должно быть -lcrypto вроде.
$ g++ function.cpp main.cpp -o my_openssl_use -I/usr/local/ssl/include/openssl/ -L/usr/local/ssl/lib/ -lssl -lcrypto
g++ -I/usr/local/ssl/include/openssl/ -L/usr/local/ssl/lib/ -lssl -lcrypto -c -g -Wall -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/function.o.d" -o build/Debug/Cygwin_4.x-Windows/function.o function.cpp
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f "build/Debug/Cygwin_4.x-Windows/main.o.d"
g++ -I/usr/local/ssl/include/openssl/ -L/usr/local/ssl/lib/ -lssl -lcrypto -c -g -Wall -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/main.o.d" -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++ -I/usr/local/ssl/include/openssl/ -L/usr/local/ssl/lib/ -lssl -lcrypto -o dist/Debug/Cygwin_4.x-Windows/encript build/Debug/Cygwin_4.x-Windows/function.o build/Debug/Cygwin_4.x-Windows/main.o
build/Debug/Cygwin_4.x-Windows/function.o: In function `Z14encript_scringRSs':
function.cpp:36: undefined reference to `ERR_load_crypto_strings'
function.cpp:37: undefined reference to `OPENSSL_add_all_algorithms_noconf'
function.cpp:38: undefined reference to `OPENSSL_config'
function.cpp:46: undefined reference to `BIO_dump_fp'
function.cpp:60: undefined reference to `EVP_cleanup'
function.cpp:61: undefined reference to `ERR_free_strings'
g++ function.cpp main.cpp -o my_openssl_use -I -L/lib> -lssl -lcrypt
g++ function.cpp main.cpp -o my_openssl_use -I/usr/local/ssl/include/openssl/ -L/usr/local/ssl/lib/ -lssl -lcrypt
./config
make
make install
g++ -m32 -o my_encrypt_app -L c:/cygwin/usr/local/ssl/lib/ -lssl -lcrypt -c -g -I/cygdrive/c/cygwin/usr/local/ssl/include/ -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/function.o.d" -o build/Debug/Cygwin_4.x-Windows/function.o function.cpp
cc1plus: fatal error: build/Debug/Cygwin_4.x-Windows/function.d: No such file or directory
1) Убедись что openssl у тебя на машине установлен. Вместе с заголовочными файлами и библиотеками
2) Для того что бы собрать твой проект тебе нужно, что-то вроде этого
g++ -m32 -o my_encrypt_app -L c:/cygwin/usr/local/ssl/lib/ -lssl -lcrypt -c -g -include /cygdrive/c/cygwin/usr/local/ssl/include/ -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/function.o.d" -o build/Debug/Cygwin_4.x-Windows/function.o function.cpp
cc1plus: fatal error: build/Debug/Cygwin_4.x-Windows/function.d: No such file or directory
compilation terminated.
#ifndef FUNCTION_H
#define FUNCTION_H
#endif /* FUNCTION_H */
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "openssl/bio.h"
#include <string>
using namespace std ;
int encript_scring(string &);
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext);
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext);
void handleErrors(void);
#include "function.h"
#include <string.h>
#define BUFSIZE 1024
using namespace std;
int encript_scring(string &data)
{
/* Set up the key and iv. Do I need to say to not hard code these in a
* real application? :-)
*/
/* A 256 bit key */
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"01234567890123456";
/* Message to be encrypted */
unsigned char *plaintext =
(unsigned char *)"The quick brown fox jumps over the lazy dog";
/* Buffer for ciphertext. Ensure the buffer is long enough for the
* ciphertext which may be longer than the plaintext, dependant on the
* algorithm and mode
*/
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
unsigned char decryptedtext[128];
int decryptedtext_len, ciphertext_len;
/* Initialise the library */
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
/* Encrypt the plaintext */
ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv,
ciphertext);
/* Do something useful with the ciphertext here */
printf("Ciphertext is:\n");
BIO_dump_fp(stdout, (char *)ciphertext, ciphertext_len);
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
decryptedtext);
/* Add a NULL terminator. We are expecting printable text */
decryptedtext[decryptedtext_len] = '\0';
/* Show the decrypted text */
printf("Decrypted text is:\n");
printf("%s\n", decryptedtext);
/* Clean up */
EVP_cleanup();
ERR_free_strings();
return 0;
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
/*
* File: main.cpp
* Author: Максим
*
* Created on 7 мая 2014 г., 18:06
*/
#include "function.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
using namespace std ;
int main()
{
int sock, listener;
int buf_size = 1024;
struct sockaddr_in addr;
char buf[buf_size];
int bytes_read;
string msg("");
string encripted("");
listener = socket(AF_INET, SOCK_STREAM, 0);
if(listener < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if(bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
listen(listener, 1);
while(1)
{
sock = accept(listener, NULL, NULL);
if(sock < 0)
{
perror("accept");
exit(3);
}
switch(fork())
{
case -1:
perror("fork");
break;
case 0:
close(listener);
while(1)
{
bytes_read = recv(sock, &buf, buf_size, 0);
msg.append(buf, 0, bytes_read);
if(bytes_read <= 0 || bytes_read <= buf_size)
{
encripted = encript_scring(msg);
//send(sock, msg.c_str(), msg.size(), 0);
close(listener);
close(sock);
break;
}
}
close(sock);
_exit(0);
default:
close(sock);
}
}
close(listener);
return 0;
}