Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Output for Triple DES encription in JAVA is not matching with Open SSL.

843811Feb 11 2008 — edited Jun 9 2008
Hi All,

We have written two different program to encrypt/decrypt a string in Java & C++(Open SSL)
.
Both the programs are same and using the same Initialization Vector & Key.
Strange to see but both the output are not matching. Thought when you decrypt then back in there respective environment, they give back the original string. So both the programs are working fine in there own respective environment.The query is why is this indifference?

The only difference that we can see is the padding option.
In Java we are using the PKCS5Padding and in Open SSL its MD5.
Just wanted to know is there anything common between them, something like "no padding" or none.

We are using the Sun JCE provider for Java and Open SSL libraries for encryption and decryption in C++.
For your reference code in Java is also attached:
Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] initVector = {(byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,(byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
       
        iv = new IvParameterSpec(initVector);
    
        SecretKeySpec key = new SecretKeySpec("&%34g}0@@dh-m(Oh#RJ0^2QW".getBytes(),"DESede");
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        
        System.out.println("IV:"+(new String(Base64.encodeBase64(cipher.getIV()))));

        // Our cleartext
        byte[] cleartext = "oblix".getBytes();
    
        // Encrypt the cleartext
        byte[] ciphertext = cipher.doFinal(cleartext);
        
        String strEncoded = new String(Base64.encodeBase64(ciphertext));
        System.out.println("Encrypted data: " + strEncoded);
        
        byte[] original = Base64.decodeBase64(strEncoded.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, key, iv);

        System.out.println("Decrypted data: " + new String(cipher.doFinal(original)));
Open SSL CODE_
int tdesEncrypt(unsigned char *in, unsigned char *output, unsigned int *outlen)
{
        if (!in || !output)
                return -1;

        unsigned char initVector[8] = {0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};        //The initialization vector will be derived from this
        unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH];

        int outbuflen=0;

        EVP_CIPHER_CTX ctx;

        unsigned char salt[8] = {0x53,0x41,0x4C,0x54,0x73,0x61,0x6C,0x74};
        //unsigned char keyInput[24] = {0x3F,0x6F,0x6B,0x69,0x20,0x5E,0x5F,0x45,0x65,0x54,0x5D,0x56,0x63,0x68,0x6E,0x6F,0x14,0x32,0x2C,0x41,0x3F,0xD3,0x9B,0xA3};
        unsigned char keyInput[24] = {0x26,0x25,0x33,0x34,0x67,0x7D,0x30,0x40,0x40,0x64,0x68,0x2D,0x6D,0x28,0x4F,0x68,0x23,0x52,0x4A,0x30,0x5E,0x32,0x51,0x57};

        memcpy(iv,initVector,sizeof(iv));

        //EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_md5(), salt, keyInput, sizeof(keyInput), 1, key, iv);
        EVP_BytesToKey(EVP_des_ede3_cbc(), EVP_md5(), NULL, keyInput, sizeof(keyInput), 1, key, iv);


        EVP_CIPHER_CTX_init(&ctx);
        EVP_CipherInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, key, iv, ENCRYPT);

        EVP_CipherUpdate(&ctx, output, &outbuflen, in, strlen((char *)in));

        *outlen = outbuflen;

        EVP_CipherFinal_ex(&ctx, &output[outbuflen], &outbuflen);

        *outlen += outbuflen;
        EVP_CIPHER_CTX_cleanup(&ctx);

        return 0;
}
An Help on above post will be a great help for us.

Thanking you in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 7 2008
Added on Feb 11 2008
4 comments
454 views