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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

encoding in encryption/decryption

843810Dec 6 2001 — edited Dec 9 2001
I am trying to encrypt a string that is passed to me and writing the encrypted string to a file. When I convert the byte array to a string encPasswd = new String (cipherText, "UTF8"); When I use the UTF8 encoding the converted string comes up null and nothing is written to the file. When I use ISO8859-1 then everything works fine the encrypted string gets written to the file and gets decrypted fine. Here is a part of my code

int count = 20; // Iteration count
pbeParamSpec = new PBEParameterSpec(salt, count); // Create PBE parameter set
System.out.println("parmspec for encryption " +pbeParamSpec);

String encryptKey = readRecord(br); // reads the key used for encryption from the file
System.out.println("The key to encrypt with is " + encryptKey);
pbeKeySpec = new PBEKeySpec(encryptKey.toCharArray());

keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
pbeKey = keyFac.generateSecret(pbeKeySpec);

Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Create PBE Cipher

pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Initialize PBE Cipher with key and parameters
byte[] cleartext = passwd.getBytes("UTF8"); // Our cleartext


System.out.println("cleartext " + cleartext);

cipherText = pbeCipher.doFinal(cleartext); // Encrypt the cleartext
encPasswd = new String (cipherText, "UTF8");
File outputFile = new File("e:\\\\JavaClasses\\Scheduler\\test.txt");
FileOutputStream out = new FileOutputStream(outputFile);
out.write(encPasswd.getBytes("UTF8"));
out.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2002
Added on Dec 6 2001
2 comments
289 views