Pergunta

Hi i am using AES/ECB/PKCS7 mode for encryption, individually in c& java, encryption & decryption working fine. But in c encrypted data is not same as encrypted data in java...

I am posting my code in C language and java. I need Encryption in c and Decryption in java. Please assist me c code for aes/ecb/pkcs7 is correct ??.. I am using openssl

C code:

int Secure_encrypt(unsigned char *in, int inlen, unsigned char *out,int *outlen)
{
    int tmplen;
    // Key is 256 and is fixed.
    unsigned char key[256] =
    {   0x21,0x0a,0x03,0x23,0x45,0x29,0x78,0x12,0x35,0x45,0x67,0x78,0x21,0x13,
        0x34,0x56,0x67,0x45,0x12,0x89,0x38,0x0e,0xa0,0x15,0x21,
        0x0a,0x03,0x23,0x45,0x0b,0x15,0x0c
    };
    unsigned char *iv=0;
    EVP_CIPHER_CTX x;
    EVP_CIPHER_CTX_init(&x);
    EVP_CIPHER_CTX_set_padding(&x,1); // 1- padding, 0 - No Padding
    if (!EVP_EncryptInit_ex(&x, EVP_aes_256_ecb(), 0, key, iv))
    {
        //printf("\n ERROR!! \n");
        return -1;
    }
    if (!EVP_EncryptUpdate(&x, out, outlen,(const unsigned char*) in, inlen))
    {
        //printf("\n ERROR!! \n");
        return -2;
    }
    if (!EVP_EncryptFinal_ex(&x,out + *outlen,&tmplen)) {
        //printf("\n ERROR!! \n");
        return -3;
    }
    *outlen += tmplen;
#ifdef DEBUG
    printf ("AES encrypted data %d len\n", *outlen);
    print_data (out, *outlen);
#endif
    EVP_CIPHER_CTX_cleanup(&x);
    return 0;

}



/*AES DECRYPTION */
AES Decryption



int Secure_decrypt(unsigned char *in, int inlen, unsigned char *out,int *outlen)
{
    int tmplen;
    unsigned char *iv=0;
    unsigned char key[256]
    //AES/ECB/PKCS7 Padding
    EVP_CIPHER_CTX x;
    EVP_CIPHER_CTX_init(&x);
    EVP_CIPHER_CTX_set_padding(&x,1); // 1- padding, 0 - No Padding
    if (!EVP_DecryptInit_ex(&x, EVP_aes_256_ecb(), 0, key, iv)) {
        //printf("\n ERROR!! \n");
        return -1;
    }
    if (!EVP_DecryptUpdate(&x, out, outlen,(const unsigned char*) in, inlen))
    {
        //printf("\n ERROR!! \n");
        return -2;
    }
    if (!EVP_DecryptFinal_ex(&x,out + *outlen,&tmplen)) {
        //printf("\n ERROR!! \n");
        return -3;
    }
    *outlen += tmplen;

#ifdef DEBUG
    printf ("AES encrypted data %d len \n", *outlen);
    print_data (out, *outlen);
#endif
    EVP_CIPHER_CTX_cleanup(&x);
    return 0;
}

Java code:

import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class doThis {

    public static void main(String[] args) {
        Security.addProvider(new BouncyCastleProvider());
        String strDataToEncrypt = "Testing Encryption";
        byte[] byteDataToTransmit = strDataToEncrypt.getBytes();
        //41 6E 6B 61 72 61 6F 20 49 74 74 61 64 69
        //byte[] byteDataToTransmit = new byte []
        {
            0x41,0x6E,0x6B,0x61,0x72,0x61,0x6F,0x20,0x49,0x74,0x74,0x61,0x64,0x69
        };
        try {

            byte [] keyBytes= new byte [] {0x21,0x0a,0x03,0x23,0x45,0x29,0x78,0x12,0x35,
                                           0x45,0x67,0x78,0x21,0x13,0x34,

                                           0x56,0x67,0x45,0x12,0x9,0x38,0x0e,0x20,
                                           0x15,0x21,0x0a,0x03,0x23,0x45,0x0b,0x15,0x0c
                                          };

            byte[] encrypted= aesEncrypt(byteDataToTransmit,keyBytes);

            System.out.println("\n AES Encrypted Data is  "+new String (encrypted));

            byte [] byteDecrypt=aesDecrypt(bytestrEncrypt, keyBytes);
            System.out.println("\n AES Decrypted Data is"+byteDecrypt);
            // byte [] byteDecrypt=aesDecrypt(encrypted , keyBytes);

            //System.out.println("\n AES Decrypted Data is"+new String(byteDecrypt));
        }
        catch(Exception exp)
        {
            System.out.println(" Exception caught " + exp);
            exp.printStackTrace();
        }
    }

    public static byte[] aesEncrypt(byte[] original, byte[] key)
    {
        try
        {
            SecretKeySpec keySpec = null;
            Cipher cipher = null;
            {
                keySpec = new SecretKeySpec(key, "AES/ECB/PKCS7Padding");
                cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
                cipher.init(Cipher.ENCRYPT_MODE, keySpec); // encryption
            }
            return cipher.doFinal(original);
        }
        catch(Exception e)
        {
            //  Logger.e(e.toString());
        }
        return null;
    }

    public static byte[] aesDecrypt(byte[] encrypted, byte[] key)
    {
        try
        {
            SecretKeySpec keySpec = null;
            Cipher cipher = null;

            {
                keySpec = new SecretKeySpec(key, "AES/ECB/PKCS7Padding");

                cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
                cipher.init(Cipher.DECRYPT_MODE, keySpec);
            }

            System.out.println("In Decryprion \n"+ new String (encrypted));
            return cipher.doFinal(encrypted);

        }
        catch(Exception e)
        {
            //  Logger.e(e.toString());
        }
        return null;
    }
}
Foi útil?

Solução

At least your handling of the returned amount of data is off. Check your buffer handling! Furthermore, in Java you use new String (encrypted) instead of converting the byte array to hexadecimals.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top