Solved

Problem about the stream!!!

Posted on 2002-03-22
4
251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Would you please explain why you graded my comment with a 'C'?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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 …

762 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

10 Experts available now in Live!

Get 1:1 Help Now