Here is my C++ code, If you see anything here that looks suspicious, please
let me know - I can't see it...
//=============================
//--------------------------------------------------------------------------
-
#include "TCSCryptography.h"
#include <stdlib.h>
char strKey[5] = {'A', 'D', 'D', 'I', '\0'};
int lenStrKey = 4;
//--------------------------------------------------------------------------
-
//Function: Encrypt
//parms:
// env - pointer to JNI class having all needed JNI methods
// obj - the calling class instance
// plainTextStr - the string to encrypt
//returns:
// jcharArray - because we CANT return a string - the resulting
encryption
// could insert embedded nulls - this causes all sorts of
// problems. The first 5 chars of the array
// are the length of the encrypted string (same as the
length of the
// original string)
//--------------------------------------------------------------------------
-
JNIEXPORT jbyteArray JNICALL Java_tcsLic_TCSCryptography_Encrypt(JNIEnv
*env, jclass obj, jstring plainTextStr)
{
jbyte buf[2000];
char tmpLenBuf[6];
/*Put the string to encrypt into a char * and get its length*/
const char *str = env->GetStringUTFChars(plainTextStr, 0);
int lenStr = env->GetStringUTFLength(plainTextStr);
/*Put length of string into first 5 chars of buffer*/
sprintf(tmpLenBuf, "%05d", lenStr);
buf[0] = tmpLenBuf[0];
buf[1] = tmpLenBuf[1];
buf[2] = tmpLenBuf[2];
buf[3] = tmpLenBuf[3];
buf[4] = tmpLenBuf[4];
/*Encrypt the string and fill in the buffer after the length*/
int i,j,iTmp;
for (i = 0, j = 5 ; i< lenStr; i++, j++)
{
/*Add each consecutive element of key to each consecutive element of
plaintext input data*/
iTmp = (int)str + (int)strKey[i%lenStrKey];
/*Insure that we are within the range of printable chars*/
iTmp %= 127;
buf[j] = (char)iTmp;
}
/************** DEBUG ************************/
printf("i is %d, j is %d, ENCRYPT --- key = %s\n, keylen = %d\n, param -
plainTextStr = %s\n, plainTextStrLen = %d\n, encstr = ", i,j, strKey,
lenStrKey, str, lenStr);
for (i = 0; i< lenStr + 5; i++)
{
printf("%c", buf);
}
printf("\n***************************\n");
/**********************************************/
/*, clean up, create a jbyteArray of buf size, fill it with the buffer and
return it*/
jbyteArray jBuf = env->NewByteArray(j);
env->SetByteArrayRegion(jBuf, 0, j, buf);
env->ReleaseStringUTFChars(plainTextStr, str);
return jBuf;
}
//--------------------------------------------------------------------------
-
//Function: Decrypt
//parms:
// env - pointer to JNI class having all needed JNI methods
// obj - the calling class instance
// encryptedCharAr - the char array to decrypt
//returns:
// jstring - because what we want from decryption is the original
string
//--------------------------------------------------------------------------
-
JNIEXPORT jstring JNICALL Java_tcsLic_TCSCryptography_Decrypt(JNIEnv *env,
jclass obj, jbyteArray encryptedCharAr)
{
char buf[2000];
jbyte* tmpEncryptedCharAr = env->GetByteArrayElements(encryptedCharAr, 0);
/*Get the length of the encrypted string from the first 5 chars*/
/* I could not get strncpy to work with tmpEncryptedCharAr*/
char lentmpEncryptedCharAr[6];
lentmpEncryptedCharAr[0] = tmpEncryptedCharAr[0];
lentmpEncryptedCharAr[1] = tmpEncryptedCharAr[1];
lentmpEncryptedCharAr[2] = tmpEncryptedCharAr[2];
lentmpEncryptedCharAr[3] = tmpEncryptedCharAr[3];
lentmpEncryptedCharAr[4] = tmpEncryptedCharAr[4];
lentmpEncryptedCharAr[5] = '\0';
int lenStr = atoi(lentmpEncryptedCharAr);
/*decrypt the string and fill the buffer*/
int i,j, iTmp;
for (i = 5, j = 0; j < lenStr; i++, j++)
{
/*Subtract each consecutive element of key from each consecutive element
of encrypted input data*/
iTmp = (int)tmpEncryptedCharAr - (int)strKey[j%lenStrKey];
/*Insure that we are within the range of printable chars*/
if (iTmp < 0)
iTmp+= 127;
buf[j] = (char)iTmp;
}
buf[j] = '\0';
/************** DEBUG ************************/
printf("DECRYPT --- key = %s\n, keylen = %d\n, param - encryptedCharAr =
", strKey, lenStrKey );
for (i = 0; i< lenStr + 5; i++)
{
printf("%c", tmpEncryptedCharAr);
}
printf("\n");
printf(" encryptedCharArLen = %d\n, decstr =
%s\n****************************\n",lenStr, buf);
/*********************************************/
/*Cleanup, and pass back decryted string*/
env->ReleaseByteArrayElements(encryptedCharAr, tmpEncryptedCharAr,0);
return env->NewStringUTF(buf);
}
//===========================