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[i] + (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[i]);
}
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[i] - (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[i]);
}
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);
}
//===========================
"Gordon Beaton" <> wrote in message
news:412a280f$...
> On Mon, 23 Aug 2004 10:12:40 -0800, John McClain wrote:
> > *my error environment is a jsp webapp running on tomcat5.0, through
> > IE5.0
> >
> > *I have created a dll with 2 functions in Borland C++builder 6.0
> > *I have a java class where I call system.loadLibrary("<dllname>") and
> > declare the functions native
> > *I have a main in my class where I test the invocation and return values
> > from the dll functions
> > * THE MAIN IS COMPLETELY SUCCESSFULL - JNI WORKS!!
> > * however - when I call the functions from a servlet that is invoked
from a
> > browser, I get an EXCEPTION_FLT_STACK_CHECK error
>
> The fact that you get the expected results in a different situation is
> no guarantee that the code is correct, and my suspicion is that you
> have a pointer error of some kind in your native code (in fact it is
> typical of pointer errors that the code appears to work correctly in
> some situations but crashes in others). Without actually seeing the
> code though, I can't be more specific.
>
> Do you get the error if you call *any* methods in the native
> library, or just one specfic method?
>
> > *So - it seems that just loading the dll causes a system error BUT
> > only if we are running through a servlet. Is there some windows
> > library conflict???
>
> I have no idea. Make a trivial ("hello world") DLL and see if that
> gives you any problems when you use it from a servlet.
>
> /gordon
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e