Search code examples
javac++cryptographyjava-native-interfacegnupg

libgcrypt AES to return ascii ciphertext?


Hey, I m developing a JNI shared library which uses AES256 CFB mode to encrypt some data using libgcrypt and then passing the encrypted the data back into java app for further processing.

However, when I try to decrypt the data by passing it again to the shared library I can no longer get the original plain text back. It seems that if I encrypted and decrypted the same data without passing it back to the Java app I would get the original plain text back.

I was thinking that it was the conversion of the char buffer that I used to store the encrypted data to a NewStringUTF() (ie env->NewStringUTF(buf)) to send back to the java that was causing the issue so is there away to get/convert the output of the encrypted data to ascii or if there was some other work around?

Many thanks, Foo


Solution

  • Why are you using JNI and external libraries just to encrypt a string? Use Java's built-in javax.crypto.Cipher class.

    However, regarding your actual question, you should be passing the data as a byte array, not a string. Ciphers operate on bytes, not abstract Unicode codepoints.