Results 1 to 10 of 10
- 01-29-2012, 09:59 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
How to generate and use variables in different switch statements
I'm trying to encrypt a text with DES algorithm using Bouncy Castle library. I have written the following code:
The code is supposed to work as follows: The following part of the code will generate "keySpec" and "Cipher" which is used in both encryption and decryption parts in the switch statement.Java Code:try{ KeyGenerator kg=KeyGenerator.getInstance("DES"); SecretKey key=kg.generateKey(); SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES"); Cipher cipher=Cipher.getInstance("DES"); }catch(Exception e) { } switch(selection){ case enc: try{ cipher.init(Cipher.ENCRYPT_MODE, keySpec); String inText=inputText.toString(); byte[] cipherText=cipher.doFinal(inText.getBytes()); encText.setText(new String(cipherText)); }catch(Exception e) { encText.setText("Exception occured!"); } break; case dec: try{ cipher.init(Cipher.DECRYPT_MODE, keySpec); String encT=encText.toString(); byte[] decBytes=cipher.doFinal(encT.getBytes()); decText.setText(new String(decBytes)); }catch(Exception e) { decText.setText("Exception occured!"); } break; } }
Then depending on the user selection, inputText is encrypted or encText is decrypted in the switch statement. Both encryption and decryption blocks share the same "keySpec" and "Cipher" variables generated in the above code snippet. However, the compiler says that keySpec and Cipher cannot be resolved which I understand is that the keySpec and Cipher generated cannot be seen by the statements inside the switch blocks. How can I make it work?Java Code:try{ KeyGenerator kg=KeyGenerator.getInstance("DES"); SecretKey key=kg.generateKey(); SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES"); Cipher cipher=Cipher.getInstance("DES"); }catch(Exception e) {
Cheers,
- 01-29-2012, 01:52 PM #2
Re: How to generate and use variables in different switch statements
If you want to share variables between different blocks of code(code inside of a pair of {}) then you must define the variable outside of the blocks so that it is in scope for all the code to see and have access to. Move the variables definition outsize of the inner pair of {}s.
You can define it and give it a null value and then give it the desired value and use it inside of any inner block (within {}s)
- 01-29-2012, 04:32 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: How to generate and use variables in different switch statements
Thanks for your reply, Norm.
However, the definition is already outside of the switch blocks and wherever I put the definition, it gives the same error as long as the definitions are in "try" statement. If I remove the "try" statement, the definition gives error as the exceptions are not handled then. Is there any other way to solve this?
Cheers
-
Re: How to generate and use variables in different switch statements
No, there is no other way as Norm is giving you the correct and only way to solve this. He doesn't tell you to remove the try block, just to declare any variables that need to be shared *outside* of try's scope outside of the try block. You then *use* the variables *inside* the try block.
- 01-30-2012, 10:59 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: How to generate and use variables in different switch statements
Thanks it worked!
Another question: When I try to decode the encrypted text, I obtain something like:
instead obtaining the text I originally put inside the input Text box. What does it mean, what's the error here? It seems like an addressing error?android.widget.EditText@40f50cd8
- 01-30-2012, 11:06 PM #6
Re: How to generate and use variables in different switch statements
What you see is the classname of the object followed by @ followed by the hashcode for the object you are printing. That is the String returned by the Object class's toString() method. You could override the class's toString method and return a String you'd like to see.
If it is not your class, read the class's API doc
-
Re: How to generate and use variables in different switch statements
It's not an addressing error. What you're getting is the default toString() output of the EditText object. You were trying to print the widget out, not the text that it contains, so you'll need to fix the bug in your code.
-
Re: How to generate and use variables in different switch statements
Norm, I don't think that this is coming from a class that he created but rather the EditText android widget. So he shouldn't try to override any toString method here, but rather he needs to extract the widget's text and then print that.
- 01-30-2012, 11:12 PM #9
Re: How to generate and use variables in different switch statements
I saw that too late and added a further comment.
- 01-31-2012, 02:48 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Multiple try & catch statements within a switch
By Martyn in forum New To JavaReplies: 21Last Post: 01-12-2012, 09:24 PM -
switch statements & conditionals
By linkxs in forum New To JavaReplies: 7Last Post: 09-25-2011, 06:17 AM -
switch statements
By jim01 in forum New To JavaReplies: 7Last Post: 04-10-2011, 10:52 PM -
Using a switch/case with my if statements
By coding in forum New To JavaReplies: 2Last Post: 03-07-2011, 08:01 AM -
[SOLVED] Using dialog boxes and switch statements question
By hungdukie in forum New To JavaReplies: 2Last Post: 11-22-2008, 05:30 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks