Not able to compile Java Service

Hello All,
I wrote a java service which encrypts based on the tripleDES algorithm.

I imported the following in the shared tab

javax.crypto.Cipher;
javax.crypto.BadPaddingException;
javax.crypto.IllegalBlockSizeException;
javax.crypto.KeyGenerator;
java.security.Key;
java.security.InvalidKeyException;

I included the following property in the extended field

watt.server.compile=javac -O -classpath {0} -d {1} {2}

when i try to compile the java service i am getting the following error

Java Service Error
D:\wm61\IntegrationServer\packages\Test\code\source\Test.java:63: unreported exception java.security.NoSuchAlgorithmException; must be caught or declared to be thrown
key = KeyGenerator.getInstance(algorithm).generateKey();
^
D:\wm61\IntegrationServer\packages\Test\code\source\Test.java:64: unreported exception java.security.NoSuchAlgorithmException; must be caught or declared to be thrown
cipher = Cipher.getInstance(algorithm);
^
D:\wm61\IntegrationServer\packages\Test\code\source\Test.java:67: unreported exception java.security.InvalidKeyException; must be caught or declared to be thrown
cipher.init(Cipher.ENCRYPT_MODE, key);
^
D:\wm61\IntegrationServer\packages\Test\code\source\Test.java:69: unreported exception javax.crypto.IllegalBlockSizeException; must be caught or declared to be thrown
byte recoveredBytes = cipher.doFinal(inputBytes);
^
Note: D:\wm61\IntegrationServer\packages\Test\code\source\Test.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
4 errors
could someone please help how to compile this code sucessfully.
Please find attached the java service code

Thanks in Advance
yarkar
EncryptionCode.txt (707 Bytes)

Wrap your code in a try-catch block.

try {
    key = KeyGenerator.getInstance(algorithm).generateKey();
    cipher = Cipher.getInstance(algorithm);

    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = inputString.getBytes();
    byte[] recoveredBytes = cipher.doFinal(inputBytes);
    encryptedData = new String(recoveredBytes);
} catch (Exception ex) {
    throw new ServiceException("Error:  something bad happened - " + ex);
}

You might need to go through some basic java training and purchase a book or two. A couple of books I’d recommend given your example is “Beginning Cryptography with Java” by David Hook. or “Java Security” by Scott Oakes.

Mark

Mark,
Than You for the reply.

I included the latest code with errorHandling but when i compile the code i get the nullPointer exception.Could you let me know how to fix this problem.

Please find attached the latest code.

Thanks in Advance,
Yarkar
EncryptionCode.txt (785 Bytes)

Sorry. I am unable to help you debug your code. If you are just learning java, you might want to start with something easier than encryption.

Mark

Hi Yarkar,

Can I have the inputString you are supplying for this service? The probable reason could be that your inputString does not meet the blockSize length. The default blockSize length is 8 bytes. And so your inputString should have its length in multiple of 8 bytes. In case it is not in multiple of 8 Bytes you may need to append with the required Bytes . Try running the service with inputString as multiples of 8 Bytes an hope this would help you to try for alternate solutions like as mentioned to append with required number of Bytes.

  • Dinakar.