Note that there are some explanatory texts on larger screens.

plurals
  1. POOutput AES/ECB/PKCS7 in c and java is not same
    text
    copied!<p>Hi i am using AES/ECB/PKCS7 mode for encryption, individually in c&amp; java, encryption &amp; decryption working fine. But in c encrypted data is not same as encrypted data in java... </p> <p>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 </p> <p>C code:</p> <pre class="lang-c prettyprint-override"><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(&amp;x); EVP_CIPHER_CTX_set_padding(&amp;x,1); // 1- padding, 0 - No Padding if (!EVP_EncryptInit_ex(&amp;x, EVP_aes_256_ecb(), 0, key, iv)) { //printf("\n ERROR!! \n"); return -1; } if (!EVP_EncryptUpdate(&amp;x, out, outlen,(const unsigned char*) in, inlen)) { //printf("\n ERROR!! \n"); return -2; } if (!EVP_EncryptFinal_ex(&amp;x,out + *outlen,&amp;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(&amp;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(&amp;x); EVP_CIPHER_CTX_set_padding(&amp;x,1); // 1- padding, 0 - No Padding if (!EVP_DecryptInit_ex(&amp;x, EVP_aes_256_ecb(), 0, key, iv)) { //printf("\n ERROR!! \n"); return -1; } if (!EVP_DecryptUpdate(&amp;x, out, outlen,(const unsigned char*) in, inlen)) { //printf("\n ERROR!! \n"); return -2; } if (!EVP_DecryptFinal_ex(&amp;x,out + *outlen,&amp;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(&amp;x); return 0; } </code></pre> <p>Java code:</p> <pre class="lang-java prettyprint-override"><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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload