Solved

debug:password is not ASCII

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 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