Solved

Problem about the stream!!!

Posted on 2002-03-22
4
255 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
java example issue 5 46
Java syntax, or is it Selenium 6 43
Bitwise and to sum elements 2 17
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

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