Solved

Problem about the stream!!!

Posted on 2002-03-22
4
252 Views
Last Modified: 2010-03-31
hi, i faced some problem dealing with the below coding!!!
let take a look at that:(Questions ask after the coding part which stated at the bottom)
**********************************************************
  //---------------------------------------------------------
  // Reads user password from given input stream.
  //---------------------------------------------------------
   private char[] readPasswd(InputStream in) throws IOException {
     char[] lineBuffer;
     char[] buf;
     int i;

     buf = lineBuffer = new char[128];

     int room = buf.length;
     int offset = 0;
     int c;

     loop:   while (true) {
       switch (c = in.read()) {
       case -1:
       case '\n':
      break loop;

       case '\r':
         int c2 = in.read();
         if ((c2 != '\n') && (c2 != -1)) {
            if (!(in instanceof PushbackInputStream)) {
                in = new PushbackInputStream(in);
         }
         ((PushbackInputStream)in).unread(c2);
      } else
      break loop;

       default:
         if (--room < 0) {
        buf = new char[offset + 128];
           room = buf.length - offset - 1;
           System.arraycopy(lineBuffer, 0, buf, 0, offset);
           Arrays.fill(lineBuffer, ' ');
           lineBuffer = buf;
      }
      buf[offset++] = (char) c;
         break;
       }//switch
     }
     if (offset == 0) {
       return null;
     }
     char[] ret = new char[offset];
     System.arraycopy(buf, 0, ret, 0, offset);
     Arrays.fill(buf, ' ');
     return ret;
   }//read password
   
   //-------------------------------------------------
   // Password based encryption
   //-------------------------------------------------
   private void encrypt(){
     PBEKeySpec pbeKeySpec;
     PBEParameterSpec pbeParamSpec;
     SecretKeyFactory keyFac;

     // Salt
     byte[] salt = {
                    (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
                    (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
                };

     // Iteration count
     int count = 20;
     
     // Create PBE parameter set
     pbeParamSpec = new PBEParameterSpec(salt, count);

     // Prompt user for encryption password.
     // Collect user password as char array (using the
     // "readPasswd" method from above), and convert
     // it into a SecretKey object, using a PBE key
     // factory.
     try{
      System.out.println();
      System.out.print("Enter encryption password:  ");
      System.out.flush();
     
      pbeKeySpec = new PBEKeySpec(readPasswd(System.in));
     
     System.out.println("Encrypting the plaintext message ...");
     
     keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
     SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

     // Create PBE Cipher
     Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

     // Initialize PBE Cipher with key and parameters
     pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

     // Our cleartext
     //byte[] cleartext = "This is another example".getBytes();

     // Encrypt the cleartext
     cipherbuff = pbeCipher.doFinal(buff);
     }catch (Exception ex) {
          // Handle the error...
       System.out.println("Caught Exception: " + ex);
      }
     
   }//encrypt()
**********************************************************
***(program will the encrypt function first, then in the encrypt function, it will call the readPasswd function!)***
this program is running on dos mode, i am try to convert it to have GUI interface(passwordfield and ok button), the problems occured like:
1. pbeKeySpec = new PBEKeySpec(readPasswd(System.in));
   * i can't replace the system.in with other variables that i define because "readPasswd(inputstream in)" !!!

so, how i can i solve this problem, how to change it into GUI interface!!! Pls help me! only you can help me! Thank you very much!!!
Pls mail to the this e-mail add: thebest4tel@yahoo.com as well
0
Comment
Question by:paradise45
  • 4
4 Comments
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6888816
Assuming you gan get your GUI to pass a String, you can do this:

ByteArrayInputStream bis = new ByteArrayInputStream(strPassword.getBytes());

pbeKeySpec = new PBEKeySpec(readPasswd(bis));
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6888823
Or even better:

StringBufferInputStream is = new StringBufferInputStream(strPassword);

pbeKeySpec = new PBEKeySpec(readPasswd(is));
0
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 300 total points
ID: 6888834
A third solution is to change readPassword(inputstream) to readPassword(String):

public char[] readPassword (String strPassword) {
  return strPassword.toCharArray();
}
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6910501
Would you please explain why you graded my comment with a 'C'?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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 …

867 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

19 Experts available now in Live!

Get 1:1 Help Now