If there's anyone who knows a thing or two about this, your help would be much appreciated. This is a code I'm working on:
try {
//ask user for short password
System.out.print("Enter a password: ");
//read in a character at a time, putting them into the buffer
while((buf[bufPos++] = (char)System.in.read()) != '\n') {}
//convert buffer into password char array
//remove the trailing \n character
char[] password = new char[--bufPos];
System.arraycopy(buf, 0, password, 0, bufPos);
//create a PBE Key specification
//provide the password, salt and iteration count
PBEKeySpec pbeKeySpec = new PBEKeySpec(password,salt,iterations);
//locate PBE secret key factory
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
//generate the secret key from the PBEKeySpec
SecretKey key = factory.generateSecret(pbeKeySpec);
//locate a PBE cipher
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
//initialise the cipher for encryption and give it the key it should use
cipher.init(Cipher.ENCRYPT_MODE, key);
//encrypt our command line sentence
byte[] cipherText = cipher.doFinal(args[0].getBytes());
//send encrypted data to target, e.g file
FileOutputStream passText = new FileOutputStream(new File("password.txt"));
passText.write(cipherText);
//clear out all password references from memory
for(int i = 0; i < password.length; i++) {
buf[i] = password[i] = 0;
}
} catch (etc...)
I keept getting this message: java.security.spec.InvalidKeySpecException: Password is not ASCII. It occurs at this line of code:
SecretKey key = factory.generateSecret(pbeKeySpec);
I honestly don't know what else to do. I've looked at everything, and nothing seems wrong.
Please, help