Problem on RSA_sign ... please help!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GenericUser
Forum Newbie
Posts: 1
Joined: Sun Jul 05, 2009 10:25 am

Problem on RSA_sign ... please help!!!

Post by GenericUser »

Hello to everybody!
I'm trying to rewrite the code below in php:

Code: Select all

#import <openssl/rsa.h>
#import <openssl/bio.h>
#import <openssl/pem.h>
 
unsigned char *privateKey = "....";
const char *clearText = "string test";
    
    /* get private RSA */   
    BIO *privateBIO = BIO_new_mem_buf(privateKey, strlen(privateKey));
    RSA *privateRSA = NULL;
    
    PEM_read_bio_RSAPrivateKey(privateBIO, &privateRSA, NULL, NULL);    
    
    /* generate hash string */
    unsigned char md[SHA_DIGEST_LENGTH];
    SHA1((unsigned char *)clearText, strlen(clearText), md);
    
    for(int i=0; i<sizeof(md); i++) printf("%x", md[i]);
    
    /* encrypt data */
    unsigned int outlen;
    unsigned char outbuf[RSA_size(privateRSA)];
    RSA_sign(NID_sha1, md, sizeof(md), outbuf, &outlen, privateRSA);
    
    BIO_free(privateBIO);
    RSA_free(privateRSA);
        
        for(int i=0; i<outlen; i++) printf("%c", outbuf[i]);
In php I've written:

Code: Select all

<?php 
    $private_key = "...";
    $stringToEncrypt = "string test";
    
    //create hash of string
    $hash = sha1(utf8_encode($stringToEncrypt));
    echo $hash;       
?>
But now I don't now how implement the RSA_sign method! If I use openssl_sign the output is not he same!!!
I'm sorry but I've few knowledgments of php!!!

Thanks in advance for any help!
Post Reply