Solved

debug:password is not ASCII

Posted on 2000-03-30
2
1,799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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