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!

Problem with PBEWithMD5AndDES on RedHat 9

843810Jun 16 2004 — edited Jul 14 2004
Hi,

I'm trying to encrypt a string then save it in the database, then retrieve the string and decrypt it.
I have tested this on JDK1.3 (Sun and IBM) yet it works on Windows XP Pro and Debian, but as soon as I try it on RedHat 9 it fails.

Where it all goes wrong is that when I call encrypt I get back an empty string, whereas I should get an encoded string. I suspect that it has to do with the following error which I can only recreate if I try writing it to a file and reading it back again, although there is absolutely NO exceptions thrown if I run the source below! (I have checked everything from system's default encoding and permissions in the security file)
sun.io.MalformedInputException
at sun.io.ByteToCharUTF8.convert(ByteToCharUTF8.java(Compiled Code))
at java.io.InputStreamReader.convertInto(InputStreamReader.java:144)
at java.io.InputStreamReader.fill(InputStreamReader.java:193)
at java.io.InputStreamReader.read(InputStreamReader.java:256)
at java.io.BufferedReader.fill(BufferedReader.java:145)
at java.io.BufferedReader.read(BufferedReader.java:163)
...

I can successfully encode a string and immediately decode it and it works, but I cannot save the encrypted string!

Any ideas?

I'm using Sun's jce1_2_2.jar (as a jre ext setup), and their sunjce_provider.jar.
JDK version:
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Classic VM (build 1.3.0, J2RE 1.3.0 IBM build cx130-20010925 (JIT enabled: jitc))

package sap.cryptography;

import javax.crypto.*;
import javax.crypto.interfaces.*;
import javax.crypto.spec.*;
import java.io.*;

public class CryptoTest {

private PBEKeySpec pbeKeySpec;
private PBEParameterSpec pbeParameterSpec;
private SecretKeyFactory keyFac;
private Cipher cipher;
private SecretKey pbeKey;

private char[] alphabet;

private byte[] salt = { (byte) 0x73, (byte) 0x99, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0xc7, (byte) 0x21, (byte) 0x8c };
private int count = 20;
private char[] key = {'S', 'o', 'm', 'e', 'k', 'i', 'n', 'd', 'O', 'f', 'K', 'e', 'y'};

public CryptoTest() {
String al = new String("sdsadwqerwqw3243fde32543rfdasasdasd23434WFSDFERT512edqdqw421");

this.alphabet = al.toCharArray();
java.security.Provider sunJce = new com.sun.crypto.provider.SunJCE();
java.security.Security.addProvider(sunJce);
try {

this.pbeParameterSpec = new PBEParameterSpec(salt, count);
this.pbeKeySpec = new PBEKeySpec(this.key);
this.keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
this.pbeKey = this.keyFac.generateSecret(pbeKeySpec);
this.cipher = Cipher.getInstance("PBEWithMD5AndDES");

}
catch(Exception e) {
e.printStackTrace();
}
}

public String convertToALPH(String text) {
return java.net.URLEncoder.encode(text);
}

private byte getByteEnc(char a) {
for(byte i = 0; i < this.alphabet.length; i++)
if(a == this.alphabet) {
return i;
}
return 0;
}

public String convertToString(String text) {
return java.net.URLDecoder.decode(text);
}

public String encrypt(String clearText) {
try {
this.cipher.init(Cipher.ENCRYPT_MODE, this.pbeKey, this.pbeParameterSpec);
byte[] cipherText = this.cipher.doFinal(clearText.getBytes());
return new String(cipherText);
}
catch(Exception e) {
System.out.println("[CryptoTest:enrypt] Exception encrypting string(" + clearText + "): " + e.getMessage());
e.printStackTrace();
}
return null;
}

public String decrypt(String eText) {
byte[] encryptedText = eText.getBytes();
try {
this.cipher.init(Cipher.DECRYPT_MODE, this.pbeKey, this.pbeParameterSpec);
byte[] clearText = this.cipher.doFinal(encryptedText);
String ret = new String(clearText);
return ret;
}
catch(Exception e) {
System.out.println("[CryptoTest:decrypt] Exception decoding string(" + eText + "): " + e.getMessage());
e.printStackTrace();
}
return null;
}


public static void main(String[] args) {
if(args==null)
System.out.println("Usage:\tjava CryptoTest somestringtoencrypt\nOR\tjava CryptoTest somestringtodecrypt 1");

boolean decrypt = false;

CryptoTest c = new CryptoTest();

if(args.length > 1) {
String deconverted = c.convertToString(args[0]);
System.out.println("Deconverted: " + deconverted);
String decrypted = c.decrypt(deconverted);
System.out.println("Decrypted: " + decrypted);
}
else {
String encrypted = c.encrypt(args[0]);
System.out.println("Encrypted: " + encrypted);
String converted = c.convertToALPH(encrypted);
System.out.println("Converted: " + converted);
}
}
}

Many thanks,
Andy//
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2004
Added on Jun 16 2004
6 comments
417 views