AES_encrypt / AES_decrypt returns part of the message

I donโ€™t know why the following code will return "Hello native! Th" and not "Hello native! This is from jni load! \ N", can someone tell me?

#include "helloJNI.h"
#include "openssl/aes.h"

#define LEN 1024

jstring jni_text(JNIEnv *env, jclass clz)
{
    AES_KEY aesKey;
    int result;
    const char origin[] = "Hello native! This is from jni load!\n";
    char out[LEN];
    char outout[LEN];
    memset(out, '\0', sizeof(out));
    memset(outout, '\0', sizeof(outout));
    result = AES_set_encrypt_key((const unsigned char *)"abc123", 256, &aesKey);
    LOGE("encypt key result %d\n", result); /* is 0 */
    AES_encrypt((const unsigned char *)origin, (unsigned char *)out, &aesKey);
    LOGE("after encrypt, chars is %s\n", out);
    result = AES_set_decrypt_key((const unsigned char *)"abc123", 256, &aesKey);
    LOGE("decrypt key result %d\n", result); /* is 0 */
    AES_decrypt(out, outout, &aesKey);
    LOGE("after decrypt, chars is %s\n", outout);
    return (*env)->NewStringUTF(env, outout); /* return "Hello native! Th" */
}
+4
source share
2 answers

AES_encrypt((const unsigned char *)origin, (unsigned char *)out, &aesKey);

AES_encryptworks with 16 byte blocks. 16 - AES block size.

Effectively, you have truncated your message.

AES_decrypt(pout, outout, &aesKey);

Here you only decrypted 16 bytes. The rest of the buffer was filled with 0. 0 served as an ASCII-Z terminator.


ECB. ECB, , . ECB , . , .

, , . , ECB .


, CBC. AES_encrypt AES_decrypt EVP_*. . EVP Symmetric Encryption and Decryption OpenSSL.

, . , , . , EAX, CCM GCM. . EVP Authenticated Encryption and Decryption.

+4

AES_encrypt AES... 16 . , 16 .

+2

Source: https://habr.com/ru/post/1584390/