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
Solved

Problem about the stream!!!

Posted on 2002-03-22
4
254 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

789 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