apt-get install libssl0.9.7
apt-get install libssl-dev
Following sections of shows the code fragments that we can use to encrypt/decrypt messages (bytes) both in Java and C++. The algorithm used for the encryption is AES (http://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
In java the encryption is handled by the provided javax.crypto.Cipher class. The following code fragment shows the encryption in Java.
byte[] bytesToEncrypt = /*Bytes to be encrypted*/
byte[] encBytes = null; /*Encrypted Bytes*/
/**
* Create a Cipher by specifying the following parameters a. Algorithm
* name - here it is AES */
try {
aesCipher = Cipher.getInstance(Constants.AES_ALGO);
encBytes = aesCipher.doFinal(msg.getBytes());
} catch (Exception e) {
throw new ClarensException(
"Error encrypt message using secret key",e);
}
These bytes are then transferred to the C++ client using socket based communication channel.
Openssl provides a set of libraries for handling the decryption and the following utility function shows how we can use those to decrypte the received set of bytes.
bool
SecurityUtil::decryptAES(const unsigned char *in,int inputLength ,unsigned char *out,int &outputLength, string aesKey){
int olen, tlen, n;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (& ctx);
EVP_DecryptInit (& ctx, EVP_aes_128_ecb (), (unsigned char *)aesKey.c_str(), NULL);
olen=0; tlen=0;
if (EVP_DecryptUpdate (& ctx, out, & olen, (const unsigned char*)in,inputLength) != 1)
{
cerr<<"error in decrypt update"<
}
if (EVP_DecryptFinal(& ctx, out + olen, & tlen) != 1)
{
cerr<<"error in decrypt final"<
}
olen += tlen;
outputLength=olen;
EVP_CIPHER_CTX_cleanup (& ctx);
return true;
}
Encryption in C++
bool
SecurityUtil::encryptAES(const unsigned char* in,int
inputLength ,unsigned char *out,int &outputLength, string
aesKey){
int olen, tlen, n;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (& ctx);
EVP_EncryptInit (& ctx, EVP_aes_128_ecb (), (unsigned char
*)aesKey.c_str(), NULL);
if (EVP_EncryptUpdate (& ctx, out, & olen, (const unsigned
char*)in , inputLength) != 1)
{
cerr<<"error in decrypt update"<
if (EVP_EncryptFinal (& ctx, out + olen, &amp;amp;amp;amp;amp; tlen) != 1)
{
cerr<<"error in encrypt final"<
olen+=tlen;
outputLength=olen;
EVP_CIPHER_CTX_cleanup (& ctx);
return true;
}
Decrypting the bytes received from the C++ client in JAVA
byte[] decBytes = null;
Cipher aesCipher; try {
aesCipher = Cipher.getInstance(Constants.RSA_ALGO);
decBytes = aesCipher.doFinal(msgBytes);
} catch (Exception e) {
throw new ClarensException(
"Error decrypting message using private key ", e);
}
Simple right? The main problem I faced when developing the above application was the lack of documentation on this regard. There are tons of documentation on how to handle encryption/decryption using java but very small number for the same in C++. How about encryption/decryption between Java and C++? I could not find anything in this sort. Openssl has a good documentation on various functions/data structures it offers for encryption/decryption but the main problem is there very limited amount of code examples which shows the exact usage. Followings are some of the resources that I used to come up with this implementation and hope someone will find this helpful.
http://www.openssl.org/docs/
http://www.madboa.com/geek/openssl/#cert-self
http://www.ibm.com/developerworks/linux/library/l-openssl.html
http://www.mail-archive.com/openssl-users@openssl.org/msg40449.html
http://www.mail-archive.com/openssl-users@openssl.org/msg23119.html
http://www.fortrel.net/blog/index.php?title=encryption_java_c&more=1&c=1&tb=1&pb=1
http://www.adp-gmbh.ch/cpp/common/base64.html
Next Blog: Signing and Verifying between Java and C++