Re: AW: AW: AW: AW: AW: RSA_public_decrypt problem
--=-eRjik8T7HkhD2zG8XLDf
Content-Transfer-Encoding: 7bit
Content-Type: text/plain
Hello,[color=blue]
> N:
> 008796FB4EAAB5FCC21619608ECB34D4BD82D062BF136A54E7E0BF6B2991C2F0F93A161930D650AF939C8282431D291D0E6E9F69A09AF091345D60439569C5CB5ECA566740B6A69FE4BBF2DB9CC03786AEDF8F9522EB7F6096A1B900140E6AA7AF55198B87E68A69546631E9EF90666984123F5364BE2EA6E067BBAA8831A34B15
>
> E:
> 0040000081[/color]
After modifying sample program "decryption" with your public key
seems to work good.
Best regards,
--
Marek Marcola <Marek.Marcola@malkom.pl>
--=-eRjik8T7HkhD2zG8XLDf
Content-Transfer-Encoding: 7bit
Content-Type: text/x-csrc; name=rsa_test13.c; charset=UTF-8
Content-Disposition: attachment; filename=rsa_test13.c
#include <stdio.h>
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
int print_hex(unsigned char *buf, int len)
{
int i;
int n;
printf(" ");
for (i = 0, n = 0; i < len; i++) {
if (n > 7) {
printf("\n ");
n = 0;
}
printf("0x%02x ", buf[i]);
n++;
}
printf("\n");
return (0);
}
int log_ssl(void)
{
char buf[256];
u_long err;
while ((err = ERR_get_error()) != 0) {
ERR_error_string_n(err, buf, sizeof(buf));
printf("*** %s\n", buf);
}
return (0);
}
int main()
{
RSA *rsa_pub;
unsigned char enc_bin[1024];
int enc_len;
unsigned char dec_bin[1024];
int dec_len;
char N[] = { "008796FB4EAAB5FCC21619608ECB34D4BD82D062BF136A54E7E0BF6B2991C2F0F93A161930D650AF939C8282431D291D0E6E9F69A09AF091345D60439569C5CB5ECA566740B6A69FE4BBF2DB9CC03786AEDF8F9522EB7F6096A1B900140E6AA7AF55198B87E68A69546631E9EF90666984123F5364BE2EA6E067BBAA8831A34B15" };
char E[] = { "0040000081" };
char msg[] = { "xyz" };
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
RAND_load_file("/dev/urandom", 1024);
if ((rsa_pub = RSA_new()) == NULL) {
goto err;
}
/* load public key */
printf("public key parameters:\n");
if (!BN_hex2bn(&rsa_pub->n, N)) {
goto err;
}
printf(" N: %s\n", N);
printf(" n: %s\n", BN_bn2hex(rsa_pub->n));
if (!BN_hex2bn(&rsa_pub->e, E)) {
goto err;
}
printf(" E: %s\n", E);
printf(" e: %s\n", BN_bn2hex(rsa_pub->e));
printf("public key size : %d bits\n", RSA_size(rsa_pub) * 8);
/* prepare "encrypted" data */
enc_len = RSA_size(rsa_pub);
memset(enc_bin, 1, enc_len);
/* decrypt */
if ((dec_len = RSA_public_decrypt(enc_len, enc_bin, dec_bin, rsa_pub,
RSA_NO_PADDING)) < 0) {
goto err;
}
printf("decrypted data:\n");
print_hex(dec_bin, dec_len);
return (0);
err:
log_ssl();
return (1);
}
--=-eRjik8T7HkhD2zG8XLDf--
______________________________________________________________________
OpenSSL Project [url]http://www.openssl.org[/url]
User Support Mailing List [email]openssl-users@openssl.org[/email]
Automated List Manager [email]majordomo@openssl.org[/email]
Simple Hello World Example of RSA encryption using OpenSSL
I could not find any simple examples of RSA encryption using OpenSSL and C/C++ Here is the one I came up with. The make file has more than it needs (ORACLE, Google etc) but it works
Generate key
openssl genrsa -out privkey.pem 2048
HelloWord.cpp
#include <global_inc.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
int main()
{
char *message = "Hello World";
unsigned char* encrypted = (unsigned char *) malloc(500);
unsigned char* decrypted = (unsigned char *) malloc(500);
int bufSize;
FILE *keyfile = fopen("privkey.pem", "r");
RSA *rsa = PEM_read_RSAPrivateKey(keyfile, NULL, NULL, NULL);
printf("\n\nStarting Message = %s\n", message);
if (rsa == NULL)
{
printf("Badness has occured! Did not read key file\n");
return 0;
}
else
{
printf("Opened the key file OK!\n");
}
bufSize = RSA_public_encrypt(strlen(message), (unsigned char *) message, encrypted, rsa, RSA_PKCS1_PADDING);
if (bufSize == -1)
{
printf("Badness has occured! encryption failed\n");
RSA_free(rsa);
return 0;
}
else
{
printf("Encrypted the message OK! = \n%s\n", encrypted );
}
if (RSA_private_decrypt(bufSize, encrypted, decrypted, rsa, RSA_PKCS1_PADDING) != -1)
{
printf("\nMessage decrypted to : %s\n", decrypted);
}
else
{
printf("Badness has occured! decryption failed\n");
RSA_free(rsa);
return 0;
}
RSA_free(rsa);
return 1;
}
Makefile
#-----------------------------------------------------------------------------
#
# File : global.make
# Date : 09/03/2009
# Author : Tom Nortillo
#
# Description: universal make definitions for development area
#
#-----------------------------------------------------------------------------
#----------------------------------
# GENERAL
#----------------------------------
CPP=g++
BASE=/home/joneil001/RSAEncryption
CPPFLAGS = -c -fPIC
LDFLAGS = -static
BIN = ${BASE}
#===================================================================
#
# THIRD-PARTY LIBRARIES
#
#===================================================================
#-------------------
# ORACLE
#-------------------
ORALIB= -L${ORACLE_LIB} -lclntsh
ORAINC= -I${ORACLE_HOME}/precomp/public -I${ORACLE_HOME}/rdbms/public
PROC=${ORACLE_BIN}/proc
ORAEXT = -DORACA_STORAGE_CLASS=extern -DSQLCA_STORAGE_CLASS=extern
#-------------------
# LIBXML
#-------------------
XML_INC = -I${BASE}/lib_xml/include/libxml2
XML_LIB = -L${BASE}/lib_xml/lib -lxml2
#--------------------------------
# GOOGLE PROTOCOL BUFFERS
#--------------------------------
GOOGLE_INC = -I${BASE}/lib_google/include
GOOGLE_LIB = -L${BASE}/lib_google/lib -lprotobuf
GOOGLE_BIN = ${BASE}/lib_google/bin
#==============================================
#
# OpenSSL
#
#=============================================
OPENSSL_LIB = -L/usr/lib64 -lcrypto -L/usr/lib64/openssl/engines -laep -lcswift -lchil -l4758cca -lgmp -lubsec -lsureware -lnuron -latalla
#===================================================================
#
# BUILD COMMAND-LINES
#
#===================================================================
#--------------------
# LIBRARIES
#--------------------
LIBLIST = -L${BASE}/lib \
${OPENSSL_LIB}
# Repeated twice because of library inter-dependencies
LIBS = ${LIBLIST} ${LIBLIST}
#--------------------
# INCLUDES
#--------------------
LOCAL_INC = -I.
INCLUDE = ${LOCAL_INC} ${ORAINC}
#===================================================================
#
# RULES
#
#===================================================================
.SUFFIXES: .cpp
.SUFFIXES: .cc $(SUFFIXES)
.SUFFIXES: .pc $(SUFFIXES)
.SUFFIXES: .proto $(SUFFIXES)
.cpp.o:
${CPP} ${CPPFLAGS} ${INCLUDE} $<
.cc.o:
${CPP} ${CPPFLAGS} ${INCLUDE} $<
.pc.o:
${PROC} SYS_INCLUDE=/usr/include include=${ORAINC} code=CPP cpp_suffix=cpp parse=NONE dbms=v8 iname=$< oname=$(*F).cpp lname=$(*F).lis
${CPP} ${CPPFLAGS} ${INCLUDE} ${ORAINC} ${ORAEXT} $*.cpp
rm -f $*.cpp
rm -f $*.lis
rm -f tp*
.proto.o:
${GOOGLE_BIN}/protoc --cpp_out=. $<
${CPP} ${CPPFLAGS} ${INCLUDE} ${ORAINC} ${ORAEXT} $*.pb.cc
#===================================================================
#
# TARGETS
#
#===================================================================
TARGET=doit
OBJECTS = HelloWorld.o
all: ${OBJECTS}
${CPP} ${INCLUDE} -o ${BIN}/${TARGET} ${OBJECTS} ${LIBS}
clean:
touch HelloWorld.o; rm *.o
FORGOT THE Global_inc.h
#ifndef _GLOBAL_INC_H_
#define _GLOBAL_INC_H_
#include <iostream>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fstream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <string>
#include <new>
#include <map>
#include <list>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
using namespace std;
#endif