Hello everyone,
                      Newbie here !  Saw a post similar to my problem, applied whatever solution was proposed and still am stumped. I wrote a small code, to apply RSA_public_encrypt on a hex string and get different results for the ciphertext everytime, even though I make sure that the same key is used !
 
I tried encrypting 6037D453AD637FD8B3B849CD95A07735AB50C327, the "to be encypted" text

 and got
 986AE5084B6988A8B9CBDEF2071D344C8DA7C2DE 1st time I ran gdb
 984A5B087A98B7D7E8FA0D21364C637B809AB5D1 2nd time
 98BA30090D2B4A6A7B8DA0B4C9DFF60E4D67829E 3rd time
 183E0709586E859DB6D0EB072442618118000000 4th time
 
IS there some concept of using system time when encrypting so that even though you may encrypt with same key and same plaintext you get different O/P for ciphertext ? Sounds strange !!
 
I made sure that the padding I used, RSA_NO_PADDING, conformed to the conditions on RSA_size() being equal to the "flen" still I get these different results..I've attached the snippet of code with this mail, I'd love it if someone could help  me out.
 
Thanx in advance, Anirban :)
 
I'm using FC2, and openssl-0.9.7g
*******the code follows..*****************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <openssl/rand.h>
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include <openssl/rc4.h>
#include <openssl/aes.h>

int main()
{
        int loop;
        BIGNUM *num,*key,*encrypted;
        RSA *digestobject;
        char *from,*to1=NULL;
        unsigned char *to=NULL,*to2=NULL;
        num=BN_new();
        key=BN_new();
        encrypted=BN_new();

        BN_hex2bn(&key,"1aab8e2d9558dc9abe52fd00a698918fd4d310ac");
        digestobject=RSA_generate_key(160,RSA_3,NULL,NULL);
        (digestobject->n)=key;
        printf("RSA_size is %d",RSA_size(digestobject));
        from="6037D453AD637FD8B3B849CD95A07735AB50C327";
        printf("\n\njust before encryption string is %s..", from);
        printf("\n and here's the BN ");
        BN_print_fp(stdout,num);
        to2=(unsigned char *)malloc(RSA_size(digestobject));
        loop=1;
        loop=RSA_public_encrypt(20,(unsigned char *)from,(unsigned char
*)to2,digestobject,RSA_NO_PADDING);
        printf("\njust after encryption string is %s ..",to2);
        encrypted=BN_bin2bn((const unsigned char *)to2,20,NULL);
        printf("\nhere comes the encrypted BN..");
        BN_print_fp(stdout,encrypted);

loop=RSA_public_encrypt(20,(unsigned char *)from,(unsigned char
*)to2,digestobject,RSA_NO_PADDING);
        printf("\njust after encryption string is %s ..",to2);
        encrypted=BN_bin2bn((const unsigned char *)to2,20,NULL);
        printf("\nhere comes the encrypted BN..");
        BN_print_fp(stdout,encrypted);
        loop=RSA_public_encrypt(20,(unsigned char *)from,(unsigned char
*)to2,digestobject,RSA_NO_PADDING);
        printf("\njust after encryption string is %s ..",to2);
        encrypted=BN_bin2bn((const unsigned char *)to2,20,NULL);
        printf("\nhere comes the encrypted BN..");
        BN_print_fp(stdout,encrypted);
}

******************************************************************

Reply via email to