I'm writing a small PBE program for a class project and I keep getting an InvalidKeySpecException that claims my char[ ] is not ASCII. Now I am under the impression that when a char value is entered it's in ASCII values... I've only put up a piece of my code, if needed I can put up the rest, it's fairly simple and easy to follow.
here is my terminal output (with a number of debug outputs):
Encryption Program Started
Getting Password
Input password (32 character max):
hello
Holdspot is: 6
Pass: hello
Holder: (shows string of char (0) values, verifies that the array was cleared)
Done getting password, pass is:
hello
Creating PBEKeySpec
My keySpec value is: hello
Creating Salt
createSalt entered
Done getting salt
Creating PBEParameterSpec
setParams entered
My iteration field is: 1500
Done getting param
My param iterations are: 1500
Creating SecretKey
createKey Entered
My keySpec value is: hello
InvalidKeySpecException: Password is not ASCII
java.security.spec.InvalidKeySpecException: Password is not ASCII
at com.sun.crypto.provider.PBEKey.<init>(DashoA6275)
at com.sun.crypto.provider.PBEKeyFactory.engineGenerateSecret(DashoA6275)
at javax.crypto.SecretKeyFactory.generateSecret(DashoA6275)
at Cryptography.createKey(Cryptography.java:141)
at Cryptography.main(Cryptography.java:265)
at __SHELL3.run(__SHELL3.java:7)
at bluej.runtime.ExecServer.vmSuspend(ExecServer.java:178)
at bluej.runtime.ExecServer.main(ExecServer.java:143)
java.security.spec.InvalidKeySpecException: Password is not ASCII
at com.sun.crypto.provider.PBEKey.<init>(DashoA6275)
at com.sun.crypto.provider.PBEKeyFactory.engineGenerateSecret(DashoA6275)
at javax.crypto.SecretKeyFactory.generateSecret(DashoA6275)
at Cryptography.createKey(Cryptography.java:141)
at Cryptography.main(Cryptography.java:265)
at __SHELL4.run(__SHELL4.java:7)
at bluej.runtime.ExecServer.vmSuspend(ExecServer.java:178)
at bluej.runtime.ExecServer.main(ExecServer.java:143)
here is my code to get the char array input:
private char[] getPass () throws IOException{
System.out.println("Input password (32 character max):");
char[] holder = new char[maxChar];
int holdspot=0;
while ((holder[holdspot++] = (char) System.in.read()) != '\n') {};
//Debug
System.out.println("Holdspot is: "+holdspot);
//End Debug
char[] pass = new char[holdspot];
System.arraycopy (holder, 0, pass, 0, holdspot);
//clear our holder array
for (int i=0; i<holder.length;i++){
holder=0;
}
//Debug
System.out.print("Pass: ");
for (int i=0; i<holdspot;i++){
System.out.print(pass[i]);
}
System.out.print("\n");
System.out.print("Holder: ");
for (int i=0; i<holder.length;i++){
System.out.print(holder[i]);
}
System.out.print("\n");
//End Debug
//Comment back in for final version, screen clear
//System.out.flush();
return (pass);
}
here is my code at the point where the exception is thrown:
private SecretKey createKey(PBEKeySpec keySpec)
throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException{
//Debug
System.out.println("createKey Entered");
char[] L = keySpec.getPassword();
System.out.print("My keySpec value is: ");
for (int i=0; i<L.length;i++){
System.out.print(L);
}
//End Debug
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey secKey = keyFac.generateSecret(keySpec);
return (secKey);
}