Solved

debug:password is not ASCII

Posted on 2000-03-30
2
1,627 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
Comment Utility
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
Comment Utility
thank you very much!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
network + 7 73
wordcount challenge 11 72
Requested array size exceeds VM limit 3 44
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

12 Experts available now in Live!

Get 1:1 Help Now