c和java中的输出AES / ECB / PKCS7不相同

时间:2013-12-02 05:11:58

标签: c encryption openssl aes pkcs#7

您好我正在使用AES / ECB / PKCS7模式进行加密,单独使用c& java,加密&解密工作正常。但是在c中加密的数据与java中的加密数据不同......

我在C语言和java中发布我的代码。我需要c中的加密和java中的解密。请帮助我c代码为aes / ecb / pkcs7是正确的??我正在使用openssl

C代码:

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代码:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

至少您对返回的数据量的处理已关闭。检查缓冲区处理!此外,在Java中,您使用new String (encrypted)而不是将字节数组转换为十六进制。