using openssl and sunjce, encrypt same text,get different result

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

using openssl and sunjce, encrypt same text,get different result

cellecial
Hi,
I need to encrypt data using C+openssl,then decrypt data using JAVA+SUNJCE.
When I encrypt the same plaintext using openssl and sunjce,
I found the two ciphertexts  are different.
I tried two algorithms:EVP_des_ede() / "DES" ,EVP_rc4() / "RC4",
but the results are all depressing.

I googled a post in which someone said,"you can use open ssl to encrypt the data and decrypt the same using java (any JCE implementaions) .Provided you have to use same algorthim with correct pading and initialisation vectors ."
I think this is my problem.Maybe I don't know the correct padding or iv.
So,can anyone help me to solve this problem?
Thank you very much.

Here is my main code.

/* C+openssl encrypt key is parameter[strref] ,such as strref="123456"; */
int M_encrypt(unsigned char *in,int inl,unsigned char *out,int *outl,
                unsigned char *strref,int strrefl)

{
        EVP_CIPHER_CTX ctx;
        unsigned char ekey[EVP_MAX_KEY_LENGTH];
        unsigned char eiv[EVP_MAX_IV_LENGTH];
        int tlen,flen;
               
        EVP_BytesToKey(EVP_des_ede(),EVP_md5(),NULL,strref,strrefl,1,ekey,eiv);

       
        EVP_EncryptInit(&ctx,cipher,ekey,eiv);
    EVP_EncryptUpdate(&ctx,out,&tlen,in,inl);
        flen=tlen;
        EVP_EncryptFinal(&ctx,out+flen,&tlen);
        flen+=tlen;
        *outl=flen;
       
        return 0;
}

/* JAVA+sun jce, encrypt key is parameter[key],byte[] key=new String("123456").getBytes(); */
public static byte[] encrypto(byte[] input,byte[] key) throws Exception{
            SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,"DES");
       
            Cipher c1 = Cipher.getInstance("DES");
            c1.init(Cipher.ENCRYPT_MODE,deskey);
            byte[] cipherByte=c1.doFinal(input);
       
            return cipherByte;
}
        


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: using openssl and sunjce, encrypt same text,get different result

Bear Giles
I don't know about the RC4 problem, but EVP_des_ede is triple DES.
  I thought JCE "DES" is single DES.  (Single DES is depreciated
now, BTW.  I thought there were also questions about RC4.)

I also wonder if you're generating the same key, but I'll leave
the nuances of the key generation procedures to others.  (E.g., I
thought the FIPS standard encrypted the key with itself and didn't
use a hash.  If both procedures should use a hash, how do you
specify it in the JCE?  Etc.)

Bear


cellecial wrote:

> Hi,
> I need to encrypt data using C+openssl,then decrypt data using JAVA+SUNJCE.
> When I encrypt the same plaintext using openssl and sunjce,
> I found the two ciphertexts  are different.
> I tried two algorithms:EVP_des_ede() / "DES" ,EVP_rc4() / "RC4",
> but the results are all depressing.
>
> I googled a post in which someone said,"you can use open ssl to encrypt the data and decrypt the same using java (any JCE implementaions) .Provided you have to use same algorthim with correct pading and initialisation vectors ."
> I think this is my problem.Maybe I don't know the correct padding or iv.
> So,can anyone help me to solve this problem?
> Thank you very much.
>
> Here is my main code.
>
> /* C+openssl encrypt key is parameter[strref] ,such as strref="123456"; */
> int M_encrypt(unsigned char *in,int inl,unsigned char *out,int *outl,
> unsigned char *strref,int strrefl)
>
> {
> EVP_CIPHER_CTX ctx;
> unsigned char ekey[EVP_MAX_KEY_LENGTH];
> unsigned char eiv[EVP_MAX_IV_LENGTH];
> int tlen,flen;
>
> EVP_BytesToKey(EVP_des_ede(),EVP_md5(),NULL,strref,strrefl,1,ekey,eiv);
>
>
> EVP_EncryptInit(&ctx,cipher,ekey,eiv);
>     EVP_EncryptUpdate(&ctx,out,&tlen,in,inl);
> flen=tlen;
> EVP_EncryptFinal(&ctx,out+flen,&tlen);
> flen+=tlen;
> *outl=flen;
>
> return 0;
> }
>
> /* JAVA+sun jce, encrypt key is parameter[key],byte[] key=new String("123456").getBytes(); */
> public static byte[] encrypto(byte[] input,byte[] key) throws Exception{
>    SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,"DES");
>  
>    Cipher c1 = Cipher.getInstance("DES");
>    c1.init(Cipher.ENCRYPT_MODE,deskey);
>    byte[] cipherByte=c1.doFinal(input);
>    
>    return cipherByte;
> }
>         
>
>
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]