Solved

Problem about the stream!!!

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 48
Configure a Bean in an XML file 4 31
How do I remove an object from a 3 23
sql import cannot be resolved jsp 3 18
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

785 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