Re: AW: AW: AW: AW: AW: RSA_public_decrypt problem - Openssl

This is a discussion on Re: AW: AW: AW: AW: AW: RSA_public_decrypt problem - Openssl ; --=-eRjik8T7HkhD2zG8XLDf Content-Transfer-Encoding: 7bit Content-Type: text/plain Hello, > N: > 008796FB4EAAB5FCC21619608ECB34D4BD82D062BF136A54E7 E0BF6B2991C2F0F93A161930D650AF939C8282431D291D0E6E 9F69A09AF091345D60439569C5CB5ECA566740B6A69FE4BBF2 DB9CC03786AEDF8F9522EB7F6096A1B900140E6AA7AF55198B 87E68A69546631E9EF90666984123F5364BE2EA6E067BBAA88 31A34B15 > > E: > 0040000081 After modifying sample program "decryption" with your public key seems to work good. Best regards, -- Marek Marcola --=-eRjik8T7HkhD2zG8XLDf Content-Transfer-Encoding: ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Re: AW: AW: AW: AW: AW: RSA_public_decrypt problem

  1. Re: AW: AW: AW: AW: AW: RSA_public_decrypt problem


    --=-eRjik8T7HkhD2zG8XLDf
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain

    Hello,
    > N:
    > 008796FB4EAAB5FCC21619608ECB34D4BD82D062BF136A54E7 E0BF6B2991C2F0F93A161930D650AF939C8282431D291D0E6E 9F69A09AF091345D60439569C5CB5ECA566740B6A69FE4BBF2 DB9CC03786AEDF8F9522EB7F6096A1B900140E6AA7AF55198B 87E68A69546631E9EF90666984123F5364BE2EA6E067BBAA88 31A34B15
    >
    > E:
    > 0040000081

    After modifying sample program "decryption" with your public key
    seems to work good.

    Best regards,
    --
    Marek Marcola

    --=-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
    #include

    #include
    #include
    #include
    #include
    #include

    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[] = { "008796FB4EAAB5FCC21619608ECB34D4BD82D062BF136A54E7 E0BF6B2991C2F0F93A161930D650AF939C8282431D291D0E6E 9F69A09AF091345D60439569C5CB5ECA566740B6A69FE4BBF2 DB9CC03786AEDF8F9522EB7F6096A1B900140E6AA7AF55198B 87E68A69546631E9EF90666984123F5364BE2EA6E067BBAA88 31A34B15" };
    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 http://www.openssl.org
    User Support Mailing List openssl-users@openssl.org
    Automated List Manager majordomo@openssl.org

  2. 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
    #include
    #include

    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
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include


    using namespace std;


    #endif

+ Reply to Thread