Solved

debug:password is not ASCII

Posted on 2000-03-30
2
1,713 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 25 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now