encoding in encryption/decryption
843810Dec 6 2001 — edited Dec 9 2001I 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();