?
Solved

debug:password is not ASCII

Posted on 2000-03-30
2
Medium Priority
?
2,067 Views
Last Modified: 2008-02-01
Dear,

   Do anyone know what's wrong with my following program?

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

public class PBE{
      public static void main(String[] args) throws Exception{
            String options=args[0];
            String passphrase=args[1];
            File inputFile = new File(args[2]);
            File outputFile=new File(args[3]);
            char[] dst=new char[10];
            String algorithm="PBEWithMD5AndDES";
            
            byte[] salt=new byte[8];
            int iterations=20;
            
            boolean encrypting=(options.indexOf("e")!=-1);
            
            //Create a key from the suppliede passphrase.
            passphrase.getChars(0,passphrase.length(),dst,0);
            System.out.println(dst);
            KeySpec ks=new PBEKeySpec(dst);
            
            SecretKeyFactory skf=SecretKeyFactory.getInstance(algorithm);
            SecretKey key=skf.generateSecret(ks);
            
            //Read the input
            FileInputStream in = new FileInputStream(inputFile);
            int length=(int)inputFile.length();
            if (!encrypting) in.read(salt);
            byte[] input=new byte[length-(encrypting?0:8)];
            in.read(input);
            in.close();
            
            if (encrypting){
                  //create the salt from eight bytes of the digest of P || M
                  MessageDigest md=MessageDigest.getInstance("MD5");
                  md.update(passphrase.getBytes());
                  md.update(input);
                  byte[] digest=md.digest();
                  System.arraycopy(digest,0,salt,0,8);
            }
            
            //create the algorithm parameters
            AlgorithmParameterSpec aps=new PBEParameterSpec(salt,iterations);
            
            //encrypt or decrypt the input
            Cipher cipher=Cipher.getInstance(algorithm);
            int mode=encrypting ? Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE;
            cipher.init(mode,key,aps);
            byte[] output=cipher.doFinal(input);
            
            //write the output
            OutputStream out=new FileOutputStream(outputFile);
            if (encrypting) out.write(salt);
            out.write(output);
            out.close();
      }
}
      
Error:java.security.spec.InvalidKeySpecException:Password is not ascii

Thank you very much!(you will need JCE to compile this program.You can email to me hpchong7@ie.cuhk.edu.hk for asking the JCE API)
0
Comment
Question by:hpchong7
2 Comments
 
LVL 1

Accepted Solution

by:
stoneman earned 100 total points
ID: 2672040
Could be a couple of things:

are you sure you're getting a valid argument into main?

The allocation of the dst variable is not very smart. If the passcode is > than 10 character then your line

passphrase.getChars(0,passphrase.length(),dst,0);

would fail with an array index out of bounds. You should allocate the destination character array like this:

char[] dst=new char[args[1].length()];

The exception is thrown because the implementation of PBEKeySpec is not returning a valid KeySpec. Where does PBEKeySpec come from? Is it possible that it is expecting a non-unicode string as the seed?
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2752081
thank you very much!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question