Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Weird problem with the Rijndael algorithm

843810Jan 14 2002 — edited Jan 15 2002
I have a really weird problem. I am creating a simple class to test the Rijndael
algorithm. I'm running it on a Win 2000 Advanced Server. I'm using the JCE
provider available at Cryptix (http://www.cryptix.org/products/jce/ to be exact).

So what is my wierd problem?
After encryption and decryption the first 16characters of the string are corrupted.
I have gone through everything i can think of but I cant find what the problem is.

PLEASE HELP me, I'm all out of ideas what might cause it.
--------------------------------------------------------------------------------------
C:\JavaCode\AES>java AES -d
Org : 1st16characters 2nd16characters 3rd16characters.
Enc : T�- c?otV|�+�++?`�=+O��-+�-:��+�^](]?�K+7e��?+?tqvc+H��:�6t?b=��

Ret : �+'+?�cS�?nO.A�+2nd16characters 3rd16characters.
Dec : �+'+?�cS�?nO.A�+2nd16characters 3rd16characters.

Somethings wrong
--------------------------------------------------------------------------------------

Results from running the code below

--------------------------------------------------------------------------------------
import java.io.*;
import java.util.Properties;
import javax.crypto.*;
import java.security.*;
import java.security.interfaces.*;
import javax.crypto.*;

public class AES {

String cryptstring = "Rijndael/CBC/PKCS#7";

public int KeyLength =256;
Cipher cipherEncrypt = null;
Cipher cipherDecrypt = null;
SecretKey key=null;

private AES(String message) {

Security.addProvider(new cryptix.jce.provider.CryptixCrypto());

try {
cipherEncrypt = Cipher.getInstance(cryptstring);
cipherDecrypt = Cipher.getInstance(cryptstring);
}
catch(java.lang.Exception x) {
System.out.println("Exception thrown "+x);
}

boolean b =KeyGenerate();
if(b){
System.out.println("Org : "+message);
byte[] enc=Encrypt(message);
System.out.println("Enc : "+ new String(enc)+"\n");
String dec=Decrypt(enc);
System.out.println("Dec : "+dec+"\n");

if (dec.equals(message)) {
System.out.println("It's OK");
}else {
System.out.println("Somethings wrong");
}
}
}

public boolean KeyGenerate() {
KeyGenerator kg = null;
SecretKey k = null;

try{
kg = KeyGenerator.getInstance("Rijndael");
kg.init(KeyLength);
k = kg.generateKey();
}
catch(java.lang.Exception x) {
System.out.println("Exception thrown "+x);
return false;
}
key=k;
return true;
}

public synchronized byte[] Encrypt(String message){

byte[] messageBytes = null;
byte[] encryptedByte = null;

try{
messageBytes = message.getBytes("ISO8859-1");
cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
encryptedByte = cipherEncrypt.doFinal(messageBytes);
}
catch(java.lang.Exception x) {
System.out.println("Exception thrown "+x);
return null;
}
return encryptedByte;
}

public String Decrypt(byte[] message){

byte[] decryptedMessage = null;
String decryptedMessageString = "";

try{
cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
decryptedMessage = cipherDecrypt.doFinal(message);

// And here something has gone wrong
decryptedMessageString = new String(decryptedMessage,"ISO8859-1");
System.out.println("Ret : "+decryptedMessageString );
return decryptedMessageString;
}
catch(java.lang.Exception x) {
System.out.println("Exception thrown "+x);
return "";
}
}


public static void main (String args[]) {
// Register the provider.
Security.addProvider(new cryptix.jce.provider.CryptixCrypto());

if ( args.length == 1 && args[0].equals("-d")) {
AES program =new AES("1st16characters 2nd16characters 3rd16characters.");
}else {
if (args.length >= 2 && args[0].equals("-t")) {
AES program =new AES(args[1]);
}else {
System.out.println("Incorrect arguments.\n");
System.out.println("Usage: \n java AES -d ");
System.out.println(" java AES -t \"Any text you want to encode\"");
}
}
}

}

--------------------------------------------------------------------------------------
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 12 2002
Added on Jan 14 2002
3 comments
428 views